How to configure nginx upstream reverse proxy

PHPz
Release: 2023-05-21 11:46:06
forward
1739 people have browsed it

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;
  }
 }

}
Copy after login

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
Copy after login

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
Copy after login

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!

Related labels:
source:yisu.com
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template
About us Disclaimer Sitemap
php.cn:Public welfare online PHP training,Help PHP learners grow quickly!