当访问 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
[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的页面
打开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; #指定代理的后台服务器
}
}
~
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页面出现的次数
[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 的时候就正常了
原文:http://blog.51cto.com/jschinamobile/2163068