I'm now deploying django projects on CentOS and have a problem with X-Accel-Redirect for protected file serving.
Here is my nginx.conf
server
{
listen 80;
server_name example;
index index.html index.htm index.php;
root /www/server/example;
charset UTF-8;
access_log /var/log/nginx/myproject_access.log;
error_log /var/log/nginx/myproject_error.log;
client_max_body_size 75M;
location /public/ {
root /www/wwwroot/myproject/;
}
location /media/ {
root /www/wwwroot/myproject/;
internal;
}
location / {
include uwsgi_params;
uwsgi_pass django;
}
...
}
Of course, protected files are on /www/wwwroot/myproject/media.
And corresponding python view file is following.
class ProtectedView(generics.GenericAPIView):
permission_classes = [IsAuthenticated]
def get(self, request, id, target):
file_name = "1.png"
response = HttpResponse()
response["X-Accel-Redirect"] = "/media/{0}".format(file_name)
response["Content-Disposition"] = "attachment; filename={0}".format(file_name)
return response
But server returns 404 error.
And the myproject_error.log is like this.
[error] 24570#0: *5 open() "/www/server/example/media/1.png" failed (2: No such file or directory), client: 174.11.13.81, server: example, request: "GET /protected-view/ HTTP/1.1", upstream: "uwsgi://0.0.0.0:8008", host: "40.1.12.23"
Maybe location /media/ {} block is not working. What problem? I have changed the permission but not working.
PS: I'm using django rest framework.