Nginx反向代理(负载均衡)html
使用3台Centos服务器,其中一台做为Nginx代理服务器,192.168.4.5,nginx
两台Web服务器IP地址分别为192.168.4.205和192.168.4.200。web
配置Nginx服务器,添加服务器池,实现反向代理功能vim
weight 权重值,假如客户反发送三个链接服务器,其中两个会转发到192.168.4.205,另一个转发到192.168.1.200。后端
max_fails 容许请求失败的次数浏览器
fail_timeout 请求失败后,暂停提供服务的时间。服务器
1)修改/usr/local/nginx/conf/nginx.conf配置文件负载均衡
[root@svr ~]# vim /usr/local/nginx/conf/nginx.confcurl
....ide
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@svr ~]# /usr/local/nginx/sbin/nginx –s stop
[root@svr ~]# /usr/local/nginx/sbin/nginx
部署实施后端Web服务器
后端Web服务器能够简单使用yum方式安装httpd实现Web服务,也能够用Nginx、Tomcat,根据实际要求来,我这边为了方便就使用httpd。
为了能够看出后端服务器的不一样,能够将两台后端服务器的首页文档内容设置为不一样的内容。
1)部署后端Web1服务器
[root@web1 ~]# yum -y install httpd
[root@web1 ~]# echo “192.168.4.205” >/var/www/html/index.html
[root@web1 ~]# service httpd start
2)部署后端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.com
2)使用浏览器访问代理服务器测试轮询效果
[root@client ~]# curl http://www.tarena.com //屡次访问查看效果