亚洲乱码中文字幕综合,中国熟女仑乱hd,亚洲精品乱拍国产一区二区三区,一本大道卡一卡二卡三乱码全集资源,又粗又黄又硬又爽的免费视频

nginx負(fù)載均衡配置方式

 更新時(shí)間:2025年06月05日 14:43:50   作者:*源  
這篇文章主要介紹了nginx負(fù)載均衡配置方式,具有很好的參考價(jià)值,希望對大家有所幫助,如有錯(cuò)誤或未考慮完全的地方,望不吝賜教

nginx實(shí)現(xiàn)負(fù)載均衡的方式

  • 1.輪詢(默認(rèn))

每個(gè)請求按時(shí)間順序逐一分配到不同的后端服務(wù)器,后端服務(wù)器宕機(jī)時(shí),能被自動刪除且請求不會受影響

  • 2.weight權(quán)重

指定輪詢概率,weight和訪問比率成正比,用于后端服務(wù)器性能不均的情況,權(quán)重越高被訪問的概率就越大

  • 3.ip hash

每個(gè)請求被訪問ip的hash結(jié)果分配,這樣每個(gè)訪客訪客固定訪問一個(gè)后端服務(wù)器

  • 4.fair

動態(tài)根據(jù)后端服務(wù)器處理請求的響應(yīng)時(shí)間來進(jìn)行負(fù)載分配,響應(yīng)時(shí)間短的優(yōu)先分配,時(shí)間長的 分配的請求會減少,nginx服務(wù)默認(rèn)不支持這個(gè)算法,需要安裝upstream_fair模塊

  • 5.url_hash

根據(jù)訪問的UPL計(jì)算出的hash結(jié)果來分配請求,每個(gè)請求會指向固定的服務(wù)器,常用于nginx作為靜態(tài)資源服務(wù)器的場景,可以提高緩存效率,nginx服務(wù)默認(rèn)不支持這個(gè)算法,需要安裝nginx的hash軟件包

環(huán)境配置

需要三臺虛擬機(jī),都要配置好nginx

機(jī)器名服務(wù)器IP用途
nginx (主)192.168.95.137負(fù)載均衡服務(wù)器
服務(wù)器1192.168.95.138后端服務(wù)器
服務(wù)器2192.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)
  • 或者臨時(shí)關(guān)閉(不用重啟機(jī)器):setenforce 0

輪詢模式負(fù)載均衡

打開nginx(負(fù)責(zé)負(fù)載均衡)主虛擬機(jī)

編輯配置文件:

cd /usr/local/nginx/conf

cp nginx.conf nginx.conf.bak #備份一個(gè)配置文件,防止配置錯(cuò)誤可重新配置

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é)

以上為個(gè)人經(jīng)驗(yàn),希望能給大家一個(gè)參考,也希望大家多多支持腳本之家。 

相關(guān)文章

最新評論