Nginx+Tomcat实现负载均衡及动静分离
内容导读
互联网集市收集整理的这篇技术教程文章主要介绍了Nginx+Tomcat实现负载均衡及动静分离,小编现在分享给大家,供广大互联网技能从业者学习和参考。文章包含4095字,纯文字阅读大概需要6分钟。
内容图文
当访问 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
- 分别修改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 的时候就正常了
原文:http://blog.51cto.com/jschinamobile/2163068
内容总结
以上是互联网集市为您收集整理的Nginx+Tomcat实现负载均衡及动静分离全部内容,希望文章能够帮你解决Nginx+Tomcat实现负载均衡及动静分离所遇到的程序开发问题。 如果觉得互联网集市技术教程内容还不错,欢迎将互联网集市网站推荐给程序员好友。
内容备注
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 gblab@vip.qq.com 举报,一经查实,本站将立刻删除。
内容手机端
扫描二维码推送至手机访问。