1. 程式人生 > >Nginx+Tomcat實現負載均衡及動靜分離

Nginx+Tomcat實現負載均衡及動靜分離

動靜 webapps ado art index.jsp mod connector mct 圖片

  • 內部模擬兩臺服務器taoba1和taobao2
  • 當訪問 www.taobao.com 時候會依據負載均衡策略來進行訪問
    技術分享圖片

  • 拷貝兩份tomcat文件,分別命名為taobao1、taobao2

    [root@fudanwuxi003 conf.d]# cd /root/software/
    [root@fudanwuxi003 software]# ll
    總用量 190720
    -rw-r--r--. 1 root root     60564 8月  21 23:36 1.jpg
    drwxr-xr-x. 9 root root       160 8月  20 14:56 apache-tomcat-8.5.32
    -rw-r--r--. 1 root root   9584807 8月  20 13:40 apache-tomcat-8.5.32.tar.gz
    -rw-r--r--. 1 root root 185646832 8月  20 13:34 jdk-8u181-linux-x64.tar.gz
    [root@fudanwuxi003 software]# cp -r apache-tomcat-8.5.32 taobao1
    [root@fudanwuxi003 software]# cp -r apache-tomcat-8.5.32 taobao2
    [root@fudanwuxi003 software]# pwd
    /root/software
  • 修改taobao1和taobao2的tomcat端口號
  • [root@fudanwuxi003 software]# vim taobao1/conf/server.xml 
     22 <Server port="8006" shutdown="SHUTDOWN">  #將默認的8005修改為8006
    
     68     -->
     69     <Connector port="8081" protocol="HTTP/1.1"  #將默認的8080修改為8081
     70                connectionTimeout="20000"
     71                redirectPort="8443" />
     72     <!-- A "Connector" using the shared thread pool-->
     73     <!--
    
    116     <Connector port="8010" protocol="AJP/1.3" redirectPort="8443" />  #將默認的8009修改為8010
    [root@fudanwuxi003 software]# vim taobao2/conf/server.xml
     22 <Server port="8007" shutdown="SHUTDOWN">  #將默認的8005修改為8007
    
     68     -->
     69     <Connector port="8082" protocol="HTTP/1.1"  #將默認的8080修改為8082
     70                connectionTimeout="20000"
     71                redirectPort="8443" />
     72     <!-- A "Connector" using the shared thread pool-->
     73     <!--
    
     116     <Connector port="8011" protocol="AJP/1.3" redirectPort="8443" />  #將默認的8009修改為8011
    • 分別修改taobao1和taobao2的默認頁面,進行區分
    [root@fudanwuxi003 software]# pwd
    /root/software
    [root@fudanwuxi003 software]# vim taobao1/webapps/ROOT/index.jsp
    <h2>If you‘re seeing this, you‘ve successfully access to taobao1!</h2>
    
    [root@fudanwuxi003 software]# vim taobao2/webapps/ROOT/index.jsp
    <h2>If you‘re seeing this, you‘ve successfully access to taobao2!</h2>
    • 分別啟動taobao1和taobao2的tomcat
    [root@fudanwuxi003 software]# ./taobao1/bin/startup.sh 
    [root@fudanwuxi003 software]# ./taobao2/bin/startup.sh 
    • 驗證結果

    打開192.168.10.191:8081時候返回的是taobao1的頁面
    技術分享圖片

    打開192.168.10.191:8082時候返回的是taobao2的頁面
    技術分享圖片

    • 創建虛擬主機文件並配置負載均衡
    [root@fudanwuxi003 software]# cd /etc/nginx/conf.d/
    [root@fudanwuxi003 conf.d]# cp proxy.conf taobao.conf
    [root@fudanwuxi003 conf.d]# vim taobao.conf 
    #後臺服務器列表
    upstream taobaohost{
             server 192.168.10.191:8081;
             server 192.168.10.191:8082;
    }
    
    server {
           listen   80;
           server_name www.taobao.com;
    
           location / {
               proxy_pass http://taobaohost;  #指定代理的後臺服務器
        }
    
    }
    ~                                                
    • 在windows的hosts文件中添加域名解析
    192.168.10.191      www.taobao.com
    • 驗證

    打開 www.taobao.com,會隨機打開taobao1或taobao2的頁面
    技術分享圖片
    技術分享圖片

    • 負載均衡策略(默認為輪詢,修改為根據權重,服務器的性能不均的情況)
    [root@fudanwuxi003 conf.d]# vim taobao.conf
    upstream taobaohost{
             server 192.168.10.191:8081 weight=8;  weight代表權重,數值越大,被訪問的概率越大
             server 192.168.10.191:8082 weight=2;
    }
    systemctl restart nginx
    • 此時打開 www.taobao.com 的時候,taobao1的頁面出現的次數明顯多於taobao2頁面出現的次數

    • 由於Tomcat處理靜態資源效率不高,會導致Web應用相應慢,占用系統資源。將靜態資源交給Nginx處理,動態資源仍由Tomcat處理,實現動靜分離。

    技術分享圖片

    • 編輯taobao.conf,將靜態資源交給Nginx處理
    [root@fudanwuxi003 conf.d]# vim taobao.conf 
    upstream taobaohost{
             server 192.168.10.191:8081 weight=8;
             server 192.168.10.191:8082 weight=2;
    }
    
    server {
           listen   80;
           server_name www.taobao.com;
    
    #處理動態資源
           location / {
               proxy_pass http://taobaohost;
        }
    #處理靜態資源
           location ~ .*\.(js|css|ico|png|jpg|eot|svg|ttf|woff) {
              root /servers/taobao/static;
        }
    
    }
    ~                                                     
    • 由於還沒有創建靜態資源的目錄,所以此時打開 www.taobao.com 如下圖

    技術分享圖片

    技術分享圖片

    • 創建存放靜態資源的目錄,並將靜態資源放在該目錄中
    
    [root@fudanwuxi003 conf.d]# mkdir -p /servers/taobao/static
    [root@fudanwuxi003 conf.d]# cd /servers/taobao/static/
    [root@fudanwuxi003 static]# cp /root/software/taobao1/webapps/ROOT/tomcat.png ./
    [root@fudanwuxi003 static]# cp /root/software/taobao1/webapps/ROOT/tomcat.css ./
    [root@fudanwuxi003 static]# ll
    總用量 16
    -rw-r-----. 1 root root 5581 8月  22 18:39 tomcat.css
    -rw-r-----. 1 root root 5103 8月  22 18:39 tomcat.png
    [root@fudanwuxi003 static]# pwd
    /servers/taobao/static
    
    [root@fudanwuxi003 static]# chmod 755 *
    [root@fudanwuxi003 static]# ll
    總用量 16
    -rwxr-xr-x. 1 root root 5581 8月  22 18:39 tomcat.css
    -rwxr-xr-x. 1 root root 5103 8月  22 18:39 tomcat.png
    • 驗證

    再次打開 www.taobao.com 的時候就正常了
    技術分享圖片

    Nginx+Tomcat實現負載均衡及動靜分離