1. 程式人生 > >當“伺服器上部署多個Web應用”,使用Nginx反向代理配置

當“伺服器上部署多個Web應用”,使用Nginx反向代理配置

當同一伺服器上部署了多個不同的web應用時,可以使用Nginx進行管理配置。

舉個例子:假如 www.aabbccdd.com 站點有好幾個web  App(web應用): finance(金融)、product(產品)、admin(使用者中心)。

訪問這些應用的方式通過上下文(context)來進行區分:

www.aabbccdd.com/finance/

www.aabbccdd.com/product/

www.aabbccdd.com/admin/

我們知道,http的預設埠號是80,如果在一臺伺服器上同時啟動這3個 webapp 應用,都用80埠,肯定是不成的。所以,這三個應用需要分別繫結不同的埠號,假設繫結的埠為18080、28080、38080。

那麼,問題來了,使用者在實際訪問 www.helloworld.com 站點時,訪問不同 web app,總不會還帶著對應的埠號去訪問吧。所以,為了解決這一問題,需要再次用到反向代理來做處理。

Nginx具體配置如下:

#執行使用者
#user somebody;

#啟動程序,通常設定成和cpu的數量相等
worker_processes  1;

#全域性錯誤日誌
error_log  C:/Users/wangcw/Desktop/nginx-1.13.12/logs/error.log;
error_log  C:/Users/wangcw/Desktop/nginx-1.13.12/logs/notice.log  notice;
error_log  C:/Users/wangcw/Desktop/nginx-1.13.12/logs/info.log  info;

#PID檔案,記錄當前啟動的nginx的程序ID
pid        C:/Users/wangcw/Desktop/nginx-1.13.12/logs/nginx.pid;

#工作模式及連線數上限
events {
    worker_connections 1024;    #單個後臺worker process程序的最大併發連結數
}


http {
     #設定mime型別,型別由mime.type檔案定義
    include       C:/Users/wangcw/Desktop/nginx-1.13.12/conf/mime.types;
    default_type  application/octet-stream;
    
	#設定日誌
    log_format  main  '[$remote_addr] - [$remote_user] [$time_local] "$request" '
                      '$status $body_bytes_sent "$http_referer" '
                      '"$http_user_agent" "$http_x_forwarded_for"';
                      
    access_log    C:/Users/wangcw/Desktop/nginx-1.13.12/logs/access.log main;
    rewrite_log     on;

	#設定訪問的web應用列表
    upstream product_server{
        server www.aabbccdd.com:18080;
    }
    upstream admin_server{
        server www.aabbccdd.com:28080;
    }
    upstream finance_server{
        server www.aabbccdd.com:38080;
    }

   #HTTP伺服器
   server {
        #監聽80埠,80埠是知名埠號,用於HTTP協議
        listen       80;
        
        #定義使用www.xx.com訪問
        server_name  www.aabbccdd.com;
        
        #首頁
        index index.jsp
        
        #指向webapp的目錄
        root C:/XMCARES_X/WorkSpace/nginx/src/main/webapp;
        
        #編碼格式
        charset utf-8;
        
        #代理配置引數
        proxy_connect_timeout 180;
        proxy_send_timeout 180;
        proxy_read_timeout 180;
        proxy_set_header Host $host;
        proxy_set_header X-Forwarder-For $remote_addr;
        
       

        #預設指向product的server
        location / {
            proxy_pass http://product_server;
        }

		#使用location對不同請求做相應處理
        location /product/{
            proxy_pass http://product_server;
        }

        location /admin/ {
            proxy_pass http://admin_server;
        }
        
        location /finance/ {
            proxy_pass http://finance_server;
        }
    }
}

現在對如上配置進行測試:

1、先啟動3個不同的web app,並測試訪問。

-----------------------------------------------------------------------------

-----------------------------------------------------------------------------

2.在 C:\Windows\System32\drivers\etc 目錄下的hosts檔案中新增一條 DNS 記錄、

再根據配置檔案啟動Nginx:

    hosts檔案:

127.0.0.1       www.aabbccdd.com  

    啟動Nginx:

   nginx.exe -c conf/nginx_3.conf

3、使用定義的域名進行訪問各個應用(不需要帶埠了大笑

--------------------------------------------------------------------------

--------------------------------------------------------------------------