Search code examples
djangonginxdjango-staticfilesstatic-files

Django/Nginx: statics files are not served


I try to deploy my Django project in a remote server using Nginx but my files are not served.

I guess my path is incorrect but don't really know why...

python3 manage.py collectstatic get all my files in intensecov_static folder.

/home/zebra/

- intensecov_app
    - intensecov
        - coverage (project)
        - manage.py
        - static
        - ...
- intensecov_static
    - css
       - style.css
    - images
    - ...

settings.py

STATIC_URL = '/static/'

STATICFILES_DIRS = (
    os.path.join(BASE_DIR,'static'),
    os.path.join(BASE_DIR,'randomization_management/static'),
    os.path.join(BASE_DIR,'randomization_settings/static'),
    os.path.join(BASE_DIR,'randomization/static'),
)

PROJECT_ROOT = os.path.dirname(os.path.abspath(__file__))

STATIC_ROOT = '/home/zebra/intensecov_static'

/etc/nginx/sites-available/intensecov

server {
        listen 80;
        server_name 192.168.80.9;
        root /home/zebra/intensecov_app;

        location /static {
                alias /home/zebra/intensecov_static;
        }

        location / {
                proxy_set_header Host $http_host;
                proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
                proxy_redirect off;
                if (!-f $request_filename) {
                        proxy_pass http://127.0.0.1:8000;
                        break;
                }
        }
}


Solution

  • Wishing your nginx.conf you declared:

        location /static {
                alias /home/zebra/intensecov_static;
        }
    

    Try this:

        location /static {
                alias /home/zebra/intensecov_static/;
        }
    

    It seems nginx needs the trailing / to make the folder work. Or at least that the serving of static-files broke when removing the trailing /.