Nginx系列:配置跳轉的常用方式


隨着應用服務的增多,服務可能部署在不同的服務器上。這些服務有可能存在IP端口Port、請求的ContextPath等一樣的情況,怎么合理的配置他們的跳轉呢?下面介紹三種常見的跳轉方式。

 

0x01:根據不同域名判斷跳轉不同服務

就是根據在nginx.conf配置的server_name與域名或者(或者IP)匹配跳轉不同的服務。

#當客戶端訪問www.domain.com,監聽端口號為80,直接跳轉到data/www目錄下文件
server {
    listen       80;
       server_name  www.domain.com;
       location / {
          root   data/www;
          index  index.html index.htm;
    }
 }
 #當客戶端訪問bbs.domain.com,監聽端口號為80,直接跳轉到data/bbs目錄下文件
server {
     listen       80;
     server_name  bbs.domain.com;
     location / {
         root   data/bbs;
         index  index.html index.htm;
     }
}

 

0x02:根據不同端口判斷跳轉不同服務

就是根據在nginx.conf配置的listen指令匹配跳轉不同的服務。

#當客戶端訪問www.domain.com,監聽端口號為8081,直接跳轉到data/www目錄下文件
server {
      listen       8081;
      server_name  www.domain.com;
      location / {
          root   data/www;
          index  index.html index.htm;
      }
}

#當客戶端訪問www.domain.com,監聽端口號為8082,直接跳轉到data/bbs目錄下文件
server {
      listen       8082;
      server_name  www.domain.com;
      location / {
          root   data/bbs;
          index  index.html index.htm;
      }
}

 

0x03:根據鏈接的ContextPath不同跳轉不同的服務器

主要根據每個應用服務器的ContextPath的普通,匹配跳轉到不同的服務器。

#服務創建監聽的端口號
server {
    #監聽的端口號
    listen       80;
    #服務名稱
    server_name  www.domain.com;
   # 匹配項目名稱為bbs開頭
   location /bbs/ {
       #  配置反向代理
       proxy_pass http://192.168.1.188:8081/;
       index  index.html index.htm;
   }
   # 匹配項目名稱為blog開頭
   location /blog/ {
        # 配置反向代理
        proxy_pass http://192.168.1.188:8082/;
       index  index.html index.htm;
   }
}

 


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM