skip to Main Content

I currently have the following configuration to proxy requests off a single domain to multiple backends:

    server {
    listen 80;
      location /team/app1/location/region/ {
        proxy_pass https://this.is.the.backend.app.example/path1/healthcheck;
    }
      location /team/app2/location/region/ {
        proxy_pass https://this.is.the.backend.app.example/path2/healthcheck;
    }
      location /team/app3/location/region/ {
        proxy_pass https://this.is.the.backend.app.example/path3/healthcheck;
    }
}

The paths are pretty arbitrary, essentially I just want to be able to proxy from:

https://proxydomain.com/team/app1/location1/region 

To:

https://this.is.the.backend.app.example/path3/healthcheck

So

/team/app1/location1/region

Would need to be stripped from the request and just proxy the request to the intended backend. I assume the path is being appended in someway as I just get 404s…

I can pass to a domain without trailing path like so – but when I try and proxy to a domain with trailing paths it gets complicated:

    server {
    listen 80;
    location /one {
        proxy_pass http://x.x.x.x/;
    }
    location /two {
        proxy_pass http://x.x.x.x/;
    }
}

I have tried the following configuration too – to try and rewrite the url:

    rewrite ^/team/app3/location/region/(.*)$ $1 break;

Hopefully it makes sense what I am trying to achieve – any guidance would be greatly appreciated.

Thanks

2

Answers


  1. Chosen as BEST ANSWER

    Thanks for that - definitely helped getting me down the correct track:

    In the end the working config was:

        location /team/app3/location/region {
        rewrite ^/team/app3/location/region(.*) /path3/healthcheck$1 break;
        proxy_pass https://this.is.the.backend.app.example;
    }
    

    Which correctly proxied through to:

    https://this.is.the.backend.app.example/path3/healthcheck
    

  2. You were on the right track. The rewrite will look like:

    location /team/app3/location/region/ {
      rewrite ^/team/app1/location/region/(.*)$ /path3/$1 break;
      proxy_pass https://this.is.the.backend.app.example/;
    }
    

    You should add the new path path3 in the rewrite rule. With this NGINX will rewrite the $uri and append it to the proxy_pass. In case app3 and path3 are a fixed pattern you can tune the location block as well as the rewrite to simplify your configuration. But I would generally start with the approach mentioned above.

    AND we keep in mind. Regex matching in locations will consume CPU. So sometimes it is better to have them fixed / static. Especially if you are using them in health checks and query them every 5s or so.

    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search