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

配置服務(wù)器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

配置服務(wù)器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

測試負(fù)載均衡:
[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/>
然后瀏覽器進(jìn)行訪問
權(quán)重模式負(fù)載均衡
打開nginx(負(fù)責(zé)負(fù)載均衡)主虛擬機(jī)
編輯配置文件:
在http{}模塊里添加以下內(nèi)容
upstream webServer {
server 192.168.95.138:80 weight=3; #在原來的基礎(chǔ)上添加weight=自定義權(quán)重值
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
測試負(fù)載均衡
[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/>
從上面結(jié)果可以看出,一共測試訪問10次用戶請求,分流至server2服務(wù)器上的有7次,分流至server1服務(wù)器上的有3次,表明權(quán)重配置生效
總結(jié)
以上為個人經(jīng)驗(yàn),希望能給大家一個參考,也希望大家多多支持腳本之家。
- Nginx中多種負(fù)載均衡策略配置的實(shí)戰(zhàn)指南
- Nginx負(fù)載均衡通用方案詳解
- Nginx負(fù)載均衡與健康檢查使用詳解
- 詳解Nginx中常見負(fù)載均衡策略配置與使用場景
- Nginx 負(fù)載均衡和緩存配置最佳實(shí)踐
- 通過nginx做mysql的負(fù)載均衡實(shí)現(xiàn)過程
- Nginx+Tomcat負(fù)載均衡群集全過程
- Nginx部署負(fù)載均衡服務(wù)的步驟全解析
- nginx負(fù)載均衡及詳細(xì)配置方法
- nginx實(shí)現(xiàn)負(fù)載均衡與實(shí)例解讀
- Nginx幾種負(fù)載均衡模式的實(shí)現(xiàn)示例
相關(guān)文章
Laravel的Nginx重寫規(guī)則實(shí)例代碼
這篇文章主要介紹了Laravel的Nginx重寫規(guī)則實(shí)例代碼,需要的朋友可以參考下2017-09-09
Nginx中的用戶認(rèn)證配置及阻止用戶使用代理訪問的方法
這篇文章主要介紹了Nginx中的用戶認(rèn)證配置及阻止用戶使用代理訪問的方法,用戶認(rèn)證部分用到了自帶的ngx_http_auth_basic_module模塊,需要的朋友可以參考下2016-01-01
nginx?Rewrite重寫地址的實(shí)現(xiàn)
本文主要介紹了nginx?Rewrite重寫地址的實(shí)現(xiàn),文中通過示例代碼介紹的非常詳細(xì),對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧2022-07-07
解讀1g內(nèi)存的電腦nginx能支持多少并發(fā)
這篇文章主要介紹了1g內(nèi)存的電腦nginx能支持多少并發(fā)的問題,具有很好的參考價值,希望對大家有所幫助,如有錯誤或未考慮完全的地方,望不吝賜教2025-06-06
nginx加php-fpm出現(xiàn)502 bad gateway錯誤的5種解決方法
這篇文章主要介紹了nginx加php-fpm環(huán)境中出現(xiàn)502 bad gateway錯誤的5種解決方法,總結(jié)歸納服務(wù)器出現(xiàn)502錯誤的原因多數(shù)為連接過多和腳本超時,本文總結(jié)了5種解決方法,需要的朋友可以參考下2014-05-05

