【问题标题】:static files with gunicorn, django and nginx带有 gunicorn、django 和 nginx 的静态文件
【发布时间】:2012-11-20 02:44:25
【问题描述】:

我有一个 django 项目,想使用 gunicorn 和 nginx 部署它。

到目前为止一切正常,但是当我添加子域时,静态文件没有提供,我的页面看起来很糟糕!

如果我改用本地主机,一切都会完美!

我在这里留下我的 nginx.conf:

server {
    listen 80 default;
    client_max_body_size 4G; 
    server_name mytest.dev;

    keepalive_timeout 5;

    # path for static files
    root /Users/danielrodriguez/workspace/mtt/static;

    location / { 
        # checks for static file, if not found proxy to app 
        try_files $uri @proxy_to_app;
    }   

    location @proxy_to_app {
        proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
        proxy_set_header Host $http_host;
        proxy_redirect off;

        proxy_pass   http://localhost:8000;
    }   
}

我的 hosts 文件中也有这个:

127.0.0.1   localhost
127.0.0.1   mytest.dev
255.255.255.255 broadcasthost
::1             localhost 
fe80::1%lo0 localhost

当我在浏览器中输入“mydev.test”时,如何让 nginx 像我在浏览器中输入“localhost”时那样工作?几乎我想做的就是使用 apache 中的虚拟主机之类的东西在同一个物理服务器中为一堆站点提供服务。

PD:我也在使用 OS Lion,以防万一。

【问题讨论】:

    标签: django nginx hosts gunicorn static-files


    【解决方案1】:

    看来你的root 设置有误。

    root /Users/danielrodriguez/workspace/mtt/static;
    

    您应该检查您的 css 文件所请求的 URL。假设您有STATIC_URL = '/static/',您的浏览器可能会想要加载/static/css/styles.css 或类似的东西。

    所以,./static/css/styles.css 文件应该在root 目录下。

    那么正确的root应该是:

    root /Users/danielrodriguez/workspace/mtt;
    

    当然,将root 设置为项目根目录确实不是一个好主意。所以'您可以为staticmedia 创建符号链接以在您的项目之外分隔目录。

    希望对您有所帮助。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2015-11-30
      • 2013-08-12
      • 2014-05-25
      • 2023-03-05
      • 2017-10-27
      • 2016-01-02
      • 2013-06-27
      • 2017-05-31
      相关资源
      最近更新 更多