nginx負載均衡配置方式
nginx實現(xiàn)負載均衡的方式
- 1.輪詢(默認)
每個請求按時間順序逐一分配到不同的后端服務器,后端服務器宕機時,能被自動刪除且請求不會受影響
- 2.weight權(quán)重
指定輪詢概率,weight和訪問比率成正比,用于后端服務器性能不均的情況,權(quán)重越高被訪問的概率就越大
- 3.ip hash
每個請求被訪問ip的hash結(jié)果分配,這樣每個訪客訪客固定訪問一個后端服務器
- 4.fair
動態(tài)根據(jù)后端服務器處理請求的響應時間來進行負載分配,響應時間短的優(yōu)先分配,時間長的 分配的請求會減少,nginx服務默認不支持這個算法,需要安裝upstream_fair模塊
- 5.url_hash
根據(jù)訪問的UPL計算出的hash結(jié)果來分配請求,每個請求會指向固定的服務器,常用于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{}模塊里添加以下內(nèi)容 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/>
然后瀏覽器進行訪問
權(quán)重模式負載均衡
打開nginx(負責負載均衡)主虛擬機
編輯配置文件:
在http{}模塊里添加以下內(nèi)容
upstream webServer { server 192.168.95.138:80 weight=3; #在原來的基礎上添加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
測試負載均衡
[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服務器上的有7次,分流至server1服務器上的有3次,表明權(quán)重配置生效
總結(jié)
以上為個人經(jīng)驗,希望能給大家一個參考,也希望大家多多支持腳本之家。
- Nginx+keepalived雙機熱備技術實踐
- 生產(chǎn)環(huán)境部署Nginx服務器雙機熱備部署keepalived的步驟(多種模式教程)
- Nginx結(jié)合keepalived實現(xiàn)雙機熱備方案
- Nginx雙機熱備的實現(xiàn)步驟
- Nginx+Keepalived實現(xiàn)雙機熱備
- keepalived雙機熱備nginx的配置方法
- Nginx+Tomcat負載均衡群集全過程
- Nginx部署負載均衡服務的步驟全解析
- nginx負載均衡及詳細配置方法
- nginx實現(xiàn)負載均衡與實例解讀
- Nginx實現(xiàn)負載均衡的配置步驟
- nginx tcp負載均衡的具體實現(xiàn)
- keepalived+nginx+httpd實現(xiàn)的雙機熱備+負載均衡
相關文章
比較完整的Nginx配置文件nginx.conf常用參數(shù)中文詳解
這篇文章主要介紹了比較完整的Nginx配置文件nginx.conf常用參數(shù)中文詳解,需要的朋友可以參考下2015-07-07Keepalived實現(xiàn)Nginx負載均衡高可用的示例代碼
這篇文章主要介紹了Keepalived實現(xiàn)Nginx負載均衡高可用的示例代碼,文中通過示例代碼介紹的非常詳細,對大家的學習或者工作具有一定的參考學習價值,需要的朋友們下面隨著小編來一起學習學習吧2020-04-04