Django + Nginx - serve private files using X-Accel-Redirect

792 Views Asked by At

I'm new to this, but my understanding of how this works is as follows:

Request private file from nginx

nginx forwards this to django

django does its thing, and returns a response containing the X-Accel-Redirect header

nginx sees this header, and serves the file.

I've set up my nginx conf like so:

upstream foo_app_server {
  server unix:/home/project_dir/gunicorn.sock fail_timeout=0;
}

server {

    listen   80;
    server_name foo.com;

    client_max_body_size 4G;

    location /static/ {
        alias   /home/projec_dir/static/;
    }

    location /media/private/ {
        internal;
        alias   /home/project_dir/media/private/;

    }

    location /media/ {
        alias   /home/project_dir/media/public/;
    }

    location / {
        proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
        proxy_pass http://foo_app_server;
    }

}

Now, django is successfully receiving the request for files in media/private, and it is successfully attaching a X-Accel-Redirect header to the response.

I've tried the following, for the content of the header, when the requested path is of the form media/private/some/path/to/file.pdf

X-Accel-Redirect:
    media/private/some/path/to/file.pdf
    private/some/path/to/file.pdf
    path/to/file.pdf

All of them gave me 404 errors. The file definitely does exist.

I think this is just path issue, of a mismatch between the path in the header, and the location and the alias in the nginx conf, but I've tried for awhile and I can't get the right combination.

Could somebody who has done this before suggest what exactly nginx is looking for?

0

There are 0 best solutions below