使用Nginx實現反向代理 - 不同的子域名映射到不同的后台地址


1、配置IP域名

C:\Windows\System32\drivers\etc\hosts中加入

127.0.0.1 8081.max.com

127.0.0.1 8082.max.com

2、配置nginx.

  1. upstream tomcatserver1 {  
  2.     server 127.0.0.1:8081;  
  3.     }  
  4. upstream tomcatserver2 {  
  5.     server 127.0.0.1:8082;  
  6.     }  
  7. server {  
  8.         listen       80;  
  9.         server_name  8081.max.com;  
  10.   
  11.         #charset koi8-r;  
  12.   
  13.         #access_log  logs/host.access.log  main;  
  14.   
  15.         location / {  
  16.             proxy_pass   http://tomcatserver1;  
  17.             index  index.html index.htm;  
  18.         }       
  19.     }  
  20. server {  
  21.         listen       80;  
  22.         server_name  8082.max.com;  
  23.   
  24.         #charset koi8-r;  
  25.   
  26.         #access_log  logs/host.access.log  main;  
  27.   
  28.         location / {  
  29.             proxy_pass   http://tomcatserver2;  
  30.             index  index.html index.htm;  
  31.         }          
  32.     }  

流程:

1)瀏覽器訪問8081.max.com,通過本地host文件域名解析,找到本機的服務器(安裝nginx)

2)nginx反向代理接受客戶機請求,找到server_name為8081.max.com的server節點,根據proxy_pass對應的http路徑,將請求轉發到upstream tomcatserver1上,即端口號為8081的tomcat服務器。

 

4、效果展示

 請求8081.max.com,tomcat1接收返回首頁

請求8082.max.com,tomcat2接收返回首頁

 

 

nginx代理地址指向修改
/etc/nginx/conf.d/default.conf

前端項目存放的地址
/usr/share/nginx/html/

常用操作

nginx -s stop       快速關閉Nginx,可能不保存相關信息,並迅速終止web服務。
    nginx -s quit       平穩關閉Nginx,保存相關信息,有安排的結束web服務。
    nginx -s reload     因改變了Nginx相關配置,需要重新加載配置而重載。
    nginx -s reopen     重新打開日志文件。
    nginx -c filename   為 Nginx 指定一個配置文件,來代替缺省的。
    nginx -t            不運行,而僅僅測試配置文件。nginx 將檢查配置文件的語法的正確性,並嘗試打開配置文件中所引用到的文件。
    nginx -v            顯示 nginx 的版本。

    nginx -V            顯示 nginx 的版本,編譯器版本和配置參數。

 


免責聲明!

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



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