nginx負載均衡實驗
Nginx負載均衡概述
Web服務器,直接面向用戶,每每要承載大量併發請求,單臺服務器難以負荷,我使用多臺WEB服務器組成集羣,前端使用Nginx負載均衡,將請求分散的打到咱們的後端服務器集羣中,
實現負載的分發。那麼會大大提高系統的吞吐率、請求性能、高容災
Nginx要實現負載均衡須要用到proxy_pass代理模塊配置html
Nginx負載均衡與Nginx代理不一樣地方在於前端
Nginx代理僅代理一臺服務器,而Nginx負載均衡則是將客戶端請求代理轉發至一組upstream虛擬服務池node
Nginx能夠配置代理多臺服務器,當一臺服務器宕機以後,仍能保持系統可用。python
upstream配置
在nginx.conf > http 區域中linux
upstream django { # django爲service要配置的proxy_pass
server 10.0.0.10:8000;
server 10.0.0.11:9000;
}
在nginx.conf > http 區域 > server區域 > location配置中nginx
添加proxy_passweb
location / { root html; index index.html index.htm; proxy_pass http://django; }
此時初步負載均衡已經完成,upstream默認按照輪訓方式負載,每一個請求按時間順序逐一分配到後端節點。算法
upstream分配策略
weight 權重django
upstream django { server 10.0.0.10:8000 weight=5; server 10.0.0.11:9000 weight=10;#這個節點訪問比率是大於8000的 }
ip_hashflask
每一個請求按訪問ip的hash結果分配,這樣每一個訪客固定訪問一個後端服務器
upstream django {
ip_hash;
server 10.0.0.10:8000; server 10.0.0.11:9000; }
backup
在非backup機器繁忙或者宕機時,請求backup機器,所以機器默認壓力最小
upstream django { server 10.0.0.10:8000 weight=5; server 10.0.0.11:9000; server node.haiyan.com:8080 backup; }
負載均衡實驗環境規劃
角色 ip 主機名 lb01 192.168.119.10 lb01 web01 192.168.119.11 web01 web02 192.168.119.12 web02
關閉防火牆
iptables -F sed -i 's/enforcing/disabled/' /etc/selinux/config systemctl stop firewalld systemctl disable firewalld
1、web01服務器配置nginx,建立index.html
server { listen 80; server_name 192.168.119.11; location / { root /node; index index.html index.htm; } }
mkdir /node
echo 'i am web01' > /node/index.html
#啓動NGINX
./sbgin/nginx
2、web01服務器配置nginx,建立index.html
server { listen 80; server_name 192.168.119.12; location / { root /node; index index.html index.htm; }
mkdir /node
echo 'i am web02...' > /node/index.html
#啓動nginx
./sbing/nginx
3、配置lb01服務器的nginx負載均衡
1.檢查lb01的 nginx.conf
http { include mime.types; default_type application/octet-stream; sendfile on; keepalive_timeout 65; upstream node { server 192.168.119.11:80; server 192.168.119.12:80; } server { listen 80; server_name 192.168.119.10; location / { proxy_pass http://node; include proxy_params; #須要手動建立 } } }
2.手動建立proxy_params文件,文件中存放代理的請求頭相關參數
proxy_set_header Host $http_host; proxy_set_header X-Real-IP $remote_addr; proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for; proxy_connect_timeout 30; proxy_send_timeout 60; proxy_read_timeout 60; proxy_buffering on; proxy_buffer_size 32k; proxy_buffers 4 128k;
啓動lb01負載均衡nginx服務
./sbin/nginx
4、訪問lb01節點nginx,反覆刷新
5、nginx負載均衡調度算法
調度算法 概述
輪詢 按時間順序逐一分配到不一樣的後端服務器(默認)
weight 加權輪詢,weight值越大,分配到的訪問概率越高
ip_hash 每一個請求按訪問IP的hash結果分配,這樣來自同一IP的固定訪問一個後端服務器
url_hash 按照訪問URL的hash結果來分配請求,是每一個URL定向到同一個後端服務器
least_conn 最少連接數,那個機器連接數少就分發
1.輪詢(不作配置,默認輪詢)
2.weight權重(優先級)
3.ip_hash配置,根據客戶端ip哈希分配,不能和weight一塊兒用
6、nginx動靜分離負載均衡
環境準備
系統 服務 軟件 ip地址 centos7(lb01) 負載均衡 nginx proxy 192.168.119.10 centos7(web01) 靜態資源 nginx靜態資源 192.168.119.11 centos7(web02) 動態資源 django 192.168.119.12
1、在web01機器上,配置靜態資源,圖片等
cat nginx.conf server { listen 80; server_name 192.168.119.11; #定義網頁根目錄 root /code; #定義了靜態資源 index index.html; #域名匹配,全部的png、jpg、gif請求資源,都去/root/code/images底下找 location ~* .*\.(png|jpg|gif)$ { root /code/images; }
#重啓nginx
./sbin/nginx
#建立目錄 mkdir -p /code/images #準備首頁文件 [root@web01 /code]$cat index.html static files... #準備靜態文件,圖片 [root@web01 /code/images]$wget http://pythonav.cn/av/girlone.jpg^C [root@web01 /code/images]$ls girlone.jpg
2、在web02配置動態請求,準備一個flask程序和靜態資源轉發
#靜態資源地址 upstream static { server 192.168.119.11:80; }
#flask動態請求 upstream flask { server 192.168.119.12:8080; }
server { listen 80; server_name 192.168.119.12;
#當請求到達192.168.119.12:80/時,轉發給flask的8080應用 location / { proxy_pass http://flask; include proxy_params; }
#當判斷資源請求是 192.168.119.12/girl.jpg時候,轉發請求給static地址池的服務器192.168.119.11/ location ~ .*\.(png|jpg|gif)$ { proxy_pass http://static; include proxy_params; }
準備flask應用,flask.py
from flask import Flask app=Flask(__name__) @app.route('/') def hello(): return "i am flask....from nginx" if __name__=="__main__": app.run(host='0.0.0.0',port=8080)
後臺運行flask程序
python flask-web.py &