nginx反向 Proxy(請求轉寄-URL匹配規則)

來源:互聯網
上載者:User

標籤:pos   blank   div   asc   java   oca   net   規則   example   

反向 Proxy適用於很多場合,負載平衡是最普遍的用法。

nginx 作為目前最流行的web伺服器之一,可以很方便地實現反向 Proxy。

nginx 反向 Proxy官方文檔: NGINX REVERSE PROXY

當在一台主機上部署了多個不同的web伺服器,並且需要能在80連接埠同時訪問這些web伺服器時,可以使用 nginx 的反向 Proxy功能: 用 nginx在80連接埠監聽所有請求,並依據轉寄規則(比較常見的是以 URI 來轉寄)轉寄到對應的web伺服器上。

例如有 webmail , webcom 以及 webdefault 三個伺服器分別運行在 portmail , portcom , portdefault 連接埠,要實現從80連接埠同時訪問這三個web伺服器,則可以在80連接埠運行 nginx, 然後將 /mail 下的請求轉寄到 webmail 伺服器, 將 /com下的請求轉寄到 webcom 伺服器, 將其他所有請求轉寄到 webdefault 伺服器。

假設伺服器網域名稱為example.com,則對應的 nginx http配置如下:

 
http {    server {            server_name example.com;            location /mail/ {                    proxy_pass http://example.com:protmail/;            }            location /com/ {                    proxy_pass http://example.com:portcom/main/;            }            location / {                    proxy_pass http://example.com:portdefault;            }    }}

  

 
  • 以上的配置會按以下規則轉寄請求( GET 和 POST 請求都會轉寄):
  • 將 http://example.com/mail/ 下的請求轉寄到 http://example.com:portmail/
  • 將 http://example.com/com/ 下的請求轉寄到 http://example.com:portcom/main/
  • 將其它所有請求轉寄到 http://example.com:portdefault/

需要注意的是,在以上的配置中,webdefault 的Proxy 伺服器設定是沒有指定URI的,而 webmail 和 webcom 的Proxy 伺服器設定是指定了URI的(分別為 / 和 /main/)。 
如果Proxy 伺服器地址中是帶有URI的,此URI會替換掉 location 所匹配的URI部分。 
而如果Proxy 伺服器地址中是不帶有URI的,則會用完整的請求URL來轉寄到Proxy 伺服器。

官方文檔描述:

If the URI is specified along with the address, it replaces the part of the request URI that matches the location parameter. 
If the address is specified without a URI, or it is not possible to determine the part of URI to be replaced, the full request URI is passed (possibly, modified).

以上配置的轉寄樣本:

    • http://example.com/mail/index.html -> http://example.com:portmail/index.html
    • http://example.com/com/index.html -> http://example.com:portcom/main/index.html
    • http://example.com/mail/static/a.jpg -> http://example.com:portmail/static/a.jpg
    • http://example.com/com/static/b.css -> http://example.com:portcom/main/static/b.css
    • http://example.com/other/index.htm -> http://example.com:portdefault/other/index.htm

轉自:http://blog.csdn.net/tobacco5648/article/details/51099426

nginx反向 Proxy(請求轉寄-URL匹配規則)

相關文章

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.