Replacing URI when using dynamic hosts in Nginx reverse proxy

The documentation states that if you use variables in the proxy_pass directive and you specify a URI part, it will be passed upstream "as is".

You will need to capture the part of the URI you need to send upstream. Either use a regular expression location block, for example:

location ~ ^/api(.*)$ {
    set $upstream ...;
    proxy_pass http://$upstream$1;
}

Or use a rewrite...break to change the current URI, for example:

location /api {
    set $upstream ...;
    rewrite ^/api(.*)$ $1 break;
    proxy_pass http://$upstream;
}