5

我有一个名为 django-admin 的应用程序myapp,我想在不同的物理盒子上部署多个实例,每个客户一个。但是,我希望它们都可以从类似的域访问,mydomain.com/customer1/myapp.

我已经摆弄了特定的代理设置,并尝试了 SO 上建议的多种方法,但没有一种完全适合我的用例……而且由于我对两者知之甚少nginxdjango我不知所措!

我当前的 nginx.conf 是:

server {
    listen 80;
    server_name myserver.com

    location ^~ /static {
        alias /path/to/static/files/;
    }
#    location / {
#        proxy_pass http://127.0.0.1:8001;
#    }
    location ^~ /customer1/myapp/static {
        alias /path/to/static/files/;
    }
    location /customer1/myapp {
        rewrite ^/customer1/myapp/(/?)(.*) /$2 break;
        proxy_pass http://127.0.0.1:8001;
    }
}

我可以通过myserver.com/customer1/myapp/admin. 但是,当我尝试登录时,nginx 会将我的 url 重写myserver.com/admin为不是有效的 url。如何防止 nginx 实际重写 url 而只更改传递给的 url 127.0.0.1:8001

FWIW,我正在使用 gunicorn 服务gunicorn -b 127.0.0.1:8001 -n myapp。如果我取消注释该/位置并删除最后两个位置块,则该应用程序运行良好。

如果有替代方案,我远未确定这种方法。目标是避免为每个部署修改 django 代码,而只需将最少的代码添加到 nginx.conf 以进行新部署。

4

2 回答 2

17

基本上,您将 url 指定为 proxy_pass 指令的一部分,以下 location 指令应该这样做:

location ~ ^/customer1/myapp(/?)(.*) {
    proxy_pass http://127.0.0.1:8001/$2;
}

有关 nginx 如何传递 uri 的详细说明,请参见http://nginx.org/en/docs/http/ngx_http_proxy_module.html#proxy_pass

于 2012-09-11T10:42:28.463 回答
3

您应该使用以下内容:

location /customer1/myapp {
    return 302 $uri/;
}
location /customer1/myapp/ {
    proxy_pass http://127.0.0.1:8001/
}

Note that this is superior to using variables within proxy_pass, because if you do use variables, then proxy_redirect can no longer be of the default value default, and will be off instead, and then internal 302 redirects within your app will not be mapped to /customer1/myapp/ after being fetched by nginx, which will likely cause you troubles and more 404s.

And, yes, using individual domains for individual customers is a better idea, because it's more secure (as far as cookie handling is concerned, for example).

于 2013-12-21T18:46:41.540 回答