Nginx Load Balance / Proxy to Upstream with Path / Rewrite

Basically I have a API farm as

192.168.1.1, 192.168.1.2, 192.168.1.3

However the endpoints have different folder structure (3rd parties which i can't amend!)

So basically I want to have a nginx config something like this

upsteam api_servers{
    server 192.168.1.1/api/;
    server 192.168.1.2/myApp/api/;
    server 192.168.1.3/;
}
server{
    listen 80;
    location / {
        proxy_pass http://api_servers;
    }
}

However you can't have path in the upstream servers. I assume I have to rewrite these? I know how to do that for the incoming request to rewrite it, but not sure how I can rewrite it differently when it goes to each backend server.

Many thanks in advance!!


There is a workaround for this. Idea is to make a proxy server block for each upstream.

upsteam api_servers {
    server 127.0.1.1;
    server 127.0.1.2;
    server 192.168.1.3;
}

server {
    listen 127.0.1.1;
    location / {
        proxy_pass http://192.168.1.1/api/;
    }
}

server {
    listen 127.0.1.2;
    location / {
        proxy_pass http://192.168.1.2/myApp/api/;
    }
}

server {
    listen 80;
    location / {
        proxy_pass http://api_servers;
    }
}