在nginx实现负载均衡前,须要下载tomcat,我这里装的是8.5.43,下载地址:https://tomcat.apache.org/download-80.cginginx
将下载的tomcat压缩包解压两次web
首先: 在Tomcat的根(安装)目录下,有一个conf文件夹,双击进入conf文件夹,在里面找到server.xml文件,打开该文件。apache
其次:在文件中找到三处须要修改的地方,以下文本:
(1)<Connector port="8080" protocol="HTTP/1.1"
maxThreads="150" connectionTimeout="20000"
redirectPort="8443" />
也有多是这样的:
<Connector port="8080" maxThreads="150" minSpareThreads="25" maxSpareThreads="75" enableLookups="false" redirectPort="8443" acceptCount="100" debug="0" connectionTimeout="20000"
disableUploadTimeout="true" />等等;浏览器
将port="8080"改成其它的就能够了。如port="18080"等。tomcat
(2)将 <Connector port="8009" enableLookups="false" redirectPort="8443" debug="0"
protocol="AJP/1.3" />的8009改成其它的端口。服务器
(3) 继续将<Server port="8005" shutdown="SHUTDOWN" debug="0">的8005改成其它的端口。app
保存server.xml文件,从新启动Tomcat服务器,Tomcat就能够使用18080端口了。负载均衡
为了区分两个tomcat,分别在tomcat目录下找到webapps文件夹下的ROOT下的index.jsp,修改一下<h2>标签的内容webapp
分别在两个tomcat的bin目录下找到startup.bat,双击启动,或者在bin目录下打开dos,输入startup.bat启动jsp
若是没有闪退,并出现以下图的窗口表示启动成功了
在浏览器地址栏输入:localhost:8080,localhost:18080,能显示出下图就正常启动了
接下来配置nginx.conf文件
worker_processes 1; events { worker_connections 1024; } http { include mime.types; default_type application/octet-stream; upstream test1 { #down 表示单前的server临时不參与负载. #weight 默以为1.weight越大,负载的权重就越大。 #max_fails :赞成请求失败的次数默以为1.当超过最大次数时,返回proxy_next_upstream 模块定义的错误. #fail_timeout : max_fails次失败后。暂停的时间。 #backup: 其余所有的非backup机器down或者忙的时候,请求backup机器。因此这台机器压力会最轻。 server localhost:8080 weight=5; server localhost:18080 weight=10; } server { listen 80; server_name localhost; root D:/root; location / { proxy_pass http://test1;#这里的test与上面upstream的命名要一致 } } }
而后启动nginx,并在浏览器访问localhost
多刷新几回,就会在两个tomcat之间切换,至此nginx负载均衡的简单实现就完成了