Nginx七層負載均衡

2021-03-03    分類: 網(wǎng)站建設(shè)

Nginx負載均衡基本概述

為什么要使用負載均衡

當我們的Web服務(wù)器直接面向用戶,往往要承載大量并發(fā)請求,單臺服務(wù)器難以負荷,我使用多臺Web服務(wù)器組成集群,前端使用Nginx負載均衡,將請求分散的打到我們的后端服務(wù)器集群中,實現(xiàn)負載的分發(fā)。那么會大大提升系統(tǒng)的吞吐率、請求性能、高容災

?

往往我們接觸的最多的是SLB(Server Load Balance)負載均衡,實現(xiàn)最多的也是SLB、那么SLB它的調(diào)度節(jié)點和服務(wù)節(jié)點通常是在一個地域里面。那么它在這個小的邏輯地域里面決定了他對部分服務(wù)的實時性、響應性是非常好的。

所以說當海量用戶請求過來以后,它同樣是請求調(diào)度節(jié)點,調(diào)度節(jié)點將用戶的請求轉(zhuǎn)發(fā)給后端對應的服務(wù)節(jié)點,服務(wù)節(jié)點處理完請求后在轉(zhuǎn)發(fā)給調(diào)度節(jié)點,調(diào)度節(jié)點最后響應給用戶節(jié)點。這樣也能實現(xiàn)一個均衡的作用,那么Nginx則是一個典型的SLB

負載均衡的叫法有很多:

負載均衡

負載

Load Balance

LB

公有云中叫法

SLB 阿里云負載均衡

QLB 青云負載均衡

CLB 騰訊云負載均衡

ULB ucloud負載均衡

常見的負載均衡的軟件

Nginx

Haproxy

LVS

?


負載均衡能實現(xiàn)的應用場景一: 四層負載均衡

所謂四層負載均衡指的是OSI七層模型中的傳輸層,那么傳輸層Nginx已經(jīng)能支持TCP/IP的控制,所以只需要對客戶端的請求進行TCP/IP協(xié)議的包轉(zhuǎn)發(fā)就可以實現(xiàn)負載均衡,那么它的好處是性能非常快、只需要底層進行應用處理,而不需要進行一些復雜的邏輯。

?


負載均衡能實現(xiàn)的應用場景二:七層負載均衡

七層負載均衡它是在應用層,那么它可以完成很多應用方面的協(xié)議請求,比如我們說的http應用的負載均衡,它可以實現(xiàn)http信息的改寫、頭信息的改寫、安全應用規(guī)則控制、URL匹配規(guī)則控制、以及轉(zhuǎn)發(fā)、rewrite等等的規(guī)則,所以在應用層的服務(wù)里面,我們可以做的內(nèi)容就更多,那么Nginx則是一個典型的七層負載均衡SLB

?


四層負載均衡與七層負載均衡區(qū)別

四層負載均衡數(shù)據(jù)包在底層就進行了分發(fā),而七層負載均衡數(shù)據(jù)包則是在最頂層進行分發(fā)、由此可以看出,七層負載均衡效率沒有四負載均衡高。

但七層負載均衡更貼近于服務(wù),如:http協(xié)議就是七層協(xié)議,我們可以用Nginx可以作會話保持,URL路徑規(guī)則匹配、head頭改寫等等,這些是四層負載均衡無法實現(xiàn)的。

注意:四層負載均衡不識別域名,七層負載均衡識別域名

Nginx負載均衡配置場景

Nginx要實現(xiàn)負載均衡需要用到proxy_pass代理模塊配置.

Nginx負載均衡與Nginx代理不同地方在于,Nginx的一個location僅能代理一臺服務(wù)器,而Nginx負載均衡則是將客戶端請求代理轉(zhuǎn)發(fā)至一組upstream虛擬服務(wù)池.

?


Nginx upstream虛擬配置語法

Syntax: upstream name { ... }
Default: -
Context: http
 
#upstream例
upstream backend {
 server backend1.example.com weight=5;
 server backend2.example.com:8080;
 server unix:/tmp/backend3;
 server backup1.example.com:8080 backup;
}
server {
 location / {
 proxy_pass http://backend;
 }
}

1.環(huán)境準備角色外網(wǎng)IP(NAT)內(nèi)網(wǎng)IP(LAN)主機名LB01eth0:10.0.0.5eth1:172.16.1.5lb01web01eth0:10.0.0.7eth1:172.16.1.7web01web02eth0:10.0.0.8eth1:172.16.1.8web02

2.Web01服務(wù)器上配置nginx

[root@web01 ~]# cd /etc/nginx/conf.d/
[root@web01 conf.d]# cat node.conf 
server {
 listen 80;
 server_name node.drz.com;
 location / {
 root /node;
 index index.html;
 }
}
[root@web01 conf.d]# mkdir /node
[root@web01 conf.d]# echo "Web01..." > /node/index.html
[root@web01 conf.d]# systemctl restart nginx

3.Web02服務(wù)器上配置nginx

[root@web02 ~]# cd /etc/nginx/conf.d/
[root@web02 conf.d]# cat node.conf 
server {
 listen 80;
 server_name node.drz.com;
 location / {
 root /node;
 index index.html;
 }
}
[root@web02 conf.d]# mkdir /node
[root@web02 conf.d]# echo "Web02..." > /node/index.html
[root@web02 conf.d]# systemctl restart nginx

4.配置Nginx負載均衡

[root@lb01 ~]# cd /etc/nginx/conf.d/
[root@lb01 conf.d]# cat node_proxy.conf 
upstream node {
 server 172.16.1.7:80;
 server 172.16.1.8:80;
}
server {
 listen 80;
 server_name node.drz.com;
 
 location / {
 proxy_pass http://node;
 include proxy_params;
 }
}
[root@lb01 conf.d]# systemctl restart nginx

5.準備Nginx負載均衡調(diào)度使用的proxy_params

[root@Nginx ~]# vim /etc/nginx/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;

打開瀏覽器訪問:http://node.drz.com

?

?


負載均衡常見典型故障

如果后臺服務(wù)連接超時,Nginx是本身是有機制的,如果出現(xiàn)一個節(jié)點down掉的時候,Nginx會更據(jù)你具體負載均衡的設(shè)置,將請求轉(zhuǎn)移到其他的節(jié)點上,但是,如果后臺服務(wù)連接沒有down掉,但是返回錯誤異常碼了如:504、502、500,這個時候你需要加一個負載均衡的設(shè)置,如下:proxy_next_upstream http_500 | http_502 | http_503 | http_504 |http_404;意思是,當其中一臺返回錯誤碼404,500...等錯誤時,可以分配到下一臺服務(wù)器程序繼續(xù)處理,提高平臺訪問成功率。

server {
 listen 80;
 server_name www.driverzeng.com;
 location / {
 proxy_pass http://node;
 proxy_next_upstream error timeout http_500 http_502 http_503 http_504;
 }
}

Nginx負載均衡調(diào)度算法

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

Nginx負載均衡[rr]輪詢具體配置

upstream load_pass {
 server 10.0.0.7:80;
 server 10.0.0.8:80;
}

Nginx負載均衡[wrr]權(quán)重輪詢具體配置

upstream load_pass {
 server 10.0.0.7:80 weight=5;
 server 10.0.0.8:80;
}

Nginx負載均衡ip_hash

具體配置不能和weight一起使用。

#如果客戶端都走相同代理, 會導致某一臺服務(wù)器連接過多
upstream load_pass {
 ip_hash;
 server 10.0.0.7:80 weight=5;
 server 10.0.0.8:80;
}

Nginx負載均衡后端狀態(tài)

后端Web服務(wù)器在前端Nginx負載均衡調(diào)度中的狀態(tài)

狀態(tài)概述down當前的server暫時不參與負載均衡backup預留的備份服務(wù)器max_fails允許請求失敗的次數(shù)fail_timeout經(jīng)過max_fails失敗后, 服務(wù)暫停時間max_conns限制大的接收連接數(shù)

測試down狀態(tài)測試該Server不參與負載均衡的調(diào)度

upstream load_pass {
 #不參與任何調(diào)度, 一般用于停機維護
 server 10.0.0.7:80 down;
}

測試backup以及down狀態(tài)

upstream load_pass {
 server 10.0.0.7:80 down;
 server 10.0.0.8:80 backup;
 server 10.0.0.9:80 max_fails=1 fail_timeout=10s;
}
 
location / {
 proxy_pass http://load_pass;
 include proxy_params;
}

測試max_fails失敗次數(shù)和fail_timeout多少時間內(nèi)失敗多少次則標記down

upstream load_pass {
 server 10.0.0.7:80;
 server 10.0.0.8:80 max_fails=2 fail_timeout=10s;
}

測試max_conns大TCP連接數(shù)

upstream load_pass {
 server 10.0.0.7:80;
 server 10.0.0.8:80 max_conns=1;
}

Nginx負載均衡健康檢查

在Nginx官方模塊提供的模塊中,沒有對負載均衡后端節(jié)點的健康檢查模塊,但可以使用第三方模塊。

nginx_upstream_check_module來檢測后端服務(wù)的健康狀態(tài)。

第三方模塊項目地址:TP

1.安裝依賴包

[root@lb02 ~]# yum install -y gcc glibc gcc-c++ pcre-devel openssl-devel patch

2.下載nginx源碼包以及nginx_upstream_check模塊第三方模塊

[root@lb02 ~]# wget http://nginx.org/download/nginx-1.14.2.tar.gz
[root@lb02 ~]# wget https://github.com/yaoweibin/nginx_upstream_check_module/archive/master.zip

3.解壓nginx源碼包以及第三方模塊

[root@lb02 ~]# tar xf nginx-1.14.2.tar.gz
[root@lb02 ~]# unzip master.zip

4.進入nginx目錄,打補丁(nginx的版本是1.14補丁就選擇1.14的,p1代表在nginx目錄,p0是不在nginx目錄)

[root@lb02 ~]# cd nginx-1.14.2/
[root@lb02 nginx-1.14.2]# patch -p1 <../nginx_upstream_check_module-master/check_1.14.0+.patch
./configure --prefix=/etc/nginx --sbin-path=/usr/sbin/nginx --modules-path=/usr/lib64/nginx/modules --conf-path=/etc/nginx/nginx.conf --error-log-path=/var/log/nginx/error.log --http-log-path=/var/log/nginx/access.log --pid-path=/var/run/nginx.pid --lock-path=/var/run/nginx.lock --http-client-body-temp-path=/var/cache/nginx/client_temp --http-proxy-temp-path=/var/cache/nginx/proxy_temp --http-fastcgi-temp-path=/var/cache/nginx/fastcgi_temp --http-uwsgi-temp-path=/var/cache/nginx/uwsgi_temp --http-scgi-temp-path=/var/cache/nginx/scgi_temp --user=nginx --group=nginx --with-compat --with-file-aio --with-threads --with-http_addition_module --with-http_auth_request_module --with-http_dav_module --with-http_flv_module --with-http_gunzip_module --with-http_gzip_static_module --with-http_mp4_module --with-http_random_index_module --with-http_realip_module --with-http_secure_link_module --with-http_slice_module --with-http_ssl_module --with-http_stub_status_module --with-http_sub_module --with-http_v2_module --with-mail --with-mail_ssl_module --with-stream --with-stream_realip_module --with-stream_ssl_module --with-stream_ssl_preread_module --add-module=/root/nginx_upstream_check_module-master --with-cc-opt='-O2 -g -pipe -Wall -Wp,-D_FORTIFY_SOURCE=2 -fexceptions -fstack-protector-strong --param=ssp-buffer-size=4 -grecord-gcc-switches -m64 -mtune=generic -fPIC' --with-ld-opt='-Wl,-z,relro -Wl,-z,now -pie'
[root@lb02 nginx-1.14.2]# make && make install

5.在已有的負載均衡上增加健康檢查的功能

[root@lb01 conf.d]# cat proxy_web.conf
upstream web {
 server 172.16.1.7:80 max_fails=2 fail_timeout=10s;
 server 172.16.1.8:80 max_fails=2 fail_timeout=10s;
 check interval=3000 rise=2 fall=3 timeout=1000 type=tcp;
 #interval 檢測間隔時間,單位為毫秒
 #rise 表示請求2次正常,標記此后端的狀態(tài)為up
 #fall 表示請求3次失敗,標記此后端的狀態(tài)為down
 #type 類型為tcp
 #timeout 超時時間,單位為毫秒
}
server {
 listen 80;
 server_name web.drz.com;
 location / {
 proxy_pass http://web;
 include proxy_params;
 }
 location /upstream_check {
 check_status;
 }
}

Nginx負載均衡會話保持

在使用負載均衡的時候會遇到會話保持的問題,可通過如下方式進行解決。

1.使用nginx的ip_hash,根據(jù)客戶端的IP,將請求分配到對應的IP上

2.基于服務(wù)端的session會話共享(NFS,MySQL,memcache,redis,file)

在解決負載均衡繪畫問題,我們需要了解session和cookie的區(qū)別。

瀏覽器端存的是cookie每次瀏覽器發(fā)請求到服務(wù)端時,報文頭是會自動添加cookie信息的。

服務(wù)端會查詢用戶的cookie作為key去存儲里找對應的value(session)

同意域名下的網(wǎng)站的cookie都是一樣的,所以無論幾臺服務(wù)器,無論請求分配到哪一臺服務(wù)器上同一用戶的cookie是不變的。也就是說cookie對應的session也是唯一的。所以,這里只要保證多臺業(yè)務(wù)服務(wù)器訪問同一個共享存儲服務(wù)器(NFS,MySQL,memcache,redis,file)就行了。

1.配置Nginx

[root@web01 conf.d]# cat php.conf
server {
 listen 80;
 server_name php.drz.com;
 root /code/phpMyAdmin-4.8.4-all-languages;
 location / {
 index index.php index.html;
 }
 location ~ \.php$ {
 fastcgi_pass 127.0.0.1:9000;
 fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
 include fastcgi_params;
 }
}
[root@web01 conf.d]# systemctl restart nginx

2.安裝phpmyadmin (web01和web02上都裝)

[root@web01 conf.d]# cd /code
[root@web01 code]# wget https://files.phpmyadmin.net/phpMyAdmin/4.8.4/phpMyAdmin-4.8.4-all-languages.zip
[root@web01 code]# unzip phpMyAdmin-4.8.4-all-languages.zip

3.配置phpmyadmin連接遠程的數(shù)據(jù)庫

[root@web01 code]# cd phpMyAdmin-4.8.4-all-languages/
[root@web01 phpMyAdmin-4.8.4-all-languages]# cp config.sample.inc.php config.inc.php
[root@web01 phpMyAdmin-4.8.4-all-languages]# vim config.inc.php
/* Server parameters */
$cfg['Servers'][$i]['host'] = '172.16.1.51';

4.配置授權(quán)

[root@web01 conf.d]# chown -R www.www /var/lib/php/

使用瀏覽器訪問頁面,獲取cookie信息

?

?

[root@web01 phpMyAdmin-4.8.4-all-languages]# ll /var/lib/php/session/
總用量 4
-rw-------. 1 www www 2424 8月 21 18:41 sess_e96b27a6a628be47745a10a36e2fcd5a

?

5.將web01上配置好的phpmyadmin以及nginx的配置文件推送到web02主機

[root@web01 code]# scp -rp phpMyAdmin-4.8.4-all-languages root@172.16.1.8:/code/
[root@web01 code]# scp /etc/nginx/conf.d/php.conf root@172.16.1.8:/etc/nginx/conf.d/

6.在web02上重載Nginx服務(wù)

[root@web02 code]# systemctl restart nginx

7.授權(quán)

[root@web02 code]# chown -R www.www /var/lib/php/

8.接入負載均衡

[root@lb01 conf.d]# vim proxy_php.com.conf 
upstream php {
 server 172.16.1.7:80;
 server 172.16.1.8:80;
}
server {
 listen 80;
 server_name php.drz.com;
 location / {
 proxy_pass http://php;
 include proxy_params;
 }
}
[root@lb01 conf.d]# nginx -t
nginx: the configuration file /etc/nginx/nginx.conf syntax is ok
nginx: configuration file /etc/nginx/nginx.conf test is successful
[root@lb01 conf.d]# systemctl restart nginx

使用負載均衡的輪詢功能之后,會發(fā)現(xiàn),如果將session保存在本地文件的話,永遠都登錄不上去。drz.com


使用redis解決會話登錄問題

1.安裝redis內(nèi)存數(shù)據(jù)庫

[root@db01 ~]# yum install redis -y

2.配置redis監(jiān)聽在172.16.1.0網(wǎng)段上

[root@db01 ~]# sed -i '/^bind/c bind 127.0.0.1 172.16.1.51' /etc/redis.conf

3.啟動redis

[root@db01 ~]# systemctl start redis
[root@db01 ~]# systemctl enable redis

4.php配置session連接redis

#1.修改/etc/php.ini文件
[root@web ~]# vim /etc/php.ini
session.save_handler = redis
session.save_path = "tcp://172.16.1.51:6379"
;session.save_path = "tcp://172.16.1.51:6379?auth=123" #如果redis存在密碼,則使用該方式
session.auto_start = 1
#2.注釋php-fpm.d/www.conf里面的兩條內(nèi)容,否則session內(nèi)容會一直寫入/var/lib/php/session目錄中
;php_value[session.save_handler] = files
;php_value[session.save_path] = /var/lib/php/session

5.重啟php-fpm

[root@web01 code]# systemctl restart php-fpm

6.將web01上配置好的文件推送到web02

[root@web01 code]# scp /etc/php.ini root@172.16.1.8:/etc/php.ini 
[root@web01 code]# scp /etc/php-fpm.d/www.conf root@172.16.1.8:/etc/php-fpm.d/www.conf 

5.上web02上重啟php-fpm

[root@web02 code]# systemctl restart php-fpm

6.redis查看數(shù)據(jù)

[root@db01 redis]# redis-cli
127.0.0.1:6379> keys *
1) "PHPREDIS_SESSION:1365eaf0490be9315496cb7382965954"

?

收藏
?
舉報

當前標題:Nginx七層負載均衡
當前URL:http://www.muchs.cn/news39/103989.html

成都網(wǎng)站建設(shè)公司_創(chuàng)新互聯(lián),為您提供網(wǎng)站營銷網(wǎng)站制作、App開發(fā)、微信公眾號、靜態(tài)網(wǎng)站、Google

廣告

聲明:本網(wǎng)站發(fā)布的內(nèi)容(圖片、視頻和文字)以用戶投稿、用戶轉(zhuǎn)載內(nèi)容為主,如果涉及侵權(quán)請盡快告知,我們將會在第一時間刪除。文章觀點不代表本網(wǎng)站立場,如需處理請聯(lián)系客服。電話:028-86922220;郵箱:631063699@qq.com。內(nèi)容未經(jīng)允許不得轉(zhuǎn)載,或轉(zhuǎn)載時需注明來源: 創(chuàng)新互聯(lián)

小程序開發(fā)