Nginx: X-Accel-Redirect internal not working

2019-06-24 04:52发布

This is my 'internal' setup on nginx:

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

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");

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/*;

}

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
2条回答
别忘想泡老子
2楼-- · 2019-06-24 05:19
header("X-Accel-Redirect: /issues/12.pdf.zip");

And 12.pdf.zip must be located in /home/some_user/some_project/issues/

查看更多
Anthone
3楼-- · 2019-06-24 05:22

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:

In nginx/sites-available/default:

   location /issues {
       root /home/ubuntu;
       internal;
   }

in views.py:

file_name = 'some.zip'
url = '/issues/' + file_name
response = HttpResponse()
response['Content-Type'] = ""
response['X-Accel-Redirect'] = url
return response

And the full path the the Zip archive is: /home/ubuntu/issues/some.zip

查看更多
登录 后发表回答