nginx實(shí)現(xiàn)請(qǐng)求轉(zhuǎn)發(fā)
反向代理適用于很多場(chǎng)合,負(fù)載均衡是最普遍的用法。
nginx 作為目前最流行的web服務(wù)器之一,可以很方便地實(shí)現(xiàn)反向代理。
nginx 反向代理官方文檔: NGINX REVERSE PROXY
當(dāng)在一臺(tái)主機(jī)上部署了多個(gè)不同的web服務(wù)器,并且需要能在80端口同時(shí)訪問這些web服務(wù)器時(shí),可以使用 nginx 的反向代理功能: 用 nginx 在80端口監(jiān)聽所有請(qǐng)求,并依據(jù)轉(zhuǎn)發(fā)規(guī)則(比較常見的是以 URI 來(lái)轉(zhuǎn)發(fā))轉(zhuǎn)發(fā)到對(duì)應(yīng)的web服務(wù)器上。
例如有 webmail , webcom 以及 webdefault 三個(gè)服務(wù)器分別運(yùn)行在 portmail , portcom , portdefault 端口,要實(shí)現(xiàn)從80端口同時(shí)訪問這三個(gè)web服務(wù)器,則可以在80端口運(yùn)行 nginx, 然后將 /mail 下的請(qǐng)求轉(zhuǎn)發(fā)到 webmail 服務(wù)器, 將 /com下的請(qǐng)求轉(zhuǎn)發(fā)到 webcom 服務(wù)器, 將其他所有請(qǐng)求轉(zhuǎn)發(fā)到 webdefault 服務(wù)器。
假設(shè)服務(wù)器域名為example.com,則對(duì)應(yīng)的 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;
}
}
}
以上的配置會(huì)按以下規(guī)則轉(zhuǎn)發(fā)請(qǐng)求( GET 和 POST 請(qǐng)求都會(huì)轉(zhuǎn)發(fā)):
將 http://example.com/mail/ 下的請(qǐng)求轉(zhuǎn)發(fā)到 http://example.com:portmail/
將 http://example.com/com/ 下的請(qǐng)求轉(zhuǎn)發(fā)到 http://example.com:portcom/main/
將其它所有請(qǐng)求轉(zhuǎn)發(fā)到 http://example.com:portdefault/
需要注意的是,在以上的配置中,webdefault 的代理服務(wù)器設(shè)置是沒有指定URI的,而 webmail 和 webcom 的代理服務(wù)器設(shè)置是指定了URI的(分別為 / 和 /main/)。
如果代理服務(wù)器地址中是帶有URI的,此URI會(huì)替換掉 location 所匹配的URI部分。
而如果代理服務(wù)器地址中是不帶有URI的,則會(huì)用完整的請(qǐng)求URL來(lái)轉(zhuǎn)發(fā)到代理服務(wù)器。
官方文檔描述:
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).
以上配置的轉(zhuǎn)發(fā)示例:
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
相關(guān)文章
filebeat同時(shí)收集錯(cuò)誤日志與普通日志并存詳解
這篇文章主要為大家介紹了filebeat同時(shí)收集錯(cuò)誤日志與普通日志并存詳解,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進(jìn)步,早日升職加薪2022-08-08
在Nginx服務(wù)器上配置Google反向代理的基本方法
這篇文章主要介紹了在Nginx服務(wù)器上配置Google反向代理的基本方法,文中使用到了SSL來(lái)加密反向代理,需要的朋友可以參考下2015-12-12
Nginx的偽靜態(tài)配置中使用rewrite來(lái)實(shí)現(xiàn)自動(dòng)補(bǔ)全的實(shí)例
這篇文章主要介紹了Nginx的偽靜態(tài)配置中使用rewrite來(lái)實(shí)現(xiàn)自動(dòng)補(bǔ)全的實(shí)例,文中對(duì)rewrite的相關(guān)參數(shù)和正則表達(dá)使用也做了介紹,需要的朋友可以參考下2015-12-12
Nginx反向代理一個(gè)80端口下配置多個(gè)微信項(xiàng)目詳解
這篇文章主要介紹了Nginx反向代理一個(gè)80端口下配置多個(gè)微信項(xiàng)目詳解的相關(guān)資料,需要的朋友可以參考下2017-02-02
一些優(yōu)化Nginx服務(wù)器的技巧簡(jiǎn)介
這篇文章主要介紹了一些優(yōu)化Nginx服務(wù)器的技巧簡(jiǎn)介,包括對(duì)HTTP模塊和Events模塊的配置建議,需要的朋友可以參考下2015-06-06

