使用Nginx实现Web反向代理功能,实现如下功能: 后端Web服务器两台,可以使用httpd实现 Nginx采用轮询的方式调用后端Web服务器 两台Web服务器的权重要求设置为不同的值 最大失败次数为2,失败超时时间为30秒
使用3台RHEL6虚拟机,其中一台作为Nginx代理服务器,该服务器需要配置两块网卡,IP地址分别为172.16.0.254和192.168.4.5,两台Web服务器IP地址分别为192.168.4.205和192.168.4.200。测试主机IP地址为172.16.0.1。如图-1所示。
步骤一:配置Nginx服务器,添加服务器池,实现反向代理功能 1)修改/usr/local/nginx/conf/nginx.conf配置文件
[root@svr5 ~]# vim /usr/local/nginx/conf/nginx.conf .. .. http { .. .. upstream webserver { server 192.168.4.205 weight=2 max_fails=2 fail_timeout=10; server 192.168.4.200 weight=1 max_fails=2 fail_timeout=10; } .. .. server { listen 80; server_name www.tarena.com; location / { proxy_pass http://webserver; } }2)重启nginx服务
[root@svr5 ~]# /usr/local/nginx/sbin/nginx –s stop [root@svr5 ~]# /usr/local/nginx/sbin/nginx步骤二:部署实施后端Web服务器 1)部署后端Web1服务器 后端Web服务器可以简单使用yum方式安装httpd实现Web服务,为了可以看出后端服务器的不同,可以将两台后端服务器的首页文档内容设置为不同的内容。
[root@web1 ~]# yum -y install httpd [root@web1 ~]# echo “192.168.4.205” > /var/www/html/index.html [root@web1 ~]# service httpd start2)部署后端Web2服务器
[root@web2 ~]# yum -y install httpd [root@web2 ~]# echo “192.168.4.200” > /var/www/html/index.html [root@web2 ~]# service httpd start步骤三:客户端测试 1)修改客户端hosts文件
[root@client ~]# vim /etc/hosts .. .. 172.16.0.254 www.tarena.com2)使用浏览器访问代理服务器测试轮询效果
[root@client ~]# curl http://www.tarena.com //使用该命令多次访问查看效果