Nginx Load Balance / Proxy to Upstream with Path / Rewrite
5,494
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;
}
}
Related videos on Youtube
Author by
Matt The Ninja
Updated on September 18, 2022Comments
-
Matt The Ninja over 1 year
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!!
-
Matt The Ninja over 8 yearsJust what I was looking for. Only problem I can see is that on the load balancer I wouldn't be able to detect server down for failover?
-
Alexey Ten over 8 yearsYou'll get 50x error. See proxy_next_upstream
-
Atul Agrawal about 4 yearsI am getting 404 on the other server location.
-
Alexey Ten about 4 years@AtulAgrawal ask a new question