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 //屢次訪問查看效果