Nginx負載均衡概述


Nginx負載均衡概述

Web服務器,直接面向用戶,往往要承載大量並發請求,單台服務器難以負荷,我使用多台WEB服務器組成集群,前端使用Nginx負載均衡,將請求分散的打到我們的后端服務器集群中,
實現負載的分發。那么會大大提升系統的吞吐率、請求性能、高容災

 

Nginx要實現負載均衡需要用到proxy_pass代理模塊配置

Nginx負載均衡與Nginx代理不同地方在於

Nginx代理僅代理一台服務器,而Nginx負載均衡則是將客戶端請求代理轉發至一組upstream虛擬服務池

Nginx可以配置代理多台服務器,當一台服務器宕機之后,仍能保持系統可用。

 upstream配置

在nginx.conf > http 區域中

upstream django {
       server 10.0.0.10:8000;
       server 10.0.0.11:9000;
}

在nginx.conf > http 區域 >  server區域  > location配置中

添加proxy_pass

location / {
            root   html;
            index  index.html index.htm;
            proxy_pass http://django;
}

此時初步負載均衡已經完成,upstream默認按照輪訓方式負載,每個請求按時間順序逐一分配到后端節點。

upstream分配策略

weight 權重

upstream django {
       server 10.0.0.10:8000 weight=5;
       server 10.0.0.11:9000 weight=10;#這個節點訪問比率是大於8000的
}

ip_hash

復制代碼
每個請求按訪問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.oldboy.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

一、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
復制代碼

二、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
復制代碼

三、配置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文件,文件中存放代理的請求頭相關參數

復制代碼
[root@lb01 conf]# cat /opt/nginx/conf/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

四、訪問lb01節點nginx,反復刷新

五、nginx負載均衡調度算法

復制代碼
調度算法      概述
輪詢        按時間順序逐一分配到不同的后端服務器(默認)
weight       加權輪詢,weight值越大,分配到的訪問幾率越高
ip_hash      每個請求按訪問IP的hash結果分配,這樣來自同一IP的固定訪問一個后端服務器
url_hash      按照訪問URL的hash結果來分配請求,是每個URL定向到同一個后端服務器
least_conn    最少鏈接數,那個機器鏈接數少就分發
復制代碼

1.輪詢(不做配置,默認輪詢)

2.weight權重(優先級)

3.ip_hash配置,根據客戶端ip哈希分配,不能和weight一起用

六、nginx動靜分離負載均衡

 

環境准備 

 

系統                 服務                軟件                ip地址
centos7(lb01)                負載均衡            nginx proxy        192.168.119.10
centos7(web01)                靜態資源            nginx靜態資源        192.168.119.11
centos7(web02)                動態資源            django            192.168.119.12

 

一、在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
復制代碼

二、在web02配置動態請求,准備一個flask程序和靜態資源轉發

復制代碼
cat  nginx.conf

#靜態資源地址
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 &

三、在負載均衡服務器lb01上測試訪問192.168.119.10


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM