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

本文涉及的产品
RDS MySQL Serverless 基础系列,0.5-2RCU 50GB
云数据库 Redis 版,社区版 2GB
推荐场景:
搭建游戏排行榜
RDS MySQL Serverless 高可用系列,价值2615元额度,1个月
简介: 【运维知识进阶篇】集群架构-Nginx七层负载均衡详解(二)

backup测试

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

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

 

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

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

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

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

max_conns最大TCP连接数

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

Nginx负载均衡健康检查

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

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.22.1.tar.gz
[root@LB02 ~]# wget https://github.com/yaoweibin/nginx_upstream_check_module/archive/master.zip

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

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

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

[root@LB02 ~]# cd nginx-1.22.1/
[root@LB02 nginx-1.22.1]# patch -p1 <../nginx_upstream_check_module-master/check_1.20.1+.patch
[root@LB02 nginx-1.22.1]# ./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.22.1]# make && make install

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

[root@LB02 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次正常,标记此后端的状态为up
    #fall      表示请求3次失败,标记此后端的状态为down
    #type      类型为tcp
    #timeout   超时时间,单位为毫秒
}
server {
    listen 80;
    server_name web.koten.com;
    location / {
        proxy_pass http://web;
        include proxy_params;
    }
    location /upstream_check {
        check_status;
    }
}

6、本地hosts后浏览器访问可以查看健康状态

Nginx负载均衡会话保持

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

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

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

注意:session和cookie的区别

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

会话登录故障场景

1、配置Nginx

[root@Web01 conf.d]# vim php.conf
server {
    listen 80;
    server_name php.koten.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、在Web01和Web02分别安装phpmyadmin

[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连接远程的数据库

[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、配置授权

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

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

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

6、将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/

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

[root@Web02 code]# systemctl restart nginx
[root@Web02 code]# chown -R www.www /var/lib/php/

8、接入负载均衡

[root@LB01 conf.d]# vim proxy_php.conf 
upstream php {
        server 172.16.1.7:80;
        server 172.16.1.8:80;
}
server {
        listen 80;
        server_name php.koten.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

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

使用redis解决会话登录问题

1、安装redis内存数据库

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

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

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

3、启动redis

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

4、php配置session连接redis

#1.修改/etc/php.ini文件
[root@Web01 ~]# 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里面的两条内容,否则session内容会一直写入/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

7、Web02重启php-fpm

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

8、redis查看数据

[root@MySQL ~]# redis-cli 
127.0.0.1:6379> keys *
1) "PHPREDIS_SESSION:f96f184460e8ca9dfdc65f3e70cca79c"

9、测试登录

登录成功!

 

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

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

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

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

相关实践学习
部署高可用架构
本场景主要介绍如何使用云服务器ECS、负载均衡SLB、云数据库RDS和数据传输服务产品来部署多可用区高可用架构。
负载均衡入门与产品使用指南
负载均衡(Server Load Balancer)是对多台云服务器进行流量分发的负载均衡服务,可以通过流量分发扩展应用系统对外的服务能力,通过消除单点故障提升应用系统的可用性。 本课程主要介绍负载均衡的相关技术以及阿里云负载均衡产品的使用方法。
目录
相关文章
|
6天前
|
负载均衡 算法 应用服务中间件
面试题:Nginx有哪些负载均衡算法?Nginx位于七层网络结构中的哪一层?
字节跳动面试题:Nginx有哪些负载均衡算法?Nginx位于七层网络结构中的哪一层?
50 0
|
6天前
|
负载均衡 应用服务中间件 API
Nginx配置文件详解Nginx负载均衡Nginx静态配置Nginx反向代理
Nginx配置文件详解Nginx负载均衡Nginx静态配置Nginx反向代理
45 4
|
1天前
|
负载均衡 前端开发 应用服务中间件
Nginx+Tomcat负载均衡配置_nginx做tomcat的负载均衡成功,但tomcat的css文件400
Nginx+Tomcat负载均衡配置_nginx做tomcat的负载均衡成功,但tomcat的css文件400
|
1天前
|
负载均衡 前端开发 应用服务中间件
Nginx+Tomcat负载均衡配置_nginx做tomcat的负载均衡成功,但tomcat的css文件400(2)
Nginx+Tomcat负载均衡配置_nginx做tomcat的负载均衡成功,但tomcat的css文件400(2)
|
5天前
|
负载均衡 应用服务中间件 nginx
解决nginx配置负载均衡时invalid host in upstream报错
在Windows环境下,配置Nginx 1.11.5进行负载均衡时遇到问题,服务无法启动。错误日志显示“invalid host in upstream”。检查发现上游服务器列表中,192.168.29.128的主机地址无效。负载均衡配置中,两个服务器地址前误加了&quot;http://&quot;。修正方法是删除上游服务器列表和proxy_pass中的&quot;http://&quot;。问题解决后,Nginx服务应能正常启动。
38 4
解决nginx配置负载均衡时invalid host in upstream报错
|
6天前
|
应用服务中间件 nginx
nginx配置集群轮训策略
nginx配置集群轮训策略
421 0
|
6天前
|
存储 运维 负载均衡
Heartbeat+Nginx实现高可用集群
通过Heartbeat与Nginx的结合,您可以建立一个高可用性的负载均衡集群,确保在服务器故障时仍能提供无中断的服务。这种配置需要仔细的计划和测试,以确保系统在故障情况下能够正确运行。
23 2
|
6天前
|
负载均衡 网络协议 应用服务中间件
【亮剑】在Linux中构建高可用性和高性能网络服务的负载均衡工具HAProxy、Nginx和Keepalived。
【4月更文挑战第30天】本文介绍了在Linux中构建高可用性和高性能网络服务的负载均衡工具HAProxy、Nginx和Keepalived。HAProxy是一个高性能的开源TCP和HTTP负载均衡器,适合处理大量并发连接;Nginx是一个多功能Web服务器和反向代理,支持HTTP、HTTPS和TCP负载均衡,同时提供缓存和SSL功能;Keepalived用于监控和故障切换,通过VRRP实现IP热备份,保证服务连续性。文中详细阐述了如何配置这三个工具实现负载均衡,包括安装、配置文件修改和启动服务,为构建可靠的负载均衡系统提供了指导。
|
6天前
|
负载均衡 算法 网络协议
LVS、Nginx和HAProxy负载均衡器对比总结
LVS、Nginx和HAProxy负载均衡器对比总结
|
6天前
|
负载均衡 应用服务中间件 PHP
使用nginx-haproxy实现七层负载均衡
【4月更文挑战第13天】使用nginx实现动静分离的负载均衡集群
40 4