當訪問 www.taobao.com 時候會依據負載均衡策略來進行訪問css
拷貝兩份tomcat文件,分別命名爲taobao一、taobao2linux
[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
[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
[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>
[root@fudanwuxi003 software]# ./taobao1/bin/startup.sh [root@fudanwuxi003 software]# ./taobao2/bin/startup.sh
打開192.168.10.191:8081時候返回的是taobao1的頁面 nginx
打開192.168.10.191:8082時候返回的是taobao2的頁面web
[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; #指定代理的後臺服務器 } } ~
192.168.10.191 www.taobao.com
打開 www.taobao.com,會隨機打開taobao1或taobao2的頁面 apache
[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頁面出現的次數vim
[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; } } ~
[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 的時候就正常了windows