Home  >  Article  >  Operation and Maintenance  >  How to configure nginx upstream reverse proxy

How to configure nginx upstream reverse proxy

PHPz
PHPzforward
2023-05-21 11:46:061788browse

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 are not careful, you may fall into a trap of adding proxies but not proxies to proxy_pass. Details here Let’s talk about the difference between proxy_pass http://tomcats and proxy_pass http://tomcats/:

Although the difference is only one /, the results are indeed very different. It is divided into the following two situations:

1. The target address does not contain uri (proxy_pass http://tomcats). At this time, in the new target URL, the matching uri part is not modified, and it is what it originally was.

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 uri (proxy_pass http://tomcats/, / is also uri). At this time, in the new target url, the matching uri part will be modified to the uri in the parameter. .

location /ops-coffee/ {
 proxy_pass http://192.168.106.135:8181/;
}

http://domain/ops-coffee/ -->  http://192.168.106.135:8181
http://domain/ops-coffee/action/abc -->  http://192.168.106.135:8181/action/abc

The above is the detailed content of How to configure nginx upstream reverse proxy. For more information, please follow other related articles on the PHP Chinese website!

Statement:
This article is reproduced at:yisu.com. If there is any infringement, please contact admin@php.cn delete