Django Gunicorn没有加载静态文件

时间:2013-11-24 13:48:15

标签: python css django gunicorn django-deployment

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

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

3 个答案:

答案 0 :(得分:11)

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工作

答案 1 :(得分:2)

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

静态文件需要由nginx提供。见:How exactly do I server static files with nginx and gunicorn for a Django app?。这应该在nginx.conf文件中配置。如果您在此处发布,我们可以查看它。

答案 2 :(得分:0)

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

sudo chmod -R 777 /webapps/yourweb/

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

相关问题