【发布时间】:2012-06-22 01:33:40
【问题描述】:
我正在尝试使用基于 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;
}
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。
你有什么想法吗?你知道更好的解决方案吗?
【问题讨论】:
-
我认为问题在于用于获取 cookie 值的正则表达式。我不是正则表达式的专家。我从 Nginx 的文档文件中获取了这个。有人可以验证吗?
标签: configuration nginx reverse-proxy