繁体   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