nginx 反向代理和正则表达式位置

nginx 反向代理和正则表达式位置

这是我想要的:

uri 类似:http://mydomain.xxx:33380/client001应在 VPN 中重定向到http://10.8.51.21:3780/ui。 uri 类似:http://mydomain.xxx:33380/client001/adm应在 VPN 中重定向到http://10.8.51.21:3780

我的配置在这里:

map $clientname $back_srv {
 "client000" "10.8.50.1";
 "client001" "10.8.51.21";
 "client002" "10.8.51.25";
 default   "mydomain.xxx";
}

server {
        listen 33380;

        location ~ "^/(client[0-9]{3})|/(adm)$" {

                set $clientname $1;
                set $adm $2;
                set $svc_pfix "/ui";

                if ($adm = "adm") {
                        set $svc_pfix "";
                }

                set $svc_port "3780";

                rewrite ^(.*)$ $svc_pfix break;
                proxy_pass http://$back_srv:$svc_port;
                proxy_http_version 1.1;
                proxy_set_header Upgrade $http_upgrade;
                proxy_set_header Connection "upgrade";

        }
}

当我转到http://mydomain.xxx:33380/client001nginx 时,重定向到http://mydomain.xxx:33380/ui并显示 404 错误。看起来 proxy_pass 不起作用。

我测试了没有正则表达式和映射的其他简单配置,它可以正常工作:

server {

        listen 888;

        location /svc_loc {
                rewrite ^/svc_loc /(.*) /$1  break;
                proxy_pass http://10.8.51.21:3780;
                proxy_http_version 1.1;
                proxy_set_header Upgrade $http_upgrade;
                proxy_set_header Connection "upgrade";
        }
}

有任何想法吗?

答案1

想尝试一下吗?

map $request_uri $back_srv {
 "/client000" "10.8.50.1";
 "/client001" "10.8.51.21";
 "/client002" "10.8.51.25";
 default   "mydomain.xxx";
}

server {
        listen 33380;

        location ~ "/adm$" {

                set $svc_port "3780";

                proxy_pass http://$back_srv:$svc_port/;
                proxy_http_version 1.1;
                proxy_set_header Upgrade $http_upgrade;
                proxy_set_header Connection "upgrade";
        }

        location ~ "^/(client[0-9]{3})" {
                set $svc_port "3780";

                proxy_pass http://$back_srv:$svc_port/ui;
                proxy_http_version 1.1;
                proxy_set_header Upgrade $http_upgrade;
                proxy_set_header Connection "upgrade";
        }

}

相关内容