簡體   English   中英

讓 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 dev 控制台顯示:

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)

我已經include /etc/nginx/mime.types; 在我的 nginx.conf 中,並且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

我不確定我是否提供了足夠的信息,我真的想加深對整個堆棧的理解。

答案是我在端口 8000 上手動運行 gunicorn,阻止 nginx 提供靜態文件。

暫無
暫無

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

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