Django + Nginx + X-Accel-Redirect

258 views
Skip to first unread message

Neto

unread,
Mar 31, 2016, 12:04:07 PM3/31/16
to Django users
How do I access protected directories using Django? I have the following configuration but is not working, there is always page 404 when I try to access the directory.

Django:

def test(request):
    file_url
= 'http://mysite.com/media/users/avatar.png'
    response
= HttpResponse()
    response
['X-Accel-Redirect'] = file_url
   
return response

Nginx:

server {
    listen
80;
    server_name mysite
.com;

    location
/media/ {
       
internal;
       
alias /home/ubuntu/webapps/myapp/media/;
   
}
}



Kristofer Pettijohn

unread,
Mar 31, 2016, 9:37:48 PM3/31/16
to django...@googlegroups.com
Try changing file_url to be relative: 

file_url = '/media/users/avatar.png'

Also check the nginx error log to make sure the path is being constructed properly by nginx.


From: "Neto" <paulosou...@gmail.com>
To: "Django users" <django...@googlegroups.com>
Sent: Thursday, March 31, 2016 11:04:07 AM
Subject: Django + Nginx + X-Accel-Redirect

--
You received this message because you are subscribed to the Google Groups "Django users" group.
To unsubscribe from this group and stop receiving emails from it, send an email to django-users...@googlegroups.com.
To post to this group, send email to django...@googlegroups.com.
Visit this group at https://groups.google.com/group/django-users.
To view this discussion on the web visit https://groups.google.com/d/msgid/django-users/c965f092-bf02-4146-8a5f-2e5f7fef90db%40googlegroups.com.
For more options, visit https://groups.google.com/d/optout.
Reply all
Reply to author
Forward
0 new messages