首頁  >  文章  >  運維  >  nginx如何設定upstream反向代理

nginx如何設定upstream反向代理

PHPz
PHPz轉載
2023-05-21 11:46:061740瀏覽

nginx配置upstream反向代理

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

}

稍不注意可能會落入一個proxy_pass加槓不加槓的陷阱,這裡詳細說下proxy_pass http://tomcats與proxy_pass http://tomcats/的差別:

雖然只是一個/的差別但結果確實是千差萬別。分為以下兩種情況:

1.  目標位址中不帶uri(proxy_pass http://tomcats)。此時新的目標url中,配對的uri部分不做修改,原來是什麼就是什麼。

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.  目標位址中帶uri(proxy_pass http://tomcats/,/也是uri),此時新的目標url中,符合的uri部分將會被修改為該參數中的uri 。

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

以上是nginx如何設定upstream反向代理的詳細內容。更多資訊請關注PHP中文網其他相關文章!

陳述:
本文轉載於:yisu.com。如有侵權,請聯絡admin@php.cn刪除