http://blog.csdn.net/zhoufoxcn/article/details/72872524
http://blog.csdn.net/duguxingfeng/article/details/78929472
安装 tomcat 为测试Nginx反向代理
1.下载
# wget http://mirrors.shuosc.org/apache/tomcat/tomcat-8/v8.0.48/bin/apache-tomcat-8.0.48.tar.gz
2.解压
# tar -zxvf apache-tomcat-8.0.48.tar.gz
3.启动测试
# cd /apache-tomcat-8.0.48/bin
# ./startup.sh
启动成功 访问:http://localhost:8080
4.打开 nginx的配置文件
# cd /usr/local/nginx/conf
复制一份配置文件 命名为: nginx.conf.bak
#cp nginx.conf nginx.conf.bak
# vim nginx.conf
找到: location / 处,添加: proxy_pass http://localhost:8080; http://localhost:8080;为tomcat的访问路径
修改之后,保存。重启nginx
# ./usr/local/nginx/sbin/nginx -s reload
访问:http://localhost:80 即可访问 tomcat的路径
四、Nginx 负载均衡
1.此处需要部署两个tomcat 进入到第四步下载的tomcat的路径下
# cp -r apache-tomcat-8.0.48 tomcat1
# cp -rapache-tomcat-8.0.48 tomcat2
2.修改tomcat配置文件
tomcat1 端口设定为8080 ;tomcat1的配置无需修改
tomcat2 端口设定为8090
# cd /tomcat2/conf
# vim server.xml
修改
<Server port="8005" shutdown="SHUTDOWN">
为:<Server port="8015" shutdown="SHUTDOWN">
<Connector port="8080" protocol="HTTP/1.1" connectionTimeout="20000" redirectPort="8443" />
为:<Connector port="8090" protocol="HTTP/1.1" connectionTimeout="20000" redirectPort="8443" />
<Connector port="8009" protocol="AJP/1.3" redirectPort="8443" />
为: <Connector port="8019" protocol="AJP/1.3" redirectPort="8443" />
3.在tomcat1和tomcat2下创建测试站点
# cd /tomcat1/webapp
# madir test
# vi index.html
添加
this is tomcat1 index
esc :wq 保存推出。
同理在tomcat2下创建test与indexhtml,添加this is tomcat2 index
# cd /tomcat2/webapp
# madir test
# vi index.html
添加
this is tomcat2 index
esc :wq 保存推出。
同时为便于识别,修改监听8081端口的Tomcat的root目录下的index.jsp页面,将首页中的“
If you're seeing this, you've successfully installed Tomcat. Congratulations!”
改为“If you're seeing this, you've successfully installed Tomcat. Congratulations!Server port:8081!”。
# vim /usr/local/nginx/conf/nginx.conf
保存后退出
分别启动tomcat1与tomcat2
# /tomcat1/bin/startup.sh
# /tomcat2/bin/startup.sh
分别访问 http://localhost:8080/test1与http://localhost:8090/test2 验证是否正常
重启 nginx
# /usr/local/nginx/sbin/nginx -s reload
打开浏览器输入:http://localhost/test 多刷新几次,就会看到请求回来的内容有时不一样。说明,nginx 是随机访问的tomcat1或者tomcat2
配置文件里面的weight哪个数值越大,请求到的次数就会多。