0

here is what I want:

uri like: http://mydomain.xxx:33380/client001 should be redirected to http://10.8.51.21:3780/ui in VPN. uri like: http://mydomain.xxx:33380/client001/adm should be redirected to http://10.8.51.21:3780 in VPN.

My config here:

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";

        }
}

When I go to http://mydomain.xxx:33380/client001 nginx redirects to http://mydomain.xxx:33380/ui and show 404 error. Look like proxy_pass doesn't works.

I tested other simple config without regexp and map, it works properly:

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";
        }
}

Any ideas?

  • I believe your simple config contains a syntax error "rewrite ^/svc_loc /(.*) /$1 break;" There's an extenxive chapter on why one should avoid "if". – Gerard H. Pille Nov 18 '20 at 14:07
  • You cannot use `if` in a `location` like that. See [this document](https://www.nginx.com/resources/wiki/start/topics/depth/ifisevil/). – Richard Smith Nov 18 '20 at 14:16

1 Answers1

0

Care to give this a try?

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";
        }

}

Gerard H. Pille
  • 2,569
  • 1
  • 13
  • 11