nginx負載均衡配置方式
nginx實現(xiàn)負載均衡的方式
- 1.輪詢(默認)
每個請求按時間順序逐一分配到不同的后端服務器,后端服務器宕機時,能被自動刪除且請求不會受影響
- 2.weight權重
指定輪詢概率,weight和訪問比率成正比,用于后端服務器性能不均的情況,權重越高被訪問的概率就越大
- 3.ip hash
每個請求被訪問ip的hash結果分配,這樣每個訪客訪客固定訪問一個后端服務器
- 4.fair
動態(tài)根據(jù)后端服務器處理請求的響應時間來進行負載分配,響應時間短的優(yōu)先分配,時間長的 分配的請求會減少,nginx服務默認不支持這個算法,需要安裝upstream_fair模塊
- 5.url_hash
根據(jù)訪問的UPL計算出的hash結果來分配請求,每個請求會指向固定的服務器,常用于nginx作為靜態(tài)資源服務器的場景,可以提高緩存效率,nginx服務默認不支持這個算法,需要安裝nginx的hash軟件包
環(huán)境配置
需要三臺虛擬機,都要配置好nginx
| 機器名 | 服務器IP | 用途 |
| nginx (主) | 192.168.95.137 | 負載均衡服務器 |
| 服務器1 | 192.168.95.138 | 后端服務器 |
| 服務器2 | 192.168.95.139 | 后端服務器 |
1.設置防火墻 三臺虛擬機都要設置
firewall-cmd --zone=public --add-port=80/tcp --permanent systemctl restart firewalld.service
- 關閉selinux: /etc/selinux/config
- 修改配置文件:將selinux=enforcing改為disabled
- 弄好后重啟虛擬機,查看后出現(xiàn)Disabled
getenforce #查看selinux狀態(tài)
- 或者臨時關閉(不用重啟機器):setenforce 0
輪詢模式負載均衡
打開nginx(負責負載均衡)主虛擬機
編輯配置文件:
cd /usr/local/nginx/conf
cp nginx.conf nginx.conf.bak #備份一個配置文件,防止配置錯誤可重新配置
vim nginx.conf
在http{}模塊里添加以下內容
upstream webServer {
server 192.168.95.138:80; #服務器1
server 192.168.95.139:80; #服務器2
}
server{
listen 80;
server_name 192.168.95.137;
location / {
index index.html index.htm;
proxy_pass http://webServer; #【webServer】和upstream 【webServer】名字一致
}
}檢查語法并重啟
/usr/local/nginx/sbin/nginx -t #檢查語法 /usr/local/nginx/sbin/nginx -s reload #重啟

配置服務器1
cd /usr/local/nginx/html/ cp index.html index.html.bak vim index.html #清空里面的所有配置
添加下面的語句:
<h>Welcome to server1<h>
保存退出
/usr/local/nginx/sbin/nginx -t /usr/local/nginx/sbin/nginx -s reload
瀏覽器訪問:http://ip

配置服務器2
cd /usr/local/nginx/html/ cp index.html index.html.bak vim index.html #清空里面的所有配置
添加下面的語句:
<h>Welcome to server2<h>
保存退出
/usr/local/nginx/sbin/nginx -t /usr/local/nginx/sbin/nginx -s reload
瀏覽器訪問:http://ip

測試負載均衡:
[root@localhost conf]# curl 192.168.95.137 <h>Welcome to server1 <h> [root@localhost conf]# curl 192.168.95.137 <h>Welcome to server2<h/> [root@localhost conf]# curl 192.168.95.137 <h>Welcome to server1 <h> [root@localhost conf]# curl 192.168.95.137 <h>Welcome to server2<h/>
然后瀏覽器進行訪問
權重模式負載均衡
打開nginx(負責負載均衡)主虛擬機
編輯配置文件:
在http{}模塊里添加以下內容
upstream webServer {
server 192.168.95.138:80 weight=3; #在原來的基礎上添加weight=自定義權重值
server 192.168.95.139:80 weight=7;
}
server{
listen 80;
server_name 192.168.95.137;
location / {
index index.html index.htm;
proxy_pass http://webServer;
}
}保存退出
/usr/local/nginx/sbin/nginx -t /usr/local/nginx/sbin/nginx -s reload
測試負載均衡
[root@localhost conf]# curl 192.168.95.137 <h>Welcome to server2<h/> [root@localhost conf]# curl 192.168.95.137 <h>Welcome to server1 <h> [root@localhost conf]# curl 192.168.95.137 <h>Welcome to server2<h/> [root@localhost conf]# curl 192.168.95.137 <h>Welcome to server2<h/> [root@localhost conf]# curl 192.168.95.137 <h>Welcome to server1 <h> [root@localhost conf]# curl 192.168.95.137 <h>Welcome to server2<h/> [root@localhost conf]# curl 192.168.95.137 <h>Welcome to server2<h/> [root@localhost conf]# curl 192.168.95.137 <h>Welcome to server2<h/> [root@localhost conf]# curl 192.168.95.137 <h>Welcome to server1 <h> [root@localhost conf]# curl 192.168.95.137 <h>Welcome to server2<h/>
從上面結果可以看出,一共測試訪問10次用戶請求,分流至server2服務器上的有7次,分流至server1服務器上的有3次,表明權重配置生效
總結
以上為個人經(jīng)驗,希望能給大家一個參考,也希望大家多多支持腳本之家。
相關文章
nginx加php-fpm出現(xiàn)502 bad gateway錯誤的5種解決方法
這篇文章主要介紹了nginx加php-fpm環(huán)境中出現(xiàn)502 bad gateway錯誤的5種解決方法,總結歸納服務器出現(xiàn)502錯誤的原因多數(shù)為連接過多和腳本超時,本文總結了5種解決方法,需要的朋友可以參考下2014-05-05

