日韩精品成人无码专区免费-国产99久久久久久免费看-国产精品丝袜久久久久久不卡-国产精品无码一区二区三区

nginx實現(xiàn)請求轉(zhuǎn)發(fā)

發(fā)布時間:2024-06-16 點擊:166
反向代理適用于很多場合,負載均衡是最普遍的用法。
nginx 作為目前最流行的web服務(wù)器之一,可以很方便地實現(xiàn)反向代理。
nginx 反向代理官方文檔: nginx reverse proxy
當在一臺主機上部署了多個不同的web服務(wù)器,并且需要能在80端口同時訪問這些web服務(wù)器時,可以使用 nginx 的反向代理功能: 用 nginx 在80端口監(jiān)聽所有請求,并依據(jù)轉(zhuǎn)發(fā)規(guī)則(比較常見的是以 uri 來轉(zhuǎn)發(fā))轉(zhuǎn)發(fā)到對應(yīng)的web服務(wù)器上。
例如有 webmail , webcom 以及 webdefault 三個服務(wù)器分別運行在 portmail , portcom , portdefault 端口,要實現(xiàn)從80端口同時訪問這三個web服務(wù)器,則可以在80端口運行 nginx, 然后將 /mail 下的請求轉(zhuǎn)發(fā)到 webmail 服務(wù)器, 將 /com下的請求轉(zhuǎn)發(fā)到 webcom 服務(wù)器, 將其他所有請求轉(zhuǎn)發(fā)到 webdefault 服務(wù)器。
假設(shè)服務(wù)器域名為example.com,則對應(yīng)的 nginx http配置如下:
123456789101112131415161718http {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;}}}
以上的配置會按以下規(guī)則轉(zhuǎn)發(fā)請求( get 和 post 請求都會轉(zhuǎn)發(fā)):
將 http://example.com/mail/ 下的請求轉(zhuǎn)發(fā)到 http://example.com:portmail/將 http://example.com/com/ 下的請求轉(zhuǎn)發(fā)到 http://example.com:portcom/main/將其它所有請求轉(zhuǎn)發(fā)到 http://example.com:portdefault/
需要注意的是,在以上的配置中,webdefault 的代理服務(wù)器設(shè)置是沒有指定uri的,而 webmail 和 webcom 的代理服務(wù)器設(shè)置是指定了uri的(分別為 / 和 /main/)。
如果代理服務(wù)器地址中是帶有uri的,此uri會替換掉 location 所匹配的uri部分。
而如果代理服務(wù)器地址中是不帶有uri的,則會用完整的請求url來轉(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ā)示例:
12345http://example.com/mail/index.html -> http://example.com:portmail/index.htmlhttp://example.com/com/index.html -> http://example.com:portcom/main/index.htmlhttp://example.com/mail/static/a.jpg -> http://example.com:portmail/static/a.jpghttp://example.com/com/static/b.css -> http://example.com:portcom/main/static/b.csshttp://example.com/other/index.htm -> http://example.com:portdefault/other/index.htm

云服務(wù)器掛lol
郵箱提示掛馬-云服務(wù)器問題
云服務(wù)器配置網(wǎng)站卡慢怎么解決
阿里云服務(wù)器配置ip
國內(nèi)哪家云服務(wù)器租用最好
如何在百度云服務(wù)器上上傳文件
備案被退回我不明白所退回的意思
vps跟云服務(wù)器的區(qū)別