Nginx:X-Accel-Redirect内部不起作用 [英] Nginx: X-Accel-Redirect internal not working
问题描述
这是我在nginx上的内部"设置:
This is my 'internal' setup on nginx:
location /issues/ {
root /home/some_user/some_project/;
internal;
}
当我注释掉内部"部分时,Nginx提供"issue"文件夹中的文件,并且其中的文件也很好.但是即使启用了它,我仍然无法通过Python或PHP代码提供任何文件:
When I comment out the "internal" part, Nginx serves the file in the "issue" folder and the files in it just fine. But even when it's enabled I am still unable to serve any files from the Python or PHP code:
# Python, outputs an empty zip
import os
file_name = '12.pdf.zip'
response = HttpResponse()
response['Content-Disposition'] = 'attachment; filename=%s' % t_str(file_name)
response['Content-Length'] = os.path.getsize(file_path)
response['Content-Type'] = "application/zip"
response['X-Accel-Redirect'] = '12.pdf.zip'
return response
# PHP, does nothing
header("X-Accel-Redirect: 12.pdf.zip");
这些是启用了网站的默认设置和nginx.conf. 8000适用于Django,而81适用于PHP
And these are sites-enabled/default and nginx.conf. 8000 is for Django and 81 is for PHP
server {
listen 80; ## listen for ipv4
server_name localhost;
access_log /var/log/nginx/localhost.access.log;
location / {
# proxy / requests to apache running django on port 8081
proxy_pass http://127.0.0.1:8000/;
proxy_redirect off;
proxy_set_header Host $host;
proxy_set_header X-Real-IP $remote_addr;
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
proxy_max_temp_file_size 0;
client_max_body_size 10m;
client_body_buffer_size 128k;
proxy_connect_timeout 90;
proxy_send_timeout 90;
proxy_read_timeout 90;
proxy_buffer_size 4k;
proxy_buffers 4 32k;
proxy_busy_buffers_size 64k;
proxy_temp_file_write_size 64k;
}
location /issues/ {
root /home/some_user/some_project/;
internal;
}
}
worker_processes 1;
error_log /var/log/nginx/error.log;
pid /var/run/nginx.pid;
events {
worker_connections 1024;
# multi_accept on;
}
http {
# include /etc/nginx/mime.types;
access_log /var/log/nginx/access.log;
sendfile on;
#tcp_nopush on;
#keepalive_timeout 0;
keepalive_timeout 65;
tcp_nodelay on;
gzip on;
gzip_disable "MSIE [1-6]\.(?!.*SV1)";
proxy_redirect off;
proxy_set_header Host $host;
proxy_set_header X-Real-IP $remote_addr;
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
proxy_max_temp_file_size 0;
proxy_connect_timeout 90;
proxy_send_timeout 90;
proxy_read_timeout 90;
proxy_buffer_size 4k;
proxy_buffers 4 32k;
proxy_busy_buffers_size 64k;
proxy_temp_file_write_size 64k;
include /etc/nginx/sites-enabled/*;
}
我尝试将Nginx配置更改为别名,也是一样.并尝试了所有可能的文件路径.谁能看到什么问题?还是给我一些指示?谢谢你.
I've tried changing the Nginx config to alias, same thing. And tried all possible file paths. Can anyone see what's wrong? Or give me some pointers? Thank you.
推荐答案
我不知道为什么以前无法使它工作.也许Nginx Wiki文档是错误的?这是我解决的方法:
I don't know why I couldn't get it to work before. Maybe the Nginx Wiki docs is wrong? This is how I solved it:
在nginx/sites-available/default中:
In nginx/sites-available/default:
location /issues {
root /home/ubuntu;
internal;
}
在views.py中:
in views.py:
file_name = 'some.zip'
url = '/issues/' + file_name
response = HttpResponse()
response['Content-Type'] = ""
response['X-Accel-Redirect'] = url
return response
Zip存档的完整路径为: /home/ubuntu/issues/some.zip
And the full path the the Zip archive is: /home/ubuntu/issues/some.zip
这篇关于Nginx:X-Accel-Redirect内部不起作用的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!