負載均衡是我們大流量網(wǎng)站要做的一個東西,下面我來給大家介紹在Nginx服務器上進行負載均衡配置方法。
測試環(huán)境
測試域名? :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段加入以下代碼 :
[php]view plaincopy
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
[php]view plaincopy
拓展知識
通過上面的配置可以發(fā)現(xiàn)上面配置的負載均衡是按照1:1的方式來回切換殖演,其實你也可以通過配置文件你可以站點的權(quán)重:
upstream??site?{
server???192.168.3.82:8040?weight=2;
server???192.168.3.82:8041?weight=1;
}
????weight即為對應網(wǎng)站的權(quán)重。
② 工作服務器配置方法
我們要在 B年鸳、C服務器nginx.conf設(shè)置如下
打開nginx.confi趴久,在http段加入以下代碼
[php]view plaincopy
server{
listen?8080;
server_name?www.threegroup.space;
index?index.html;
root?/data0/htdocs/www;
}
如截圖所示
保存重啟nginx
③ 測試
當訪問www.threegroup.space的時候,為了區(qū)分是轉(zhuǎn)向哪臺服務器處理我分別在B搔确、C服務器下寫一個不同內(nèi)容的index.html文件彼棍,以作區(qū)分。
打開瀏覽器訪問www.threegroup.space結(jié)果膳算,刷新會發(fā)現(xiàn)所有的請求均分別被主服務器(A) 分配到?B服務器(101.200.159.138)與C服務器(123.56.255.53)上座硕,實現(xiàn)了負載均衡效果。