Nginx 不提供静态文件/Django

Nginx not serving static files /Django

当我使用 Nginx 将项目部署到服务器时,Gunicorn 静态文件未加载。

这是我的网站部署后的样子

nginx.service如下


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')

我有 运行 collectstatic 命令,它给出了一些已经在目标路径中找到的警告文件,但这只是一个警告,而不是错误。

nginx错误日志如下

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/"

控制台错误:

P.S:在 nginx.service 文件中将根更改为别名也无济于事。 我想这与 gunicorn 无关。

nginx 正在尝试在 /etc/nginx/home/zp_dashboard_testing_python/zp_main/staticfiles/ 中查找静态文件。但看起来您的静态文件在 /home/zp_dashboard_testing_python/zp_main/staticfiles 中。像@Niloct 建议的那样,将 location /static/ 更改为

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