【问题标题】:Django Gunicorn not load static filesDjango Gunicorn 不加载静态文件
【发布时间】:2013-12-09 03:02:27
【问题描述】:

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

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

【问题讨论】:

标签: python css django gunicorn django-deployment


【解决方案1】:

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.py collectstatic”来让你的css工作

【讨论】:

  • 不是一个真正的答案。我想发起者和我一样想知道如何让 gunicorn 为这些静态文件提供服务。这也不是一个答案,因为它没有为使用 gunicorn 提供相当完整的 nginx 配置。
  • 我应该把这个粘贴到哪里?
【解决方案2】:

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

sudo chmod -R 777 /webapps/yourweb/

尝试再次启动您的服务器。 希望对您有所帮助。

【讨论】:

    【解决方案3】:

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

    静态文件需要由 nginx 提供服务。请参阅:How exactly do I server static files with nginx and gunicorn for a Django app?。这应该在nginx.conf 文件中配置。如果你把它贴在这里,我们可以看看它。

    【讨论】:

    • 我遵循了本指南goo.gl/wrRJPa,并将我的站点添加到了 nginx 的可用站点(代码:dpaste.de/q5Qa),并且我没有根据指南更改 nginx.conf 文件。
    猜你喜欢
    • 2021-08-08
    • 2016-01-02
    • 2020-01-04
    • 2016-06-01
    • 2017-11-08
    • 1970-01-01
    • 2017-05-31
    • 2015-03-30
    • 2012-04-30
    相关资源
    最近更新 更多