Nginx + tomcat 实现服务集群

xiaoxiao2021-02-28  104

测试环境: 虚拟机 + linux(centOs 6.5) + windows + tomcat + Nginx 话不多说,直接开撸

1、在linux上放两个tomcat, 并更改不同的端口,我测试了一下,需要改如下几个地方(我都在默认的端口上加了一个1),因为我只有两个tomcat,所以另一个就不用改了,使用默认的就行,所以这里改的是其中一个: <Server port="18005" shutdown="SHUTDOWN"> <Connector port="18080" protocol="HTTP/1.1" connectionTimeout="20000" redirectPort="8443" /> <Connector executor="tomcatThreadPool" port="18080" protocol="HTTP/1.1" connectionTimeout="20000" redirectPort="8443" /> <Connector port="18009" protocol="AJP/1.3" redirectPort="8443" />

2、修改一下/webapps/ROOT下的index.jsp,随便加点什么标识一下,以便区分后面访问的是哪个tomcat

3、先测试一下访问两个tomcat,如图,两个tomcat可以正常访问

4、在linux上安装Nginx,如果不会的可以查看我的另一篇文章哈 http://blog.csdn.net/fantasic_van/article/details/767852715、配置nginx worker_processes 1;#工作进程的个数,一般与计算机的cpu核数一致 events { worker_connections 1024;#单个进程最大连接数(最大连接数=连接数*进程数) } http { include mime.types; #文件扩展名与文件类型映射表 default_type application/octet-stream;#默认文件类型 sendfile on;#开启高效文件传输模式,sendfile指令指定nginx是否调用sendfile函数来输出文件,对于普通应用设为 on,如果用来进行下载等应用磁盘IO重负载应用,可设置为off,以平衡磁盘与网络I/O处理速度,降低系统的负载。注意:如果图片显示不正常把这个改成off keepalive_timeout 65; #长连接超时时间,单位是秒 gzip on;#启用Gizp压缩 #服务器的集群 upstream netitcast.com { #服务器集群名字 server 192.168.40.120:8080 weight=1;#服务器配置 weight是权重的意思,权重越大,分配的概率越大。 server 192.168.40.120:18080 weight=2; } #当前的Nginx的配置 server { listen 80;#监听80端口,可以改成其他端口 server_name localhost;############## 当前服务的域名 location / { proxy_pass http://netitcast.com; proxy_redirect default; } error_page 500 502 503 504 /50x.html; location = /50x.html { root html; } } } 6、上面的主要配置其实就这两个地方: #服务器的集群 upstream netitcast.com { #服务器集群名字 server 192.168.40.120:8080 weight=1; server 192.168.40.120:18080 weight=2; } location / { proxy_pass http://netitcast.com; #这里的名字要个集群的名字对应 proxy_redirect default; } 7、启动nginx8、在windows中访问 192.168.40.120/index.jsp,出现如下画面

当刷新一下页面后:

再刷新后,发现又会变,而且tomcat2出现的次数会比tomcat1多,原因就是因为之前Nginx的配置中,weight权重的影响了。

转载请注明原文地址: https://www.6miu.com/read-62346.html

最新回复(0)