实验环境
一台centos7做nginx服务器
一台centos7做tomcat服务器1
一台centos7做tomcat服务器2
共三台
两台tomcat服务器搭建方法可参考我的上篇文章
Nginx服务器配置
安装依赖包
yum -y install pcre-devel zlib-devel openssl-devel gcc gcc-c++
解压并安装nginx。
[[email protected] /]# groupadd www
[[email protected] /]# useradd -g www www -s /bin/false
[[email protected] /]# tar zxf nginx-1.12.0.tar.gz
[[email protected] /]# cd nginx-1.12.0
[[email protected] nginx-1.12.0]# ./configure --prefix=/usr/local/nginx --user=www --group=www --with-file-aio --with-http_stub_status_module --with-http_gzip_static_module --with-http_flv_module
[[email protected] nginx-1.12.0]#make && make install
配置nginx.conf.
vim /usr/local/nginx/conf/nginx.conf
在文件中添加以下内容
http中添加
#gzip on;
upstream tomcat_server {
server 192.168.109.100:8080 weight=1;
server 192.168.109.200:8080 weight=1;
}
在
location / {
root html;
index index.html index.htm;
下添加
proxy_pass http://tomcat_server;
}
效果图:
测试nginx配置文件是否正确。
[[email protected] nginx-1.12.0]# /usr/local/nginx/sbin/nginx -t
nginx: the configuration file /usr/local/nginx/conf/nginx.conf syntax is ok
nginx: configuration file /usr/local/nginx/conf/nginx.conf test is successful
确认OK后再启动nginx
[[email protected] nginx-1.12.0]# /usr/local/nginx/sbin/nginx -c /usr/local/nginx/conf/nginx.conf
测试负载均衡效果
浏览器访问nginx服务器
第一次访问
第二次访问
这说明已经可以在两个Tomcat server 站点进行切换了