如何在Nginx的proxy_redirect最终URL中保留子域和基本路径

问题描述 投票:1回答:1

基本上我在中正确设置proxy_redirect有问题。基本上我想像这样执行代理重定向:

http://subdomain.domain.com/test1/test2/test3 -> http://subdomain.another.com/test1/test2/test3

这里subdomain和url路径(即/ test1 / test2 / test3)不断变化,所以在这里我必须从重定向url中获取它们并将其传递给最终url。

我是这样想的:

proxy_redirect ~^(http://[^\.]+)\.domain\.com/(.+)$ http://$1.another.com/$2; 

请提供任何解决方案来执行此操作。

redirect nginx reverse-proxy
1个回答
0
投票

使用正则表达式进行重写将在更改域时保留路径:

server {
    listen              80;
    server_name         subdomain.domain.com;

    location / {
        rewrite  ^/(.*)$  subdomain.another.com/$1;
    }
}
© www.soinside.com 2019 - 2024. All rights reserved.