一、环境
一台nginx做反向代理:192.168.88.142
两台tomcat机器做web:192.168.88.143 192.168.88.144
安装过程略,下一篇详细说明
二、nginx配置文件
nginx:192.168.88.142
user www www; ##用户和组
worker_processes 1; ##和cpu核数有关
events {
use epoll; ##nginx的工作模式
worker_connections 1024; ##每个进程允许的最大连接数
}
http {
include mime.types;
default_type application/octet-stream;
sendfile on;
keepalive_timeout 65;
##设置代理的后端tomcat集群
upstream web_server {
server 192.168.88.143:8080 weight=1 max_fails=2 fail_timeout=30s;
server 192.168.88.144:8080 weight=1 max_fails=2 fail_timeout=30s;
}
server {
listen 80; ## nginx监听端口
server_name 192.168.88.142; ## 设置nginx的主机名或IP地址
root html; ## nginx站点的根目录
index index.html index.htm index.jsp;
##设置代理
location / {
proxy_pass http://web_server; ## 这里设置要代理的集群名称
}
error_page 500 502 503 504 /50x.html;
location = /50x.html {
root html;
}
}
}
三、验证
1.tomcat配置
tomcat1:192.168.88.143
[[email protected] ~]# cat /usr/local/tomcat/webapps/shop/test.jsp
tomcat1 jsp
tomcat2:192.168.88.144
[[email protected] ~]# cat /usr/local/tomcat/webapps/shop/test.jsp
tomcat2 jsp
2.反向代理测试
访问http://192.168.88.142/shop/test.jsp,过几秒后刷新访问,能出现tomcat1和tomcat2,正常
3.负载均衡测试
关闭tomcat1,访问http://192.168.88.142/shop/test.jsp,出现tomcat2正常
启动tomcat1,访问http://192.168.88.142/shop/test.jsp,两个tomcat页面都出现,正常