【问题标题】:Nginx conf for two gunicorn applications (django and tilestache)用于两个 gunicorn 应用程序(django 和 tilestache)的 Nginx conf
【发布时间】:2025-12-31 18:00:01
【问题描述】:

我正在尝试托管一个由 django 应用程序和由 tilestache 提供的地图图块组成的网站。我可以使用任一方法让它们分别运行和提供内容

gunicorn_django -b 0.0.0.0:8000 

对于 django 应用程序,或

gunicorn "TileStache:WSGITileServer('tilestache.cfg')"

用于tilestache。我已经尝试守护 django 应用程序并在不同端口 (8080) 上使用 tilestache 进程同时运行它们,但 tilestache 不起作用。我认为问题在于我的 nginx 配置:

server {
    listen   80;
    server_name localhost;

    access_log /opt/django/logs/nginx/vc_access.log;
    error_log  /opt/django/logs/nginx/vc_error.log;

    # no security problem here, since / is alway passed to upstream
    root /opt/django/;
    # serve directly - analogous for static/staticfiles
    location /media/ {
        # if asset versioning is used
        if ($query_string) {
            expires max;
        }
    }
    location /static/ {
        # if asset versioning is used
        if ($query_string) {
            expires max;
        }
    }
    location / {
        proxy_pass_header Server;
        proxy_set_header Host $http_host;
        proxy_redirect off;
        proxy_set_header X-Real-IP $remote_addr;
        proxy_set_header X-Scheme $scheme;
        proxy_connect_timeout 10;
        proxy_read_timeout 10;
        proxy_pass http://localhost:8000/;
    }
    # what to serve if upstream is not available or crashes
    error_page 500 502 503 504 /media/50x.html;
}

我可以在proxy_pass http://localhost:8080/ 的conf 中添加另一个server 块吗?此外,我对这个堆栈非常陌生(我非常依赖Adrián Deccico 的教程here 来启动并运行 django 部分)所以任何“哇,这是一个明显的错误”或建议将不胜感激.

【问题讨论】:

  • 您将如何区分这两个应用程序 - 它们使用不同的域 - 例如 www.mydjangoapp.com 和 www.mytilestache.com?或者他们共享相同的域名,但使用不同的 /paths/ ?
  • @Tisho 相同的域,不同的路径。我已经看到很多地图示例,它们将切片服务器放在子域上,并且会对此开放。

标签: django nginx gis gunicorn


【解决方案1】:

据我所知-您已将location / 映射到localhost:8000。当您有 2 个不同的上游时,您将需要两个不同的位置映射,每个上游一个。 因此,假设 django 应用程序是您域中的主站点,您将拥有现在的默认位置:

location / {
    proxy_pass_header Server;
    proxy_set_header Host $http_host;
    proxy_redirect off;
    proxy_set_header X-Real-IP $remote_addr;
    proxy_set_header X-Scheme $scheme;
    proxy_connect_timeout 10;
    proxy_read_timeout 10;
    proxy_pass http://localhost:8000/;
}

然后为其他应用添加另一个位置:

location /tilestache {
    proxy_pass_header Server;
    proxy_set_header Host $http_host;
    proxy_redirect off;
    proxy_set_header X-Real-IP $remote_addr;
    proxy_set_header X-Scheme $scheme;
    proxy_connect_timeout 10;
    proxy_read_timeout 10;
    proxy_pass http://localhost:8080/;
}

这里唯一的区别是端口。这样 domain.com/tilestache 将由localhost:8080 处理,而所有其他地址将默认为localhost:8000 的django 应用程序。 将location /tilstache 放在location / 之前。

为清楚起见,您可以像这样定义上游:

upstream django_backend  {
  server localhost:8000;
}

upstream tilestache_backend  {
  server localhost:8080;
}

然后在location 部分,使用:

location / {
    .....
    proxy_pass  http://django_backend;
}

location /tilestache {
    .....
    proxy_pass  http://tilestache_backend;
}

【讨论】:

  • 非常感谢。经过大量搜索,这为我解决了