简体   繁体   中英

Nginx not serving static files /Django

When I deploy my project to server using Nginx and Gunicorn static files are not loading.

在此处输入图像描述

This is how my website looks after deploying

nginx.service is as follows


user  nginx;
worker_processes  auto;

error_log  /var/log/nginx/error.log notice;
pid        /var/run/nginx.pid;


events {
    worker_connections  1024;
}


http {
    include       /etc/nginx/mime.types;
    default_type  application/octet-stream;

    log_format  main  '$remote_addr - $remote_user [$time_local] "$request" '
                      '$status $body_bytes_sent "$http_referer" '
                      '"$http_user_agent" "$http_x_forwarded_for"';

    access_log  /var/log/nginx/access.log  main;

    sendfile        on;
    #tcp_nopush     on;

    keepalive_timeout  65;

    #gzip  on;

    include /etc/nginx/conf.d/*.conf;

    server {
    listen 8081;
    server_name 0.0.0.0;
    location = /favicon.ico { access_log off; log_not_found off; }
    location /static/ {
       root home/zp_dashboard_testing_python/zp_main/staticfiles;
    }
    location / {
        proxy_set_header Host $http_host;
        proxy_set_header X-Real-IP $remote_addr;
        proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
        proxy_set_header X-Forwarded-Proto $scheme;
        proxy_pass http://unix:/run/gunicorn.sock; }
}

}

settings.py

BOOTSTRAP4 = {
    'include_jquery': True,
}


STATIC_URL = '/static/'

STATICFILES_DIRS = [
    os.path.join(BASE_DIR, "static")
]

STATIC_ROOT = os.path.join(BASE_DIR, 'staticfiles')

I have run collectstatic command and it gives some warnings file already found at destination path, but its just a warning and not an error.

nginx error logs are as follows

2022/05/01 19:27:49 [error] 77154#77154: *5 open() "/etc/nginx/home/zp_dashboard_testing_python/zp_main/staticfiles/static/assets/libs/datatables.net-buttons/js/buttons.html5.min.js" failed (2: No such file or directory), client: 10.81.234.6, server: 0.0.0.0, request: "GET /static/assets/libs/datatables.net-buttons/js/buttons.html5.min.js HTTP/1.1", host: "10.10.89.25:8081", referrer: "http://10.10.89.25:8081/dashboard/"
2022/05/01 19:27:49 [error] 77154#77154: *7 open() "/etc/nginx/home/zp_dashboard_testing_python/zp_main/staticfiles/static/assets/libs/datatables.net-buttons/js/buttons.print.min.js" failed (2: No such file or directory), client: 10.81.234.6, server: 0.0.0.0, request: "GET /static/assets/libs/datatables.net-buttons/js/buttons.print.min.js HTTP/1.1", host: "10.10.89.25:8081", referrer: "http://10.10.89.25:8081/dashboard/"
2022/05/01 19:27:49 [error] 77152#77152: *6 open() "/etc/nginx/home/zp_dashboard_testing_python/zp_main/staticfiles/static/assets/libs/datatables.net-buttons/js/buttons.colVis.min.js" failed (2: No such file or directory), client: 10.81.234.6, server: 0.0.0.0, request: "GET /static/assets/libs/datatables.net-buttons/js/buttons.colVis.min.js HTTP/1.1", host: "10.10.89.25:8081", referrer: "http://10.10.89.25:8081/dashboard/"
2022/05/01 19:27:49 [error] 77154#77154: *1 open() "/etc/nginx/home/zp_dashboard_testing_python/zp_main/staticfiles/static/assets/libs/datatables.net-responsive/js/dataTables.responsive.min.js" failed (2: No such file or directory), client: 10.81.234.6, server: 0.0.0.0, request: "GET /static/assets/libs/datatables.net-responsive/js/dataTables.responsive.min.js HTTP/1.1", host: "10.10.89.25:8081", referrer: "http://10.10.89.25:8081/dashboard/"
2022/05/01 19:27:49 [error] 77154#77154: *7 open() "/etc/nginx/home/zp_dashboard_testing_python/zp_main/staticfiles/static/assets/libs/datatables.net-responsive-bs4/js/responsive.bootstrap4.min.js" failed (2: No such file or directory), client: 10.81.234.6, server: 0.0.0.0, request: "GET /static/assets/libs/datatables.net-responsive-bs4/js/responsive.bootstrap4.min.js HTTP/1.1", host: "10.10.89.25:8081", referrer: "http://10.10.89.25:8081/dashboard/"
2022/05/01 19:27:49 [error] 77154#77154: *5 open() "/etc/nginx/home/zp_dashboard_testing_python/zp_main/staticfiles/static/assets/libs/admin-resources/rwd-table/rwd-table.min.js" failed (2: No such file or directory), client: 10.81.234.6, server: 0.0.0.0, request: "GET /static/assets/libs/admin-resources/rwd-table/rwd-table.min.js HTTP/1.1", host: "10.10.89.25:8081", referrer: "http://10.10.89.25:8081/dashboard/"
2022/05/01 19:27:49 [error] 77154#77154: *4 open() "/etc/nginx/home/zp_dashboard_testing_python/zp_main/staticfiles/static/assets/js/pages/table-responsive.init.js" failed (2: No such file or directory), client: 10.81.234.6, server: 0.0.0.0, request: "GET /static/assets/js/pages/table-responsive.init.js HTTP/1.1", host: "10.10.89.25:8081", referrer: "http://10.10.89.25:8081/dashboard/"
2022/05/01 19:27:49 [error] 77152#77152: *6 open() "/etc/nginx/home/zp_dashboard_testing_python/zp_main/staticfiles/static/assets/js/pages/datatables.init.js" failed (2: No such file or directory), client: 10.81.234.6, server: 0.0.0.0, request: "GET /static/assets/js/pages/datatables.init.js HTTP/1.1", host: "10.10.89.25:8081", referrer: "http://10.10.89.25:8081/dashboard/"

Console error: 在此处输入图像描述

PS: Changing root to alias in nginx.service file didn't help either. I guess this doesn't concern with gunicorn.

nginx is trying to find static files in /etc/nginx/home/zp_dashboard_testing_python/zp_main/staticfiles/ . But it looks like your static files are in /home/zp_dashboard_testing_python/zp_main/staticfiles . Like @Niloct suggested, change location /static/ to

location /static/ {
       root /home/zp_dashboard_testing_python/zp_main/staticfiles;
    }

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM