Nginx負載均衡配置實例詳解
負載均衡是我們大流量網站要做的一個東西,下面我來給大家介紹在Nginx服務器上進行負載均衡配置方法。
測試環境
測試域名 :www.threegroup.space
A服務器IP :123.56.255.173 (主)
B服務器IP :101.200.159.138
C服務器IP :123.56.255.53
部署思路
A服務器做爲主服務器,域名直接解析到A服務器(123.56.255.173)上,由A服務器負載均衡到B服務器(101.200.159.138)與C服務器(123.56.255.53)上。
開始配置Nginx負載均衡
① 打開nginx.conf,文件位置在nginx安裝目錄的conf目錄下。
在http段加入以下代碼 :
upstream www.threegroup.space {
server 123.56.255.53:8080;
server 101.200.159.138:8080;
}
server{
listen 80;
server_name www.threegroup.space;
location / {
proxy_pass http://www.threegroup.space;
proxy_set_header Host $host;
proxy_set_header X-Real-IP $remote_addr;
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
}
}
如截圖所示
從截圖上看到
已成功將www.threegroup.space解析到101.200.159.138:8080 的IP
和 123.56.255.53:8080 的IP上
最後 保存重啓nginx
② 工作服務器配置方法
我們要在 B、C服務器nginx.conf設置如下
打開nginx.confi,在http段加入以下代碼
server{
listen 8080;
server_name www.threegroup.space;
index index.html;
root /data0/htdocs/www;
}
如截圖所示
保存重啓nginx
③ 測試
當訪問www.threegroup.space的時候,爲了區分是轉向哪臺服務器處理我分別在B、C服務器下寫一個不同內容的index.html文件,以作區分。
打開瀏覽器訪問www.threegroup.space結果,刷新會發現所有的請求均分別被主服務器(A) 分配到 B服務器(101.200.159.138)與C服務器(123.56.255.53)上,實現了負載均衡效果。
推薦學習網站
http://www.php100.com/html/program/nginx/2013/0905/5525.html
http://www.cnblogs.com/liping13599168/archive/2011/04/15/2017369.html