国产探花免费观看_亚洲丰满少妇自慰呻吟_97日韩有码在线_资源在线日韩欧美_一区二区精品毛片,辰东完美世界有声小说,欢乐颂第一季,yy玄幻小说排行榜完本

首頁 > 網站 > Nginx > 正文

nginx實現請求轉發

2024-08-30 12:22:29
字體:
來源:轉載
供稿:網友
本文給大家分享的是使用nginx實現代理(請求轉發)的教程及簡單示例,非常實用,有需要的小伙伴可以參考下

反向代理適用于很多場合,負載均衡是最普遍的用法。

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

nginx 反向代理官方文檔: NGINX REVERSE PROXY

當在一臺主機上部署了多個不同的web服務器,并且需要能在80端口同時訪問這些web服務器時,可以使用 nginx 的反向代理功能: 用 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 的代理服務器設置是沒有指定URI的,而 webmail 和 webcom 的代理服務器設置是指定了URI的(分別為 / 和 /main/)。

如果代理服務器地址中是帶有URI的,此URI會替換掉 location 所匹配的URI部分。

而如果代理服務器地址中是不帶有URI的,則會用完整的請求URL來轉發到代理服務器。

官方文檔描述:

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.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

發表評論 共有條評論
用戶名: 密碼:
驗證碼: 匿名發表
主站蜘蛛池模板: 盱眙县| 兴义市| 鹤庆县| 竹北市| 吉隆县| 丹棱县| 静安区| 明光市| 东山县| 理塘县| 平利县| 金溪县| 女性| 漳州市| 眉山市| 景德镇市| 醴陵市| 水城县| 鄂托克旗| 鄂温| 银川市| 南城县| 梓潼县| 九龙城区| 宁海县| 修水县| 昭平县| 石嘴山市| 阳高县| 大姚县| 鄂托克前旗| 温宿县| 盐池县| 栾城县| 南汇区| 时尚| 巴青县| 泰和县| 枝江市| 大冶市| 启东市|