Django Gunicorn 不加载静态文件

问题描述 投票:0回答:4

我正在尝试使用 Gunicorn 和 nginx 部署我的 django 项目,但我需要一些帮助。当我编写gunicorn myproject.wsgi:application 代码时,我设法在本地主机页面中看到我的网站,但没有任何CSS。为什么gunicorn不加载我项目静态文件夹中的css文件?

Guinicorn_start 脚本:https://dpaste.de/TAc4 Gunicorn 输出:https://dpaste.de/C6YX

python css django gunicorn django-deployment
4个回答
23
投票

Gunicorn 将仅提供动态内容,即 Django 文件。因此,您需要设置一个代理服务器(例如 nginx)来处理静态内容(您的 CSS 文件)。我假设您以正确的方式启动 Gunicorn,因此您只需配置 nginx 来提供静态文件。您可以使用如下配置,只需更改静态文件的路径:

server {
    listen       80;
    server_name  localhost;

    location / {
        root   html;
        index  index.html index.htm;
        proxy_pass http://127.0.0.1:8000;
    }
    location /static {
        autoindex on;
        alias /path/to/staticfiles;
    }

    error_page   500 502 503 504  /50x.html;
    location = /50x.html {
        root   html;
    }
}

即使设置了这个配置,你也必须调用“./manage.pycollectstatic”来让你的css工作


4
投票

Gunicorn 只负责为您网站的 django 方面提供服务。

静态文件需要由nginx提供服务。请参阅:如何使用 nginx 和 Gunicorn 为 Django 应用程序提供静态文件?。这应该在

nginx.conf
文件中配置。如果您将其发布在这里,我们可以看一下。


0
投票

我以前也遇到过这个问题。 尝试在 Nginx 配置中设置静态路径,然后授予项目权限。

sudo chmod -R 777 /webapps/yourweb/

尝试再次启动服务器。 希望有帮助。


0
投票

Gunicorn 维护者最近解决了一个关于

reload-extra-file
的问题(2023 年 12 月 27 日)。

在 Github 上提出了一个关于 json 文件的问题:Opened issues

--reload-extra-file 参数旨在在更改时重新加载额外的文件,除了 Python 文件(即

--reload = True
)。团队之一决定打开拉取请求来解决问题:打开拉取

因此,从最近的版本开始,可能是在 20.1.x 之后,有以下选项(我在gunicorn.config.py 上的案例示例):

reload_extra_file = ['static/css/*.css', 'static/js/*.js', 'templates/*.html']

还有一个细节给我带来了一些问题。 文档引用了

reload-extra-files
(复数)。但正确的参数是单数:
reload-extra-file
,如文档示例所示,位于参考下方。

© www.soinside.com 2019 - 2024. All rights reserved.