簡體   English   中英

Nginx 不提供 static 個文件 /Django

[英]Nginx not serving static files /Django

當我使用 Nginx 和 Gunicorn static 將項目部署到服務器時,文件未加載。

在此處輸入圖像描述

這是我的網站部署后的樣子

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; }
}

}

設置.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/"

控制台錯誤: 在此處輸入圖像描述

PS:在 nginx.service 文件中將 root 更改為別名也無濟於事。 我想這與 gunicorn 無關。

nginx 正在嘗試在/etc/nginx/home/zp_dashboard_testing_python/zp_main/staticfiles/中查找 static 個文件。 但看起來您的 static 文件在/home/zp_dashboard_testing_python/zp_main/staticfiles中。 就像@Niloct 建議的那樣,將location /static/更改為

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

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM