Nginx:X-Accel-Redirect内部无效

时间:2011-04-05 04:26:51

标签: nginx

这是我在nginx上的'内部'设置:

location /issues/ {
    root /home/some_user/some_project/;
    internal;
}

当我注释掉“内部”部分时,Nginx在“issue”文件夹中提供文件,其中的文件就可以了。但即使启用它,我仍然无法提供Python或PHP代码中的任何文件:

# 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

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配置更改为别名,同样的事情。并尝试了所有可能的文件路径。任何人都可以看到什么是错的?或者给我一些指示?谢谢。

2 个答案:

答案 0 :(得分:5)

我不知道为什么我以前无法上班。也许Nginx Wiki文档错了?这就是我解决它的方法:

在nginx / sites-available / default中:

   location /issues {
       root /home/ubuntu;
       internal;
   }
在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

答案 1 :(得分:1)

header("X-Accel-Redirect: /issues/12.pdf.zip");

12.pdf.zip必须位于/ home / some_user / some_project / issues /