1. 程式人生 > >linux下Nginx反向代理多個tomcat(單獨訪問或叢集配置)

linux下Nginx反向代理多個tomcat(單獨訪問或叢集配置)

第一步需要在你的伺服器上安裝Nginx,請檢視此文章:https://blog.csdn.net/u013641234/article/details/73838472

安裝完成以後,啟動Nginx,看看是否能夠正常訪問,

然後開始配置Nginx反向代理 : 同一臺伺服器或多臺伺服器的多個tomcat,每次轉發單獨訪問某個tomcat配置:

1.找到nginx配置檔案路徑: /usr/local/nginx/conf/nginx.conf

命令 : # vi /usr/local/nginx/conf/nginx.conf

在這段紅色程式碼下加  如下配置:

    server {
        listen       80;
        server_name  localhost;


        #charset koi8-r;


        #access_log  logs/host.access.log  main;


        location / {
            root   html;
            index  index.html index.htm;

        }

#tomcat server one
location /server1{
    proxy_pass http://47.52.28.116:7070/TestNginx;
    proxy_set_header           Host $host;
            proxy_set_header  X-Real-IP  $remote_addr;
    proxy_set_header           X-Forwarded-For $proxy_add_x_forwarded_for;
            client_max_body_size  100m;
            root   html;
            index  index.html index.htm;
}




#tomcat server two
location /server2{
    proxy_pass http://47.52.28.116:7071/TestNginx;
    proxy_set_header           Host $host;
            proxy_set_header  X-Real-IP  $remote_addr;
    proxy_set_header           X-Forwarded-For $proxy_add_x_forwarded_for;
            client_max_body_size  100m;
            root   html;
            index  index.html index.htm;
}


#tomcat server three
location /server3{
    proxy_pass http://47.52.28.116:7072/Test2;
    proxy_set_header           Host $host;
            proxy_set_header  X-Real-IP  $remote_addr;
    proxy_set_header           X-Forwarded-For $proxy_add_x_forwarded_for;
            client_max_body_size  100m;
            root   html;
            index  index.html index.htm;

}

這是本臺伺服器上安裝了三個tomcat 例子   配置方法.  如果你的tomcat在別的伺服器,就把對應ip替換即可

配置完成強制重新整理nginx配置檔案以後,即可通過 http://47.52.28.116/server1/  http://47.52.28.116/server2/

http://47.52.28.116/server3/來訪問不同的tomcat專案了

如果是一個專案放在多個tomcat下,tomcat叢集的話,

命令 : # vi /usr/local/nginx/conf/nginx.conf

在這段紅色程式碼之上加  如下配置:

    server {
        listen       80;
        server_name  localhost;


        #charset koi8-r;


        #access_log  logs/host.access.log  main;


        location / {
            root   html;
            index  index.html index.htm;

        }

upstream lxstest {
        server localhost:7070;
        server localhost:7071;
        server localhost:7072;

}

localhost:7070  的地方放你的伺服器ip及埠

然後在 server標籤中的location中加一行:  proxy_pass http://lxstest;  即可

現在訪問nginx之後,會輪詢訪問代理的某一個tomcat伺服器.

Nginx upstream的5種權重分配方式

原文地址:https://blog.csdn.net/wh2691259/article/details/52300423

1.輪詢(預設)

每個請求按時間順序逐一分配到不同的後端伺服器,如果後端伺服器down掉,能自動剔除。

2.weight

指定輪詢機率,weight和訪問比率成正比,用於後端伺服器效能不均的情況。


upstream backend { 
    server 192.168.0.14 weight=10; 
    server 192.168.0.15 weight=10; 
} 

3.ip_hash

每個請求按訪問ip的hash結果分配,這樣每個訪客固定訪問一個後端伺服器,可以解決session的問題。


upstream backend { 
    ip_hash; 
    server   172.16.125.76:8066 weight=10;  
    server   172.16.125.76:8077 down;  
    server   172.16.0.18:8066 max_fails=3 fail_timeout=30s;  
    server   172.16.0.18:8077 backup;  
} 

根據伺服器的本身的效能差別及職能,可以設定不同的引數控制。

  • down 表示負載過重或者不參與負載

  • weight 權重過大代表承擔的負載就越大

  • backup 其它伺服器時或down時才會請求backup伺服器

  • max_fails 失敗超過指定次數會暫停或請求轉往其它伺服器

  • fail_timeout 失敗超過指定次數後暫停時間

4.fair(第三方)

按後端伺服器的響應時間來分配請求,響應時間短的優先分配

5.url_hash(第三方)

按訪問url的hash結果來分配請求,使每個url定向到同一個後端伺服器,後端伺服器為快取時比較有效