超详细Linux -- nginx负载均衡配置
生活随笔
收集整理的這篇文章主要介紹了
超详细Linux -- nginx负载均衡配置
小編覺得挺不錯的,現在分享給大家,幫大家做個參考.
Nginx 負載均衡應用配置
Nginx實現負載均衡的方式
1、輪詢(默認)
每個請求按時間順序逐一分配到不同的后端服務器,后端服務器宕機時,能被自動刪除,且請求不會受影響。
2、weight權重
指定輪詢概率,weight和訪問比率成正比,用于后端服務器性能不均的情況。權重越高,被訪問的概率就越大。
1.環境配置
| 機器名 | 服務器IP | 用途 |
| nginx (主) | 192.168.233.70 | 負載均衡服務器 |
| 服務器A | 192.168.233.80 | 后端服務器 |
| 服務器B | 192.168.233.90 | 后端服務器 |
需要三臺虛擬機,同時都需要配置好nginx
?
?2.設置防火墻? 三臺虛擬機都要設置
firewall-cmd --zone=public --add-port=80/tcp --permanent systemctl restart firewalld.service關閉selinux: /etc/selinux/config 修改配置文件:將selinux=enforcing改為disabled 弄好后重啟虛擬機,查看后出現Disabled getenforce #查看selinux狀態 或者, 臨時關閉(不用重啟機器):setenforce 0輪詢模式負載均衡?
打開nginx(負責負載均衡)主虛擬機
編輯配置文件:
cd /usr/local/nginx/confcp nginx.conf nginx.conf.bak //養成良好習慣,備份一個配置文件vim nginx.conf在http{}模塊里添加以下內容upstream webServer {server 192.168.233.80:80; //服務器Aipserver 192.168.233.90:80; //服務器Bip } server{ listen 80; server_name 192.168.233.70; 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 //重啟配置服務器A
cd /usr/local/nginx/html/cp index.html index.html.bakvim index.html //清空里面的所有配置添加下面的語句:<h>Welcome to serverA<h>保存退出/usr/local/nginx/sbin/nginx -t/usr/local/nginx/sbin/nginx -s reload?在瀏覽器測試:http://192.168.233.80
?配置服務器B
cd /usr/local/nginx/html/cp index.html index.html.bakvim index.html //清空里面的所有配置添加下面的語句:<h>Welcome to serverB<h>保存退出/usr/local/nginx/sbin/nginx -t/usr/local/nginx/sbin/nginx -s reload在瀏覽器測試:http://192.168.233.90
測試負載均衡?
命令如下
[root@localhost conf]# curl 192.168.233.70 <h>Welcome to serverA <h> [root@localhost conf]# curl 192.168.233.70 <h>Welcome to serverB<h/> [root@localhost conf]# curl 192.168.233.70 <h>Welcome to serverA <h> [root@localhost conf]# curl 192.168.233.70 <h>Welcome to serverB<h/>瀏覽器訪問結果 :
?刷新之后,結果如圖:
?權重模式負載均衡?
打開nginx(負責負載均衡)主虛擬機
編輯配置文件:
在http{}模塊里添加以下內容upstream webServer {server 192.168.233.80:80 weight=3; //在原來的基礎上添加weight=自定義權重值server 192.168.233.90:80 weight=7; } server{ listen 80; server_name 192.168.233.70; 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.233.70 <h>Welcome to serverB<h/>[root@localhost conf]# curl 192.168.233.70 <h>Welcome to serverA <h>[root@localhost conf]# curl 192.168.233.70 <h>Welcome to serverB<h/>[root@localhost conf]# curl 192.168.233.70 <h>Welcome to serverB<h/>[root@localhost conf]# curl 192.168.233.70 <h>Welcome to serverA <h>[root@localhost conf]# curl 192.168.233.70 <h>Welcome to serverB<h/>[root@localhost conf]# curl 192.168.233.70 <h>Welcome to serverB<h/>[root@localhost conf]# curl 192.168.233.70 <h>Welcome to serverB<h/>[root@localhost conf]# curl 192.168.233.70 <h>Welcome to serverA <h>[root@localhost conf]# curl 192.168.233.70 <h>Welcome to serverB<h/>從上面結果可以看出,一共測試訪問10次用戶請求,分流至serverB服務器上的有7次,分流至serverA服務器上的有3次,表明權重配置生效
總結
以上是生活随笔為你收集整理的超详细Linux -- nginx负载均衡配置的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: 【html】盒模型div,边框borde
- 下一篇: 小马哥---山寨仿苹果6 主板型号E2