让 nginx 为静态文件提供服务(django、gunicorn)

getting nginx to serve static files (django, gunicorn)

DEBUG = True 时,Django 提供静态文件,我可以看到我的站点和管理页面,样式正确并且 运行 正确的 js。我的直觉是我错误配置了 nginx,我正在努力寻找原因。一些背景:

sudo nginx -t && sudo systemctl restart nginx
nginx: the configuration file /etc/nginx/nginx.conf syntax is ok
nginx: configuration file /etc/nginx/nginx.conf test is successful
cat sites-enabled/mysite
server {
    listen 80;
    server_name IP_ADDR;

    location = /favicon.ico { access_log off; log_not_found off; }
    location /static {
        root /home/ubuntu/mysite;
    }

    location / {
        include proxy_params;
        proxy_pass http://unix:/run/gunicorn.sock;
    }
}

符号链接到 sites-available/mysite

/var/log/nginx/error.log只有2020/03/01 19:57:49 [notice] 20644#20644: signal process started行,但是访问日志有很多404。在尝试访问应用程序 URL 时,chrome 开发控制台显示:

Refused to apply style from 'IP_ADDR:8000/static/css/console.css' because its MIME type ('text/html') is not a supported stylesheet MIME type, and strict MIME checking is enabled.
IP_ADDR/:9 GET IP_ADDR:8000/static/js/jquery.min.js net::ERR_ABORTED 404 (Not Found)
IP_ADDR/:11 GET IP_ADDR:8000/static/js/console.js net::ERR_ABORTED 404 (Not Found)
IP_ADDR/:10 GET IP_ADDR:8000/static/js/content.js net::ERR_ABORTED 404 (Not Found)
IP_ADDR/:11 GET IP_ADDR:8000/static/js/console.js net::ERR_ABORTED 404 (Not Found)
favicon.ico:1 GET IP_ADDR:8000/static/favicon.ico 404 (Not Found)

我的 nginx.conf 中有 include /etc/nginx/mime.types;,并且 mime.types 包含行

text/html html htm shtml;,

虽然我认为这是正确的,因为我的 css 文件应该是 text/css,但我不知道克服这个障碍的最佳方法。我有 运行 python manage.py collectstatic,我的项目结构是这样的:

(venv) ubuntu@IP_ADDR:~/mysite$ tree -L 1
.
├── README.md
├── manage.py
├── mysite
├── requirements.txt
├── static
├── venv
└── mysite_views_urls_templates

我不确定我是否提供了足够的信息,我真的想加深对整个堆栈的理解。

答案是我 运行 gunicorn 手动通过端口 8000,阻止 nginx 传送静态文件。