37-Nginx负载均衡(一)

37-Nginx负载均衡

Nginx负载均衡基本概述
当我们的Web服务器直接面向用户,往往要承载大量并发请求,单台服务器难以负荷,我使用多台Web服务器组成集群,前端使用Nginx负载均衡,将请求分散的打到我们的后端服务器集群中,实现负载的分发。那么会大大提升系统的吞吐率、请求性能、高容灾。
往往我们接触的最多的是SLB(Server Load Balance)负载均衡,实现最多的也是SLB、那么SLB它的调度节点和服务节点通常是在一个地域里面。那么它在这个小的逻辑地域里面决定了他对部分服务的实时性、响应性是非常好的。
所以说当海量用户请求过来以后,它同样是请求调度节点,调度节点将用户的请求转发给后端对应的服务节点,服务节点处理完请求后在转发给调度节点,调度节点最后响应给用户节点。这样也能实现一个均衡的作用,那么Nginx则是一个典型的SLB

1

负载均衡能实现的应用场景一:四层负载均衡
	所谓四层负载均衡指的是OSI七层模型中的传输层,那么传输层Nginx已经能支持TCP/IP的控制,所以只需要对客户端的请求进行TCP/IP协议的包转发就可以实现负载均衡,那么它的好处是性能非常快、只需要底层进行应用处理,而不需要进行一些复杂的逻辑。

2

负载均衡能实现的应用场景二:七层负载均衡
	七层负载均衡它是在应用层,那么它可以完成很多应用方面的协议请求,比如我们说的http应用的负载均衡,它可以实现http信息的改写、头信息的改写、安全应用规则控制、URL匹配规则控制、以及转发、rewrite等等的规则,所以在应用层的服务里面,我们可以做的内容就更多,那么Nginx则是一个典型的七层负载均衡SLB

3

四层负载均衡与七层负载均衡区别
四层负载均衡数据包在底层就进行了分发,而七层负载均衡数据包则是在最顶层进行分发、由此可以看出,七层负载均衡效率没有四层负载均衡高
但是七层负载均衡更贴近于服务,如http协议就是七层协议,我们用nginx可以做会话保持,URL路径规则匹配、head头改写等等,这些是四层负载均衡无法实现的。

#注意:四层负载均衡不识别域名,七层负载均衡识别域名
nginx负载均衡配置场景
Nginx要实现负载均衡需要用到proxy_pass代理模块配置。

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

4

Nginx负载均衡调度算法
调度算法概述
轮询按时间顺序逐一分配到不同的后端服务器(默认)
weight加权轮询,weight值越大,分配到的访问几率越高
ip_hash每个请求按访问IP的hash结果分配,这样来自同一IP的固定访问一个服务器
url_hash按照访问URL的hash结果来分配请求,是每个URL定向到同一个后端服务器
least_conn最少连接数,哪个机器链接数少就分发到哪个

面试题:简述一下负载均衡的几种调度算法

简述:
1.rr轮询(默认)
2.加权轮询
3.ip_hash
4.url_hash
5.最少链接数
rr轮询
默认轮询
[root@lb01 conf.d]#cat default.conf 
upstream webs {
	server 10.0.0.7;
	server 10.0.0.8;
}
server{
	listen 80;
	server_name www.static.com;
	
	location / {
	proxy_pass http://webs;
	include proxy_params;
	
	}
}
加权轮询
[root@lb01 conf.d]#cat default.conf 
upstream webs {
	server 10.0.0.7 weight=5;	#值越大越最先匹配
	server 10.0.0.8;
}
server{
	listen 80;
	server_name www.static.com;
	
	location / {
	proxy_pass http://webs;
	include proxy_params;
	
	}
}

ip_hash
upstream webs {
        ip_hash;		#先匹配到哪个就一直是哪个。缺点:导致负载不均衡。优点:可以做会话保持
        server 10.0.0.7;
        server 10.0.0.8;
}
server{
        listen 80;
        server_name www.static.com;

        location / {
        proxy_pass http://webs;
        include proxy_params;

        }

}
Nginx负载均衡后端状态
后端Web服务器在前端Nginx负载均衡调度中的状态
状态概述
down当前的server暂时不参与负载均衡
backup预留的备份服务器
max_fails允许请求失败的次数
fail_timeout经过max_fails失败后,服务暂停时间
max_conns限制最大的接收连接数
upstream webs {
        server 10.0.0.7 down;		#不参与负载,相当于#注释掉
        server 10.0.0.8;
}
server{
        listen 80;
        server_name www.static.com;

        location / {
        proxy_pass http://webs;
        include proxy_params;

        }
}
upstream webs {
        server 10.0.0.7;
        server 10.0.0.8 backup;		#当所有的服务器都挂掉后,web02才参与负载。做灾备
}
server{
        listen 80;
        server_name www.static.com;

        location / {
        proxy_pass http://webs;
        include proxy_params;

        }
}
Nginx编译安装
想要实现一个功能,但是Nginx默认没有此模块,需要编译安装的方式将新的模块编译进已安装的Nginx服务中 nginx_upstream_check
检查nginx默认模块:
[root@lb01 conf.d]#nginx -V
nginx version: nginx/1.24.0
built by gcc 4.8.5 20150623 (Red Hat 4.8.5-44) (GCC) 
built with OpenSSL 1.0.2k-fips  26 Jan 2017
TLS SNI support enabled
configure arguments: --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 --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@lb01 ~]#yum install -y gcc glibc gcc-c++ pcre-devel openssl-devel patch

第二步:下载和已经安装的nginx版本相同的源码包

[root@lb01 ~]#nginx -v
nginx version: nginx/1.24.0
[root@lb01 ~]#wget http://nginx.org/download/nginx-1.24.0.tar.gz

下载第三方插件:
先下载到Windows,再上传到lb01
解压第三方插件和源码包

[root@lb01 ~]#unzip nginx_upstream_check_module-master.zip
[root@lb01 ~]#tar xf nginx-1.24.0.tar.gz

打补丁:将下载的模块加载到nginx模块中
[root@lb01 ~]#cd nginx-1.24.0/
[root@lb01 nginx-1.24.0]#patch -p1 < ../nginx_upstream_check_module-master/check_1.20.1+.patch

[root@lb01 nginx-1.24.0]#./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 --add-module=/root/nginx_upstream_check_module-master --with-stream_ssl_preread_module --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'

#make编译
[root@lb01 nginx-1.24.0]#make
[root@lb01 nginx-1.24.0]#make install

测试新加入模块是否可以使用:
[root@lb01 ~]#cat /etc/nginx/conf.d/default.conf 
upstream webs {
	#server 10.0.0.7;
	#server 10.0.0.8;
	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;
}
server{
	listen 80;
	server_name www.static.com;
	
	location / {
	proxy_pass http://webs;
	include proxy_params;
	
	}
	location /check {
	check_status;
	}	
}
访问:www.static.com/check

5

Nginx负载均衡会话保持
在使用负载均衡的时候会遇到会话保持的问题,可通过如下方式进行解决。
1.使用nginx的ip_hash,根据客户端的IP,将请求分配到对应的IP上
2.基于服务端的session会话共享(NFS,MySQL,memcache,redis,file)

在解决负载均衡会话问题,我们需要了解session和cookie的区别。
浏览器端存的是cookie每次浏览器发请求到服务端时,报文头是会自动添加cookie信息的。
服务端会查询用户的cookie作为key去存储里找对应的value(session)
同一个域名下的网站的cookie都是一样的,所以无论几台服务器,无论请求分配到哪一台服务器上同一用户的cookie是不变的。也就是说cookie对应的session也是唯一的。所以,这里只要保证多台业务服务器访问同一个共享存储服务器(NFS,MySQL,memcache,redis,file)就行了。
配置phpadmin业务
1.配置nginx
[root@web01 conf.d]# cp wordpress.conf admin.conf
[root@web01 conf.d]# vim admin.conf 
server {
        listen 80;
        server_name www.admin.com;
        root /code/admin;
        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]# nginx -t
nginx: the configuration file /etc/nginx/nginx.conf syntax is ok
nginx: configuration file /etc/nginx/nginx.conf test is successful
[root@web01 conf.d]# systemctl restart nginx

2.配置代码目录
[root@web01 conf.d]# mkdir -p /code/admin
[root@web01 conf.d]# cd /code/admin/
配置代码目录 下载代码
[root@web01 conf.d]#mkdir /code/admin
[root@web01 conf.d]#cd /code/admin
[root@web01 admin]#wget https://files.phpmyadmin.net/phpMyAdmin/4.8.4/phpMyAdmin-4.8.4-all-languages.zip
解压代码:
[root@web01 admin]#unzip phpMyAdmin-4.8.4-all-languages.zip
拷贝代码到当前的目录
[root@web01 admin]#mv phpMyAdmin-4.8.4-all-languages/* .
3.配置数据库连接
[root@web01 admin]# cp config.sample.inc.php config.inc.php
[root@web01 admin]# grep "172.16.1.51" config.inc.php 
$cfg['Servers'][$i]['host'] = '172.16.1.51';
4.远程用户登陆。

5.快速部署web02
1.scp配置文件
[root@web01 admin]# scp /etc/nginx/conf.d/admin.conf root@10.0.0.8:/etc//nginx/conf.d
2.scp代码
[root@web01 code]# tar -zcf admin.tar.gz admin/
[root@web01 code]# scp admin.tar.gz root@10.0.0.8:/code


6.配置负载均衡
[root@lb01 conf.d]#cp wp.conf admin.conf
[root@lb01 conf.d]#vim admin.conf 
upstream admin {
        server 10.0.0.7;
        server 10.0.0.8;
}
server{
        listen 80;
        server_name www.admin.com;

        location / {
        proxy_pass http://admin;
        include proxy_params;
        }
}

[root@lb01 conf.d]#nginx -t
nginx: [warn] conflicting server name "www.she.com" on 0.0.0.0:80, ignored
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

测试登录phpadmin,发现无法登陆。原因:session会话写入了磁盘中,负载均衡会轮询访问web01和web02。session不能验证成功。

解决:会话保持redis.
phpmyadmin业务实现会话保持写入到redis
10.0.0.51安装redis
[root@db01 ~]#yum -y install redis

2.配置redis 修改监听地址为172内网段  默认只监听127.0.0.1
[root@db01 ~]#grep 127.0.0.1 /etc/redis.conf 
# bind 127.0.0.1 ::1
bind 127.0.0.1 172.16.1.51

3.启动redis
[root@db01 ~]#systemctl start redis
[root@db01 ~]#systemctl enable redis

4.检查端口
[root@db01 ~]#netstat -tunlp
Active Internet connections (only servers)
Proto Recv-Q Send-Q Local Address           Foreign Address         State       PID/Program name    
tcp        0      0 172.16.1.51:6379        0.0.0.0:*               LISTEN      4436/redis-server 1 
tcp        0      0 127.0.0.1:6379          0.0.0.0:*               LISTEN      4436/redis-server 1 

5.配置php会话写入到redis中
web01和web02同时修改
vim /etc/php.ini

[root@web01 code]# egrep "session.save_han|session.save_pa" /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存在密码,则使用该方式

注释两行:
[root@web01 code]# vim /etc/php-fpm.d/www.conf
[root@web01 code]# egrep "session.save_han|session.save_pa" /etc/php-fpm.d/www.conf 
;php_value[session.save_handler] = files
;php_value[session.save_path]    = /var/lib/php/session

重启服务生效:
[root@web01 code]# systemctl restart php-fpm


文件同步到web02
[root@web01 code]# scp /etc/php.ini root@10.0.0.8:/etc/php.ini
[root@web01 code]# scp /etc/php-fpm.d/www.conf root@10.0.0.8:/etc/php-fpm.d/www.conf
[root@web02 code]# systemctl restart php-fpm


页面登录:www.admin.com

查看reids中的cookie值:
[root@db01 ~]#redis-cli
127.0.0.1:6379> keys *
1) "PHPREDIS_SESSION:f8c7cc03ab9b3993914c230f354ad76a"
127.0.0.1:6379> 
  • 3
    点赞
  • 4
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

atomLg

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值