django production deployment does not load static files

252 views Asked by At

I've been trying for about a whole day to deploy Django's static files in production, but till now I had no luck, so I absolutely need community's help!

My nginx config is:

worker_processes 1;

user nobody nogroup;
pid /tmp/nginx.pid;
error_log /tmp/nginx.error.log;

events {
    worker_connections 1024;
    accept_mutex off;
}

http {
    include mime.types;
    default_type application/octet-stream;
    access_log /tmp/nginx.access.log combined;
    sendfile on;

    upstream app_server {
        server unix:/tmp/gunicorn.sock fail_timeout=0;
        # For a TCP configuration:
        # server 192.168.0.7:8000 fail_timeout=0;
    }

    server {
        listen 80 default;
        client_max_body_size 4G;
        server_name _;

        keepalive_timeout 5;

        # path for static files
        root /home/ubuntu/src/static;

        location / {
            # checks for static file, if not found proxy to app
            try_files $uri @proxy_to_app;
        }

        location @proxy_to_app {
            proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
            proxy_set_header Host $http_host;
            proxy_redirect off;

            proxy_pass   http://app_server;
        }

        error_page 500 502 503 504 /500.html;
        location = /500.html {
            root /home/ubuntu/src/static;
        }
    }
}

and in my django settings.py file I've set the following:

STATIC_URL = '/static/'
STATICFILES_DIRS = ()
STATICFILES_FINDERS = (
'django.contrib.staticfiles.finders.FileSystemFinder',
'django.contrib.staticfiles.finders.AppDirectoriesFinder',
 )
STATIC_ROOT = os.path.join(BASE_DIR, 'static')

and of course I've run the collectstatic django command and the folder in path /home/ubuntu/src/static exists containing all appropriate files.

Still my deployment does not server any static file :/

Does anybody have any idea what might be wrong with my setup?

Thank you in advance

2

There are 2 answers

0
skolsuper On

I used a great tutorial here for my setup. Hopefully this can help. One obvious change I can see is using location /static/ for static files and just forwarding location / straight to gunicorn (or uwsgi in my case)

5
moonstruck On

Add static alias to server conf

location /static {
            alias /home/ubuntu/src/static; # your Django project's static files - amend as required
         }

Full configuration should be like

server {
        listen 80 default;
        client_max_body_size 4G;
        server_name _;

        keepalive_timeout 5;

        # path for static files
        # root /home/ubuntu/src/static;

        location /static {
            alias /home/ubuntu/src/static; # your Django project's static files - amend as required
         }

        location / {
            # checks for static file, if not found proxy to app
            try_files $uri @proxy_to_app;
        }



        location @proxy_to_app {
            proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
            proxy_set_header Host $http_host;
            proxy_redirect off;

            proxy_pass   http://app_server;
        }

        error_page 500 502 503 504 /500.html;
        location = /500.html {
            root /home/ubuntu/src/static;
        }
    }
}