Nginx:重写一系列位置

Nginx:重写一系列位置,nginx,url-rewriting,nginx-reverse-proxy,Nginx,Url Rewriting,Nginx Reverse Proxy,我正试图用Nginx为多个代理重写多个位置(使用不同的ip) 我试过这个: location ~/target/(?<ip>\d+\.\d+\.\d+\.\d+)/ { proxy_http_version 1.1; proxy_set_header Upgrade $http_upgrade; proxy_set_header Connection "upgrade"; proxy_pass https://$ip; r

我正试图用Nginx为多个代理重写多个位置(使用不同的ip)

我试过这个:

location ~/target/(?<ip>\d+\.\d+\.\d+\.\d+)/ {
    proxy_http_version 1.1;
    proxy_set_header Upgrade $http_upgrade;
    proxy_set_header Connection "upgrade";
    proxy_pass  https://$ip;
    rewrite ^/target_ip/$ip/(.*) /$1 break;
    proxy_intercept_errors off;
    proxy_buffering off;
}
location~/target/(?\d+\.\d+\.\d+\.\d+){
proxy_http_版本1.1;
代理设置头升级$http\U升级;
代理设置头连接“升级”;
代理通过https://$ip;
重写^/target_ip/$ip/(.*)/$1中断;
代理截获错误关闭;
代理缓冲关闭;
}
但不幸的是,它没有删除/target/ip:

在此服务器上找不到请求的URL/target/192.168.10.3/cgi-bin/luci

这种方法是有效的:

location ~/target/(?<ip>\d+\.\d+\.\d+\.\d+)/ {
    proxy_http_version 1.1;
    proxy_set_header Upgrade $http_upgrade;
    proxy_set_header Connection "upgrade";
    proxy_pass  https://$ip;
    if ($ip = "192.168.10.3") { 
        rewrite ^/target_ip/192.168.10.3/(.*) /$1 break;
    }
    proxy_intercept_errors off;
    proxy_buffering off;
}
location~/target/(?\d+\.\d+\.\d+\.\d+){
proxy_http_版本1.1;
代理设置头升级$http\U升级;
代理设置头连接“升级”;
代理通过https://$ip;
如果($ip=“192.168.10.3”){
重写^/target_ip/192.168.10.3/(.*)/$1 break;
}
代理截获错误关闭;
代理缓冲关闭;
}
但是,我必须为每个IP指定一个if语句。我需要更紧凑的

你有什么建议吗

谢谢

---------------编辑---------------

感谢Richard Smith,以下是解决方案:

location ~/target/(?<ip>\d+\.\d+\.\d+\.\d+)/ {
proxy_http_version 1.1;
proxy_set_header Upgrade $http_upgrade;
proxy_set_header Connection "upgrade";
proxy_pass  https://$ip;
rewrite ^/target_ip/[^/]+/(.*)$ /$1 break;
proxy_intercept_errors off;
proxy_buffering off;
}
location~/target/(?\d+\.\d+\.\d+\.\d+){
proxy_http_版本1.1;
代理设置头升级$http\U升级;
代理设置头连接“升级”;
代理通过https://$ip;
重写^/target_ip/[^/]+/(.*)$/$1个中断;
代理截获错误关闭;
代理缓冲关闭;
}

使用相同的正则表达式:
重写^/target\u ip/\d+\.\d+\.\d+\.\d+/(.*)$/$1 break-或者更简单地说:
重写^/target_ip/[^/]+/(.*)$/$1中断我没有那样想。它工作得很好,非常感谢!