使用 nginx proxy_pass 和重写的多个 django 应用程序

2024-03-25

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

我摆弄了特定的代理设置,并尝试了多种建议的东西,但没有一个完全适合我的用例......而且因为我对两者都知之甚少nginx and django我很茫然!

我当前的 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这不是一个有效的网址。如何防止 nginx 实际重写 url 并仅更改传递到的 url127.0.0.1:8001?

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

如果有其他选择的话,我还远未确定这种方法。目标是避免修改每个部署的 django 代码,而只需向新部署的 nginx.conf 添加最少的代码。


基本上,您将 url 指定为 proxy_pass 指令的一部分,以下 location 指令应该执行此操作:

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

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

本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)

使用 nginx proxy_pass 和重写的多个 django 应用程序 的相关文章

随机推荐