配置
vim /usr/local/nginx/conf/nginx.conf
在http內(nèi)添加:
upstream proxy_test {
server 192.168.10.87:80 weight=1; #如果你要測試,把這里換成你自己要代理后端的ip
server 192.168.10.86:80 weight=1;
#ip_hash; #當(dāng)負(fù)載兩臺以上用ip來hash解決session的問題,一臺就別hash了。
}
在server內(nèi)添加:
location / {
proxy_pass http://proxy_test; #這里proxy_test是上面的負(fù)載的名稱,映射到代理服務(wù)器,可以是ip加端口, 或url
proxy_set_header Host $host;
proxy_set_header X-Real-IP $remote_addr;
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
}
nginx平滑重啟
nginx -s reload
測試
后端服務(wù)器開啟,在192.168.10.87和86的網(wǎng)頁文件位置添加測試文件test.html,內(nèi)容87上: this is test 87, 86上:this is test 86,這樣方便查看訪問到哪臺了。
在本地配置好host,192.168.10.65 www.test.com
ping www.test.com 是否能ping通這個(gè)192.168.10.65地址,如果ok,則繼續(xù)打開瀏覽器用www.test.com去訪問后端服務(wù)器的文件,如: www.test.com/test.html,瀏覽器打開顯示有this...說明配置ok了。
然后F5刷新一下,如果是87和86不停的切換,說明負(fù)載ok了。