我正在尝试使用基于URL的Nginx进行反向代理。我希望将http://mydomain.example.com/client1/...
重定向到http://127.0.0.1:8193/...
。我尝试了很多方法,但没有一个方法奏效。请注意,该应用程序可以进行重定向。这些是我上一个解决方案的配置文件:
server {
listen 80;
server_name mydomain.example.com;
location / {
set $instance none;
if ($request_uri ~ ^/(.*)/$) {
set $instance $1;
}
set $no_cookie true;
if ($http_cookie ~ "instance=([^;] +)(?:;|$)") {
set $instance $1;
set $no_cookie false;
}
if ($no_cookie = true) {
add_header Set-Cookie "instance=$instance;Domain=$host;Path=/";
rewrite ^ / break;
}
include instances.conf;
}
proxy_redirect off;
proxy_set_header Host $host;
proxy_set_header X-Real-IP $remote_addr;
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
proxy_connect_timeout 90;
proxy_send_timeout 60;
# Installation of language packs, etc. can take a long time
proxy_read_timeout 10800;
if ($instance = client1) {
proxy_pass http://127.0.0.1:8193;
}
if ($instance = client2) {
proxy_pass http://127.0.0.1:8194
}
...
当浏览器请求http://mydomain.example.com/client1/
时,Nginx应设置名为instance
的cookie,其值为client1
,然后将流量重定向到相应的代理。对于后续查询,它应该使用此cookie进行重定向。我遇到的问题是它从未将$instance
变量设置为client1
。不要忘记应用程序不知道前缀/client1
。
答案 0 :(得分:1)
用于获取cookie的正则表达式是错误的。我已将此更改为
"instance=([^;][^ ]+)(?:;|$)"
现在有效。
编辑:它最终只是解决方案的一部分。对不起。还有一个问题。请参阅下面的评论。
答案 1 :(得分:0)
它与您的问题无关,但“proxy_connect_timeout”
“此指令为上游服务器的连接分配超时。请记住,此时间不能超过75秒。”
答案 2 :(得分:-1)
请参阅Nginx'map module
map $uri $proxy {
/client1 http://127.0.0.1:8193/client1;
/client2 http://127.0.0.1:8194/client2;
}
server {
server_name my.domain.com;
proxy_pass $proxy;
}
请注意,将/ clientX附加到proxy_pass URI的末尾会从请求中删除该部分URI(这对我来说似乎是合理的,但可能不是您想要的)。