13@nginx負載均衡 文章目錄
- 一、為什麼要用負載均衡
- 二、常見的負載均衡
- 三、負載均衡的常用軟體
- 四、負載均衡類型
- 五、Nginx負載均衡配置
- 1、負載均衡配置文法 ngx_http_upstream_module
- 2、環境準備
- 3、準備站點
- 4、負載均衡配置檔案
- 5、優化配置檔案
- 6、nginx負載均衡常見問題
- 1.輪詢配置方法
- 2.權重輪詢配置方法
- 3.ip_hash的配置方法
- 1.down狀态配置測試
- 2.backup狀态配置測試
- 3.通路錯誤狀态
- 4.max_conns限制最大連接配接數狀态配置
Nginx負載均衡
#注意:代理隻能代理一台機器
nginx做代理,一個location可以做一個代理
一、為什麼要用負載均衡
當我們的Web伺服器直接面向使用者,往往要承載大量并發請求,單台伺服器難以負荷,我使用多台Web伺服器組成叢集,前端使用Nginx負載均衡,将請求分散的打到我們的後端伺服器叢集中,實作負載的分發。那麼會大大提升系統的吞吐率、請求性能、高容災
往往我們接觸的最多的是SLB(Server Load Balance)負載均衡,實作最多的也是SLB、那麼SLB它的排程節點和服務節點通常是在一個地域裡面。那麼它在這個小的邏輯地域裡面決定了他對部分服務的實時性、響應性是非常好的。
是以說當海量使用者請求過來以後,它同樣是請求排程節點,排程節點将使用者的請求轉發給後端對應的服務節點,服務節點處理完請求後在轉發給排程節點,排程節點最後響應給使用者節點。這樣也能實作一個均衡的作用,那麼Nginx則是一個典型的SLB
二、常見的負載均衡
SLB 阿裡雲負載均衡
LB 青雲負載均衡
CLB 騰訊雲負載均衡
ULB ucloud負載均衡
#負載均衡的稱呼
三、負載均衡的常用軟體
Nginx #在1.9版本之前隻能做七層,1.9版本之後既能做七層,也能做四層負載均衡
Haproxy #既能做四層,也能做七層負載均衡
LVS #隻能做四層負載均衡
#LVS是最快的負載均衡軟體,其他兩個軟體需要将請求發送到服務再轉發到後端,LVS不用到服務,它相當于将伺服器變成了負載均衡,直接轉發請求
四、負載均衡類型
#四層負載均衡
所謂四層負載均衡指的是OSI七層模型中的傳輸層,那麼傳輸層Nginx已經能支援TCP/IP的控制,是以隻需要對用戶端的請求進行TCP/IP協定的包轉發就可以實作負載均衡,那麼它的好處是性能非常快、隻需要底層進行應用處理,而不需要進行一些複雜的邏輯。
#七層負載均衡
七層負載均衡它是在應用層,那麼它可以完成很多應用方面的協定請求,比如我們說的http應用的負載均衡,它可以實作http資訊的改寫、頭資訊的改寫、安全應用規則控制、URL比對規則控制、以及轉發、rewrite等等的規則,是以在應用層的服務裡面,我們可以做的内容就更多,那麼Nginx則是一個典型的七層負載均衡SLB
#四層負載與七層負載差別
五、Nginx負載均衡配置
#Nginx要實作負載均衡需要用到proxy_pass代理子產品配置.
1、負載均衡配置文法 ngx_http_upstream_module
Syntax: upstream name { ... }
Default: —
Context: http
upstream backend {
server backend1.example.com;
server backend2.example.com;
}
server {
listen 80;
server_name www.linux.com
location / {
proxy_pass http://backend;
}
}
2、環境準備
主機 | 外網ip | 内網ip | 身份 |
lb01 | 10.0.0.4 | 172.16.1.4 | 負載均衡 |
web01 | 10.0.0.7 | 172.16.1.7 | web |
web02 | 10.0.0.8 | 172.16.1.8 | web |
3、準備站點
#web01準備
[root@web01 conf.d]# vim node.conf
server {
listen 80;
server_name node.linux.com;
location / {
root /code/node;
inde index.html;
}
}
[root@web01 conf.d]# mkdir /code/node
[root@web01 conf.d]# echo "我是web01......" > /code/node/index.html
[root@web01 conf.d]# systemctl restart nginx
#web02準備
[root@web02 conf.d]# vim node.conf
server {
charset 'utf-8';
listen 80;
server_name node.linux.com;
location / {
root /code/node;
index index.html;
}
}
[root@web02 conf.d]# mkdir /code/node
[root@web02 conf.d]# echo "我是web02......" > /code/node/index.html
[root@web02 conf.d]# systemctl restart nginx
4、負載均衡配置檔案
[root@lb01 conf.d]# vim node_proxy.conf
upstream node { #配置連結池
server 172.16.1.7:80;
server 172.16.1.8:80;
}
server {
listen 80;
server_name node.linux.com;
location / {
proxy_pass http://node;
include proxy_params;
}
}
#檢查include檔案是否存在
[root@lb01 conf.d]# ll /etc/nginx/proxy_params
-rw-r--r-- 1 root root 275 Feb 28 15:24 /etc/nginx/proxy_params
[root@lb01 conf.d]# systemctl restart nginx
5、優化配置檔案
[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;
6、nginx負載均衡常見問題
#如下:
proxy_next_upstream http_500 | http_502 | http_503 | http_504 |http_404;
#當其中一台傳回錯誤碼404,500...等錯誤時,可以配置設定到下一台伺服器程式繼續處理,提高平台通路成功率
#配置檔案
upstream test {
server 172.16.1.7:80;
server 172.16.1.8:80;
server 172.16.1.9:80;
}
server {
listen 80;
server_name linux.slb.cluster.local.com;
location / {
proxy_pass http://test;
include proxy_params;
proxy_next_upstream error timeout http_500 http_502 http_503 http_504 http_404 http_403;
}
}
六、nginx負載均衡排程算法
排程算法 | 概述 |
輪詢 | 按時間順序逐一配置設定到不同的後端伺服器(預設) |
weight | 權重輪詢,weight值越大,配置設定到的通路幾率越高 |
ip_hash | 每個請求按通路IP的hash結果配置設定,這樣來自同一IP的固定通路一個後端伺服器 |
url_hash | 按照通路URL的hash結果來配置設定請求,是每個URL定向到同一個後端伺服器 |
least_conn | 最少連結數,那個機器連結數少就分發 |
# 權重,權值越小,通路機率越低,反之則越高
server 172.16.1.7:80 weight=1;
server 172.16.1.8:80 weight=5;
server 172.16.1.9:80 weight=100;
1.輪詢配置方法
upstream node {
server 172.16.1.7:80;
server 172.16.1.8:80;
}
2.權重輪詢配置方法
#通路根據配置的權重比例進行配置設定
upstream node {
server 172.16.1.7:80 weight=5;
server 172.16.1.8:80 weight=1;
}
3.ip_hash的配置方法
#根據通路的來源IP配置設定至同一台伺服器
upstream node {
server 172.16.1.7:80;
server 172.16.1.8:80;
ip_hash;
}
#經常使用這種方式進行會話保持
七、nginx負載均衡狀态
狀态 | 概述 |
down | 目前的server暫時不參與負載均衡 |
backup | 預留的備份伺服器 |
max_fails | 允許請求失敗的次數 |
fail_timeout | 經過max_fails失敗後, 服務暫停時間 |
1.down狀态配置測試
upstream node {
#不參與負載均衡任何排程,一般停機維護或者上線的時候使用
server 172.16.1.7:80 down;
server 172.16.1.8:80;
}
2.backup狀态配置測試
upstream node {
server 172.16.1.7:80;
server 172.16.1.8:80 backup;
}
3.通路錯誤狀态
upstream node {
server 172.16.1.7:80;
server 172.16.1.8:80 max_fails=3 fail_timeout 10s;
}
4.max_conns限制最大連接配接數狀态配置
upstream node {
server 172.16.1.7:80;
server 172.16.1.8:80 max_conns=120;
}
八、負載均衡nginx健康檢測子產品
#安裝需要的軟體
[root@lb02 ~]# yum install -y gcc glibc gcc-c++ pcre-devel openssl-devel patch
#下載下傳源件包
[root@lb02 ~]#wget http://nginx.org/download/nginx-1.16.1.tar.gz
[root@lb02~]# wget https://github.com/yaoweibin/nginx_upstream_check_module/archive/master.zip
[root@lb02~]# tar xf nginx-1.16.1.tar.gz
[root@lb02~]# unzip master.zip
[root@lb02~]# cd nginx-1.16.1/
# p1:在nginx目錄内 p0不在nginx目錄内
[root@lb02 nginx-1.16.1]# patch -p1 <../nginx_upstream_check_module-master/check_1.16.1+.patch
[root@lb02 nginx-1.16.1]#./configure --prefix=/data/nginx --sbin-path=/usr/sbin/nginx --modules-path=/usr/lib64/nginx/modules --conf-path=/data/nginx/nginx.conf --error-log-path=/data/log/nginx/error.log --http-log-path=/data/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.16.1]# make -j && make install
[root@lb02 nginx-1.16.1]# vim /etc/nginx/conf.d/proxy.conf
upstream test {
server 172.16.1.7:80;
server 172.16.1.8:80;
server 172.16.1.9:80;
check interval=3000 rise=1 fall=2 timeout=1000;
}
server {
listen 80;
server_name linux.slb.cluster.local.com;
location / {
proxy_pass http://test;
# proxy_next_upstream error timeout http_500 http_502 http_503 http_504 http_404 http_403;
include proxy_params;
}
location /upstream_check {
check_status;
}
}
interval:檢測間隔
rise:重試測試
fall:錯誤次數
timeout:逾時時間
[root@lb02 nginx]# cat /etc/nginx/conf.d/test.conf
upstream test {
server 172.16.1.7:80;
server 172.16.1.8:80;
server 172.16.1.9:80;
check interval=3000 rise=1 fall=2 timeout=1000;
}
server {
listen 80;
server_name linux.slb.cluster.local.com;
location / {
proxy_pass http://test;
# proxy_next_upstream error timeout http_500 http_502 http_503 http_504 http_404 http_403;
include proxy_params;
}
location /upstream_check {
check_status;
}
}