pythondjangonginxx-sendfile

Django sendfile with nginx failed to send the file as attachment


I'm using django-sendfile for sending a large file as attachment to the UI. This works with sendfile.backends.simple backend but when I use sendfile.backends.nginx, it returns 404 error on nginx logs.

I think there was something wrong with my nginx configuration.

views.py

class FileDownloadView(View):
    def get(self, request, id_file):
        obj = FileCombined.objects.get(id_file=id_file)
        return sendfile(request, obj.path, attachment=True)

urls.py

from django.conf.urls import url
from django.urls import path

urlpatterns += [
    path(r'file/id/<str:id_file>/delete/', FileDeleteView.as_view(), name='files-delete'),
    path(r'file/id/<str:id_file>/download/', FileDownloadView.as_view(), name='files-download'),
    ]

settings.py

MEDIA_URL = '/media/'
MEDIA_ROOT = os.path.join(BASE_DIR, 'media')
SENDFILE_BACKEND = "sendfile.backends.nginx"
SENDFILE_ROOT = os.path.join(BASE_DIR, 'media')
SENDFILE_URL = '/media'

/etc/nignx/sites-enabled/myapp

server { # simple reverse-proxy
    listen       80;
    server_name  localhost;
    sendfile            on;

    # serve static files
    location /static {
       proxy_pass      http://127.0.0.1:8000/static;
    }

    location /media/ {
        internal;
        root  /home/foo/project/django-combine/media;
    }

    location / {
      proxy_pass      http://127.0.0.1:8000;
    }
}

nginx/access.log

127.0.0.1 - - [30/Dec/2017:08:20:01 +0530] "GET /file/id/5114a7721b6943fb31fc143a20adbec630bb5eb2516d7574b881454209338eed/download/ HTTP/1.1" 404 208 "-" "Mozilla/5.0 (X11; Linux x86_64) AppleWebKit/537.36 (KHTML, like Gecko) Ubuntu Chromium/52.0.2743.116 Chrome/52.0.2743.116 Safari/537.36"
127.0.0.1 - - [30/Dec/2017:08:25:01 +0530] "GET /file/id/5114a7721b6943fb31fc143a20adbec630bb5eb2516d7574b881454209338eed/download/ HTTP/1.1" 404 208 "-" "Mozilla/5.0 (X11; Linux x86_64) AppleWebKit/537.36 (KHTML, like Gecko) Ubuntu Chromium/52.0.2743.116 Chrome/52.0.2743.116 Safari/537.36"

Actually the file I want to download was exists inside /home/foo/project/django-combine/media directory.

print(obj.path) exists in my view prints

/home/foo/project/django-combine/media/google-oauth2|817468732648743/33601a74-de02-4b83-8587-1f8c3d8c96fb/output/combined.csv

Solution

  • You need to change

    root /home/foo/project/django-combine/media/;
    

    to

    root /home/foo/project/django-combine/; 
    

    As have /media in root makes the effective path to root /home/foo/project/django-combine/media/media.

    You could have even used alias /home/foo/project/django-combine/media/; instead of root /home/foo/project/django-combine/media/. That also would have worked. As for aliases the the location path doesn't get appended

    Also you need to make sure you don't have special symbols like | which makes it difficult for nginx to locate the file