简体   繁体   English

带有 gunicorn、django 和 nginx 的静态文件

[英]static files with gunicorn, django and nginx

I have a django project and want to deploy it using gunicorn and nginx.我有一个 django 项目,想使用 gunicorn 和 nginx 部署它。

So far everything works, but when I add subdomains, static files are not served and my page look terrible!到目前为止一切正常,但是当我添加子域时,不提供静态文件,我的页面看起来很糟糕!

If I use localhosts instead, everything works perfect!如果我改用本地主机,则一切正常!

Here I leave my nginx.conf:在这里我留下我的 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;
    }   
}

I also have this in my hosts file:我的主机文件中也有这个:

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

How can I make nginx to work as it does when I type "localhost" in my browser when I type "mydev.test"?当我在浏览器中输入“mydev.test”时,如何让 nginx 像在浏览器中输入“localhost”那样工作? Pretty much all I want to do is serve a bunch of sites within the same physical server using something like virtualhosts in apache.几乎我想要做的就是使用类似 apache 中的虚拟主机之类的东西在同一物理服务器中为一堆站点提供服务。

PD: I'm also using OS Lion in case it helps. PD:我也在使用 OS Lion 以防万一。

That seems you have wrong root setting.看来您的root设置有误。

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

You should check the requested URL for your css files.您应该检查您的 css 文件所请求的 URL。 Assumimg you have STATIC_URL = '/static/' you're browser will probably want to load /static/css/styles.css or something like that.假设你有STATIC_URL = '/static/'你的浏览器可能想要加载/static/css/styles.css或类似的东西。

So, the file ./static/css/styles.css should be found in root directory.因此,文件./static/css/styles.css应该在root目录中找到。

Then right root should be:那么右root应该是:

root /Users/danielrodriguez/workspace/mtt;

Of course that's really not good idea to make root setting to your project root.当然,对您的项目根进行root设置确实不是一个好主意。 So' you may create symlink for static and media to separate dir outside your project.因此,您可以为staticmedia创建符号链接,以将项目外的目录分开。

Hope that helps.希望有帮助。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM