解决方案

【运维知识进阶篇】集群架构-Nginx七层负载均衡详解

seo靠我 2023-09-24 00:32:05

为什么要使用负载均衡

当我们的Web服务器直接面向用户,往往要承载大量并发请求,单台服务器难以负荷,我使用多台Web服务器组成集群,前端使用Nginx负载均衡,将请求分散的打到我们的后端服务器集群中,实SEO靠我现负载的分发。那么会大大提升系统的吞吐率、请求性能。

负载均衡简单介绍

我们将负载均衡称为SLB(Server Load Balance),Nginx就是SLB的一种,负载均衡的叫法有很多,还可以叫负载、SEO靠我Load Balance、LB,公有云中叫SLB(阿里云负载均衡)、QLB(青云负载均衡)、CLB(腾讯云负载均衡)、ULB(ucloud负载均衡),常见的负载均衡软件有Nginx、Haproxy、LSEO靠我VS,Nginx是七层负载,可以造伪四层,Haproxy是七层负载、LVS是四层负载。

负载均衡分七层和四层,七层从下到上分别是1.物理层、2.数据链路层、3.网络层、4.传输层、5.会话层、6.表示层SEO靠我、7.应用层,而四层负载均衡仅到传输层,所以说四层负载均衡速度更快,但是七层负载均衡更贴近于服务,http协议就是七层协议,我们可以用Nginx做会话保持、URL路径规则匹配、Head头改写等等,这些SEO靠我四层无法实现。本篇文章我们介绍Nginx的七层负载均衡。

Nginx负载均衡配置语法

Nginx实现负载均衡都需要用proxy_pass代理模块配置

Nginx负载均衡与Nginx代理不同,Nginx的一个SEO靠我location仅能代理一台服务器,而Nginx负载均衡则是将客户端请求代理转发至一组upstream虚拟服务池。

1、我们准备1台LB01作为负载均衡服务器(10.0.0.5;172.16.1.5)、SEO靠我2台Web服务器分别是Web01(10.0.0.7;172.16.1.7)、Web02(10.0.0.8;172.16.1.8)

2、在Web01和Web02分别配置Nginx,并创建测试代码文件

[roSEO靠我ot@Web01 ~]# cat /etc/nginx/conf.d/test.conf server {listen 80;server_name test.koten.com;loSEO靠我cation /{root /code/test;index index.html;} } [root@Web01 ~]# mkdir -p /code/test SEO靠我 [root@Web01 ~]# echo Web01 > /code/test/index.html [root@Web01 ~]# nginx -t ngSEO靠我inx: the configuration file /etc/nginx/nginx.conf syntax is ok nginx: configuration file /etSEO靠我c/nginx/nginx.conf test is successful [root@Web01 ~]# systemctl restart nginx[root@Web02 ~]#SEO靠我 cat /etc/nginx/conf.d/test.conf server {listen 80;server_name test.koten.com;location /{rooSEO靠我t /code/test;index index.html;} } [root@Web02 ~]# mkdir -p /code/test [root@SEO靠我Web02 ~]# echo Web02 > /code/test/index.html [root@Web02 ~]# nginx -t nginx: the conSEO靠我figuration file /etc/nginx/nginx.conf syntax is ok nginx: configuration file /etc/nginx/nginSEO靠我x.conf test is successful [root@Web02 ~]# systemctl restart nginx

3、配置Nginx负载均衡

[root@LB01 ~]#SEO靠我 vim /etc/nginx/conf.d/test_proxy.conf #负载均衡配置文件 upstream node {server 172.16.1.7;server 172SEO靠我.16.1.8; } server {listen 80;server_name test.koten.com;location / {proxy_pass http:SEO靠我//node;include proxy_params;} } ~ ~ ~ [root@LB01 ~]# vim /etSEO靠我c/nginx/proxy_params #负载均衡调用的模块信息 proxy_set_header Host $http_host; proxy_set_headerSEO靠我 X-Real-IP $remote_addr; proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;proxy_cSEO靠我onnect_timeout 30; proxy_send_timeout 60; proxy_read_timeout 60;proxy_buffering on; SEO靠我 proxy_buffer_size 32k; proxy_buffers 4 128k; ~ ~ ~ ~SEO靠我 ~ [root@LB01 ~]# nginx -t nginx: the configuration file /etc/nginx/nginx.coSEO靠我nf syntax is ok nginx: configuration file /etc/nginx/nginx.conf test is successful [SEO靠我root@LB01 ~]# systemctl restart nginx

4、修改本地HOST文件并打开浏览器访问test.koten.com

刷新一下页面,显示Web02,意味着我们已经通过负载均衡访SEO靠我问到了Web02服务器。

Nginx负载均衡常见故障

Nginx负载均衡比较智能,如果一个服务器宕了,会自动访问下一个服务器,但是如果后台服务器没有宕而是返回错误,就需要我们增加一个负载均衡设置了,以保证SEO靠我如果出错能直接访问下一台,而不是返回错误。

proxy_next_upstream error timeout http_500 http_502 http_503 http_504; SEO靠我[root@LB01 ~]# vim /etc/nginx/conf.d/test_proxy.conf upstream node {server 172.16.1.7;serverSEO靠我 172.16.1.8; } server {listen 80;server_name test.koten.com;location / {proxy_pass hSEO靠我ttp://node;include proxy_params;proxy_next_upstream error timeout http_500 http_502 http_ 50SEO靠我3 http_504; #这样如果报这些错误也会自动访问下一个服务器节点} } ~ <inx/conf.d/test_proxy.conf" 14L, SEO靠我274C written

Nginx负载均衡调度算法

这样的负载均衡,每刷新一下就是一次请求,一次请求就变一次访问的服务器,我们把这种负载均衡算法,称为轮询。常见的负载均衡算法有以下几种:

调度算法概述轮询SEO靠我按时间顺序逐一分配到不同的后端服务器(默认)weight加权轮询,weight值越大,分配到的访问几率越高ip_hash每个请求按照访问的IP的hash结果分配,这样来自同一IP的固定访问一个后端服务SEO靠我器url_hash按照访问的URL的hash结果来分配请求,不同的URL定向到不同的后端服务器

least_conn

最少连接数,哪个机器连接数少就分发哪个

Nginx负载均衡轮询配置

upstream loSEO靠我ad_pass {server 10.0.0.7:80;server 10.0.0.8:80; }

 Nginx负载均衡权重轮询配置

upstream load_pass {server 1SEO靠我0.0.0.7:80 weight=5;server 10.0.0.8:80; }

Nginx负载均衡ip_hash配置

upstream load_pass {ip_hash;serveSEO靠我r 10.0.0.7:80 weight=5;server 10.0.0.8:80; }注意:不能和weight一起使用,因为一个IP下不止一台客户端,如果客户端都走相同代理,会导致某SEO靠我一台服务器连接数过多。

Nginx负载均衡后端状态

后端Web服务器在前端Nginx负载均衡调度中的状态

状态概述down当前的服务器不参与负载均衡,一般用于停机维护backup预留的备份服务器max_faSEO靠我ils允许请求失败的次数fail_timeout经过max_fails失败后,服务器暂停时间max_coms限制最大的接收连接数

down测试

[root@LB01 ~]# vim /etc/nginx/SEO靠我conf.d/test_proxy.conf upstream webs {server 172.16.1.7 down;server 172.16.1.8; } SEO靠我 server {listen 80;server_name test.koten.com;location / {proxy_pass http://webs;include proxy_SEO靠我params;proxy_next_upstream error timeout http_500 http_502 http_ 503 http_504;} } SEO靠我 ~ <inx/conf.d/test_proxy.conf" 14L, 279C written [root@LB01 ~]# systemctl restSEO靠我art nginx

浏览器访问test.koten.com刷新只显示Web02了 

backup测试

把Web01设为backup,刷新浏览器,发现只能访问到Web01,Web02作为预留。

[root@LB0SEO靠我1 ~]# vim /etc/nginx/conf.d/test_proxy.conf upstream webs {server 172.16.1.7 backup;server 1SEO靠我72.16.1.8; } server {listen 80;server_name test.koten.com;location / {proxy_pass httSEO靠我p://webs;include proxy_params;proxy_next_upstream error timeout http_500 http_502 http_ 503 SEO靠我http_504;} } ~ <inx/conf.d/test_proxy.conf" 14L, 281C written [root@SEO靠我LB01 ~]# systemctl restart nginx

把Web02down掉,发现刷新会访问Web01,预留的服务器启用。

[root@LB01 ~]# vim /etc/nginx/confSEO靠我.d/test_proxy.conf upstream webs {server 172.16.1.7 backup;server 172.16.1.8 down; }SEO靠我 server {listen 80;server_name test.koten.com;location / {proxy_pass http://webs;include proSEO靠我xy_params;proxy_next_upstream error timeout http_500 http_502 http_ 503 http_504;} }SEO靠我 ~ <inx/conf.d/test_proxy.conf" 14L, 286C written [root@LB01 ~]# systemctl rSEO靠我estart nginx

max_fails失败次数和fail_timeout时间内失败多少次则标记down

[root@LB01 ~]# vim /etc/nginx/conf.d/test_proxySEO靠我.conf upstream webs {server 172.16.1.7 max_fails=2 fail_timeout=10s;server 172.16.1.8; SEO靠我 } server {listen 80;server_name test.koten.com;location / {proxy_pass http://webs;includeSEO靠我 proxy_params;proxy_next_upstream error timeout http_500 http_502 http_ 503 http_504;} SEO靠我 } ~ <inx/conf.d/test_proxy.conf" 14L, 303C written [root@LB01 ~]# systemcSEO靠我tl restart nginx

max_conns最大TCP连接数

[root@LB01 ~]# vim /etc/nginx/conf.d/test_proxy.conf upstreSEO靠我am webs {server 172.16.1.7 max_conns=1;server 172.16.1.8; } server {listen 80;serverSEO靠我_name test.koten.com;location / {proxy_pass http://webs;include proxy_params;proxy_next_upstream errSEO靠我or timeout http_500 http_502 http_ 503 http_504;} } ~ <inx/conf.d/teSEO靠我st_proxy.conf" 14L, 286C written [root@LB01 ~]# systemctl restart nginx

Nginx负载均衡健康检查

我们采用第三方模SEO靠我块nginx_upstream_check_module来检测后端服务的健康状态,我们新克隆一个LB02用来测试,配置yum源,安装上Nginx。

1、安装依赖包

[root@LB02 ~]# yum iSEO靠我nstall -y gcc glibc gcc-c++ pcre-devel openssl-devel patch

 2、下载nginx源码包和nginx_upstream_check模块第三方模块

[rSEO靠我oot@LB02 ~]# wget http://nginx.org/download/nginx-1.22.1.tar.gz [root@LB02 ~]# wget https://SEO靠我github.com/yaoweibin/nginx_upstream_check_module/archive/master.zip

3、解压nginx源码包以及第三方模块

[root@LB02 ~]#SEO靠我 tar xf nginx-1.22.1.tar.gz [root@LB02 ~]# unzip master.zip

4、进入nginx目录,打补丁(nginx的版本是1.22补丁就选SEO靠我择1.22的,或者是相近的版本号,p1代表在nginx目录,p0是不在nginx目录),打好补丁后编译并安装

[root@LB02 ~]# cd nginx-1.22.1/ [root@SEO靠我LB02 nginx-1.22.1]# patch -p1 <../nginx_upstream_check_module-master/check_1.20.1+.patch [roSEO靠我ot@LB02 nginx-1.22.1]# ./configure --prefix=/etc/nginx --sbin-path=/usr/sbin/nginx --modules-path=/uSEO靠我sr/lib64/nginx/modules --conf-path=/etc/nginx/nginx.conf --error-log-path=/var/log/nginx/error.log -SEO靠我-http-log-path=/var/log/nginx/access.log --pid-path=/var/run/nginx.pid --lock-path=/var/run/nginx.loSEO靠我ck --http-client-body-temp-path=/var/cache/nginx/client_temp --http-proxy-temp-path=/var/cache/nginxSEO靠我/proxy_temp --http-fastcgi-temp-path=/var/cache/nginx/fastcgi_temp --http-uwsgi-temp-path=/var/cacheSEO靠我/nginx/uwsgi_temp --http-scgi-temp-path=/var/cache/nginx/scgi_temp --user=nginx --group=nginx --withSEO靠我-compat --with-file-aio --with-threads --with-http_addition_module --with-http_auth_request_module -SEO靠我-with-http_dav_module --with-http_flv_module --with-http_gunzip_module --with-http_gzip_static_modulSEO靠我e --with-http_mp4_module --with-http_random_index_module --with-http_realip_module --with-http_securSEO靠我e_link_module --with-http_slice_module --with-http_ssl_module --with-http_stub_status_module --with-SEO靠我http_sub_module --with-http_v2_module --with-mail --with-mail_ssl_module --with-stream --with-streamSEO靠我_realip_module --with-stream_ssl_module --with-stream_ssl_preread_module --add-module=/root/nginx_upSEO靠我stream_check_module-master --with-cc-opt=-O2 -g -pipe -Wall -Wp,-D_FORTIFY_SOURCE=2 -fexceptions -fsSEO靠我tack-protector-strong --param=ssp-buffer-size=4 -grecord-gcc-switches -m64 -mtune=generic -fPIC --wiSEO靠我th-ld-opt=-Wl,-z,relro -Wl,-z,now -pie [root@LB02 nginx-1.22.1]# make && make install

5、在配置文件SEO靠我上增加健康检查的功能

[root@LB02 conf.d]# cat proxy_web.conf upstream web {server 172.16.1.7:80 max_failSEO靠我s=2 fail_timeout=10s;server 172.16.1.8:80 max_fails=2 fail_timeout=10s;check interval=3000 rise=2 faSEO靠我ll=3 timeout=1000 type=tcp;#interval 检测间隔时间,单位为毫秒#rise 表示请求2次正常,标记此后端的状态为up#fall 表示请求3次失败,标记此后端的状态为dSEO靠我own#type 类型为tcp#timeout 超时时间,单位为毫秒 }server {listen 80;server_name web.koten.com;location / {SEO靠我proxy_pass http://web;include proxy_params;}location /upstream_check {check_status;} }

6、本地hoSEO靠我sts后浏览器访问可以查看健康状态

Nginx负载均衡会话保持

两种处理会话保持问题的方法

1、使用nginx的ip_hash,根据客户端不同的IP,将请求分配到对应的IP

2、基于服务端的session会话SEO靠我共享(NFS、MySQL、memcache、redis、file)

注意:session和cookie的区别

cookie是保存到客户端浏览器的信息,session是保存到服务端的信息,用户登录账号密码,SEO靠我会将cookie保存到浏览器,将session保存到请求的服务器,登录时候,将cookie与session进行匹配,匹配的上即为登录成功。如果采取负载均衡轮询等方式,一台服务器有了session,但是SEO靠我另一台服务器上没有session,这就导致登录不上,此时就需要我们采取redis,将session放到redis中,将redis单独放到一个服务器上,每次登录,我们都去这个服务器找session,这样SEO靠我就不会出现cookie与session不匹配而导致登录失败了。

会话登录故障场景

1、配置Nginx

[root@Web01 conf.d]# vim php.conf server {liSEO靠我sten 80;server_name php.koten.com;root /code/phpMyAdmin-4.8.4-all-languages;location / {index index.SEO靠我php index.html;}location ~ \.php$ {fastcgi_pass 127.0.0.1:9000;fastcgi_param SCRIPT_FILENAME $documeSEO靠我nt_root$fastcgi_script_name;include fastcgi_params;} } [root@Web01 conf.d]# systemctSEO靠我l restart nginx

2、在Web01和Web02分别安装phpmyadmin

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

3、配置phpmyadmin连接远程的数据库

[root@Web01 code]# cd phpSEO靠我MyAdmin-4.8.4-all-languages/ [root@Web01 phpMyAdmin-4.8.4-all-languages]# cp config.sample.iSEO靠我nc.php config.inc.php [root@Web01 phpMyAdmin-4.8.4-all-languages]# vim config.inc.php SEO靠我 /* Server parameters */ $cfg[Servers][$i][host] = 172.16.1.51;

4、配置授权

[root@Web01 conf.d]# cSEO靠我hown -R www.www /var/lib/php/

5、浏览器访问并登录,获取cookie信息

[root@Web01 php]# ll /var/lib/php/session/ SEO靠我 total 28 -rw------- 1 www www 36 Apr 8 22:20 sess_24615fce63ea1785689dba62147bf683

6、将Web01上SEO靠我配置好的phpmyadmin和Nginx配置文件推送到Web02上

[root@Web01 code]# scp -rp phpMyAdmin-4.8.4-all-languages root@172.SEO靠我16.1.8:/code/ [root@Web01 code]# scp /etc/nginx/conf.d/php.conf root@172.16.1.8:/etc/nginx/cSEO靠我onf.d/

7、在web02上重载Nginx服务、授权

[root@Web02 code]# systemctl restart nginx [root@Web02 code]# choSEO靠我wn -R www.www /var/lib/php/

8、接入负载均衡

[root@LB01 conf.d]# vim proxy_php.conf upstream php {servSEO靠我er 172.16.1.7:80;server 172.16.1.8:80; } server {listen 80;server_name php.koten.comSEO靠我;location / {proxy_pass http://php;include proxy_params;} }[root@lb01 conf.d]# nginx -t SEO靠我 nginx: the configuration file /etc/nginx/nginx.conf syntax is ok nginx: configuration fiSEO靠我le /etc/nginx/nginx.conf test is successful [root@lb01 conf.d]# systemctl restart nginx

9、经过轮SEO靠我询后发现,如果session保存在本地文件,登录不上phpmyadmin

使用redis解决会话登录问题

1、安装redis内存数据库

[root@MySQL ~]# yum install redis -SEO靠我y

2、配置redis监听在172.16.1.0网段上

[root@MySQL ~]# sed -i /^bind/c bind 127.0.0.1 172.16.1.51 /etc/redis.confSEO靠我

3、启动redis

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

4、php配置SEO靠我session连接redis

#1.修改/etc/php.ini文件 [root@Web01 ~]# vim /etc/php.ini session.save_handSEO靠我ler = redis session.save_path = "tcp://172.16.1.51:6379" ;session.save_path = "tcp:/SEO靠我/172.16.1.51:6379?auth=123" #如果redis存在密码,则使用该方式 session.auto_start = 1#2.注释php-fpm.d/www.conSEO靠我f里面的两条内容,否则session内容会一直写入/var/lib/php/session目录中 ;php_value[session.save_handler] = files SEO靠我 ;php_value[session.save_path] = /var/lib/php/session

5、重启php-fpm

[root@Web01 code]# systemctl reSEO靠我start php-fpm

6、Web01配置推送给Web02

[root@Web01 code]# scp /etc/php.ini root@172.16.1.8:/etc/php.ini SEO靠我 [root@Web01 code]# scp /etc/php-fpm.d/www.conf root@172.16.1.8:/etc/php-fpm.d/www.conf

7、Web02重启phSEO靠我p-fpm

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

8、redis查看数据

[root@MySQL ~]# redis-cli 127.0.SEO靠我0.1:6379> keys * 1) "PHPREDIS_SESSION:f96f184460e8ca9dfdc65f3e70cca79c"

9、测试登录

登录成功! 

多级代理获取真实用户SEO靠我IP地址

 我们的web服务器的日志查看到的是负载均衡的服务器IP,所以我们需要加如下参数去获取真实的用户IP

set_real_ip_from: 真实服务器上一级代理的IP地址或者IP地址段,可以写多行 SEO靠我 real_ip_header: 从那个heare头检索出需要的IP地址 real_ip_recursive: 递归排除set_real_ip_from里出现的IP,没有SEO靠我出现的则为remote用户来源IP server {listen 80;server_name nginx.oldboy.com;set_real_ip_from 172.16.1.7SEO靠我;set_real_ip_from 172.16.1.8;real_ip_header X-Forwarded-For;real_ip_recursive on;location / {root /cSEO靠我ode;index index.html index.php;} }

我是koten,10年运维经验,持续分享运维干货,感谢大家的阅读和关注!

“SEO靠我”的新闻页面文章、图片、音频、视频等稿件均为自媒体人、第三方机构发布或转载。如稿件涉及版权等问题,请与 我们联系删除或处理,客服邮箱:html5sh@163.com,稿件内容仅为传递更多信息之目的,不代表本网观点,亦不代表本网站赞同 其观点或证实其内容的真实性。

网站备案号:浙ICP备17034767号-2