[英]Tried everything but still cannot serve static files of Django project via nginx+gunicorn
[英]Problems with nginx+gunicorn resolving static content
我正在使用 django 應用程序(askbot 開源項目)、gunicorn 和 nginx 設置環境。 nginx 和 gunicorn 在不同的 docker 容器中。
這是我的 nginx 配置。
server {
listen 80;
location /static {
alias /askbot/static;
}
location /m {
alias /askbot/static;
}
location / {
proxy_set_header Host $host;
proxy_set_header X-Real-IP $remote_addr;
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
proxy_pass http://askbot:8080/;
}
}
If I run the django app on debug mode, everything is ok, I can go through nginx and I see how nginx is only invoking gunicorn for the dynamic content, and the static content is resolved locally.
But, when I run the django application with debug false, nginx does not revolve the static content, and if I see the source code for the web page, I can see all the paths for the static content have changed, using somethign like "/米/緩存...”。 我想這就是 nginx 無法再解析 static 內容的原因。
例如,當使用調試模式等於 true 時,這是 html 源代碼的一個片段。
<link rel="stylesheet" href="/m/CACHE/css/9275d0e5b87f.css" type="text/css" /> <link href="//fonts.googleapis.com/css?family=Open+Sans+Condensed:700&subset=latin-ext" rel="stylesheet" type="text/css" /> <link href="//fonts.googleapis.com/css?family=Open+Sans+Condensed:700&subset=cyrillic-ext" rel="stylesheet" type="text/css" /> <script type="text/javascript" src="/m/default/media/jslib/modernizr.custom.js?v=1"></script> <script type="text/javascript">
並且當使用debug 運行時等於false,同樣的片段是這樣的。
<link href="/m/default/media/style/style.css?v=1" rel="stylesheet" type="text/css" /> <link href="/m/default/media/fa-4.3.0/css/font-awesome.css?v=1" rel="stylesheet" type="text/css" /> <link href="/m/default/media/bootstrap/css/bootstrap.css?v=1" rel="stylesheet" type="text/css" /> <link href="//fonts.googleapis.com/css?family=Open+Sans+Condensed:700&subset=latin-ext" rel="stylesheet" type="text/css" /> <link href="//fonts.googleapis.com/css?family=Open+Sans+Condensed:700&subset=cyrillic-ext" rel="stylesheet" type="text/css" /> <script type="text/javascript" src="/m/default/media/jslib/modernizr.custom.js?v=1"></script> <script type="text/javascript">
我已經解決了兩天,誰能向我解釋為什么會發生這種情況以及如何解決?,以不同的方式配置 nginx,或禁用 django 以具有該行為。
提前致謝, 埃斯特班·科拉多
好的,我能夠解決我的問題,
它是由 settings.py 文件中包含的壓縮器應用程序引起的。
INSTALLED_APPS = (
...
'compressor'
...
)
我正在部署的 django 應用程序需要該應用程序,所以我所做的是禁用添加到同一文件的壓縮。
COMPRESS_ENABLED = False
現在,我沒有看到我在問題中描述的行為,所有 static 文件都添加到了 URL 中的 CACHE 文件夾中。
BR,埃斯特萬·科拉多
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.