【问题标题】:django+gunicorn+nginx wierdnessdjango+gunicorn+nginx
【发布时间】:2013-03-25 16:00:00
【问题描述】:

我正在按照http://senko.net/en/django-nginx-gunicorn/ 中的说明部署我的 django 应用程序。我的应用程序在 gunicorn 下运行,并且我已配置 nginx 来为该站点提供服务。一切都很好,只是静态文件在实际静态文件目录的“静态”子目录中查看。

我在 nginx 配置文件中有以下静态文件节:

location /static {
    root /webapps/myproject/project_staticfiles;
}

/webapps/myproject/project_staticfiles 是我拥有所有静态文件的地方(我使用 manage.py collectstatic 将静态文件放在那里)。但是,在 nginx 配置中配置的端口访问站点时,找不到它们。 error.log 显示它们在更深的目录中查看:/webapps/myproject/project_staticfiles/static。

确实,当我创建一个新目录“静态”并将所有静态文件放入其中时,一切都找到了。在我的项目 settings.py 文件中,我有:

STATIC_URL = '/static/'
STATIC_ROOT = '/webapps/myproject/project_staticfiles'

导致这种奇怪行为的设置是什么?

【问题讨论】:

    标签: django nginx gunicorn


    【解决方案1】:

    我认为您希望使用alias

    location /static {
        alias /webapps/myproject/project_staticfiles;
    }
    

    如果您阅读 root 上的文档,它会指出:

    root 指定请求的文档根目录。例如,使用此配置:

    location  /i/ {   
        root  /spool/w3; 
    } 
    

    对“/i/top.gif”的请求将返回文件“/spool/w3/i/top.gif”。 您可以在参数中使用变量。

    root 将目录附加到文件路径中,alias 没有,这就是为什么 static 会附加到您的根目录以形成 /webapps/myproject/project_staticfiles/static

    【讨论】:

    • 太好了,就是这样!非常感谢。
    猜你喜欢
    • 2013-03-01
    • 2021-03-19
    • 2017-12-08
    • 2016-04-24
    • 2013-10-05
    • 2012-05-27
    • 2015-12-24
    • 2023-03-20
    • 2013-12-15
    相关资源
    最近更新 更多