How to configure upstream reverse proxy in nginx

10-25-2023

Nginx configures upstream reverse proxy.

http { ... upstream tomcats { server 192.168.106.176 weight=1; server 192.168.106.177 weight=1; } server { location /ops-coffee/ { proxy_pass http://tomcats; proxy_set_header Host $host; proxy_set_header X-Real-IP $remote_addr; proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for; proxy_set_header X-Forwarded-Proto $scheme; } }}

If you don't pay attention to it, you may fall into a trap of proxy_pass adding leverage but not adding leverage. Here's the difference between proxy_pass http://tomcats and proxy_pass http://tomcats/ in detail:

Although it's only a/difference, the result is very different. Divided into the following two situations:

1. there is no uri(proxy_pass http://tomcats) in the destination address. At this time, in the new target url, the matching uri part is not modified, and it is what it used to be.

location /ops-coffee/ { proxy_pass http://192.168.106.135:8181; }http://domain/ops-coffee/ --> http://192.168.106.135:8181/ops-coffee/http://domain/ops-coffee/action/abc --> http://192.168.106.135:8181/ops-coffee/action/abc

2. The target address contains a uri(proxy_pass http://tomcats/,/is also a uri). At this time, the matching uri part in the new target url will be modified to the uri in this parameter.

location /ops-coffee/ { proxy_pass http://192.168.106.135:8181/; }http://domain/ops-coffee/ --> http://192.168.106.135:8181http://domain/ops-coffee/action/abc --> http://192.168.106.135:8181/action/abc

Copyright Description:No reproduction without permission。

Knowledge sharing community for developers。

Let more developers benefit from it。

Help developers share knowledge through the Internet。

Follow us