nginx的均衡负载
一、nginx的编译
1、获取到nginx软件
2、解压文件
[root@ck1 ~] tar zxf nginx-1.20.1.tar.gz
[root@ck1 ~] cd nginx-1.20.1/
[root@ck1 nginx-1.20.1] ls
3、nginx源码文件,需要gcc等依赖文件进行编译
[root@ck1 ~] yum install gcc pcre-devel openssl-devel.x86_64 -y
4、关闭c语言编译debug(轻量化编译文件)
[root@ck1 nginx-1.20.1] vim root/nginx-1.20.1/auto/cc/gcc
5、编译路径及功能模块
[root@ck1 nginx-1.20.1] ./configure --prefix=/usr/local/nginx --with-http_stub_status_module --with-http_ssl_module
6、执行编译
[root@ck1 nginx-1.20.1] make
能在nginx/obs目录下下看到二进制编译文件nginx
7、将编译好的文件安装到指定路径
[root@ck1 nginx-1.20.1] make install
在我们设置的路径下能看到编译好的nginx文件
如果需要二次编译nginx,则先需要先make clean, 再做全部过程,不做make install(因为文件已经复制过去,不需要再次复制)
8、为方便启动,加入软链接,并启动
[root@ck1 sbin] cd /usr/local/nginx/sbin
[root@ck1 sbin] ln -s /usr/local/nginx/sbin/nginx /usr/local/sbin/
[root@ck1 sbin] nginx 开启进程
[root@ck1 sbin] netstat -antlp 图1、查看80端口的nginx
图1、nginx的80端口已经启用
成功访问到nginx的默认发布目录
注意:当启用nginx进程时,如果出现80端口被占用的情况,用以下代码重新开启nginx
[root@ck1 sbin] nginx -s reload
[root@ck1 sbin] nginx -s stop
[root@ck1 sbin] nginx 启动nginx
9、官网下载system anginx设置自启动
在指定的内核路径下获取文件,设置开机自启动
system启动nginx
[root@ck1 system] systemctl daemon-reload 重启system,使得刚才获取的nginx服务生效
[root@ck1 system] nginx -s stop
[root@ck1 system] systemctl start nginx
[root@ck1 system] nginx -s reload
[root@ck1 system] systemctl enable nginx
二、nginx的worker管理
用cpu的个数控制nginx的work进程个数
1、升级为两个cpu
2、管理内核
管理nginx的work进程
[root@ck1 ~] cd /usr/local/nginx/conf/
[root@ck1 conf] vim nginx.conf 图1
图1
3、管理虚拟机的并发限制
[root@ck1 conf] vim /etc/security/limits.conf 图1
[root@ck1 conf] systemctl daemon-reload 重启内核
图1、设定并发数
4、新建nginx用户,重新加载nginx服务
[root@ck1 conf] useradd -M -d /usr/local/nginx/ -s /sbin/nologin nginx
[root@ck1 conf] nginx -s reload
可以查看到一个主控制端和控制的两个worker
三、nginx平滑升级
(1)平滑升级
使用nginx1.20对nginx1.19进行版本升级时、为避免升级时掉线,采用平滑升级
1、官网获取新的压缩包,关闭debug
[root@ck1 ~] tar zxf nginx-1.20.1.tar.gz
[root@ck1 ~] cd nginx-1.20.1/
[root@ck1 nginx-1.20.1] vim auto/cc/gcc 图1、修改debug
图1、
2、编译三部曲的前两步
[root@ck1 nginx-1.20.1] ./configure --prefix=/usr/local/nginx --with-http_stub_status_module --with-http_ssl_modul
[root@ck1 nginx-1.20.1] make
3、拷贝旧版本
cp /usr/local/nginx/sbin/nginx /usr/local/nginx/sbin/nginx-old
4、将nginx的1.20新版本覆盖到执行文件下
[root@ck1 nginx-1.20.1] \cp -f objs/nginx /usr/local/nginx/sbin/nginx
其中/cp -f 代表强制覆盖,不询问的意思
5、查看当前的nginx进程,升级新版本
kill -USR2 老版本的进程号
,产生新版本的master和worker
6、结束老版本的work,保留其master
kill -WINCH 老版本的进程号
,这样可以保留老版本的master进程
已经升级到1.20版本
(2)版本回退
当新的版本出现问题时,可以回退老版本。上一节保留的老版本master进程为版本回退创造了条件。
1、将备份的老版本文件覆盖执行目录的nginx
[root@ck1 nginx-1.20.1]# cd /usr/local/nginx/sbin/
[root@ck1 sbin]# \cp -f nginx.old nginx
2、kill -HUP 老版本id
这样通过老版本的mster进程和老版本备份文件实现了平滑后退
3、kill -WINCH 新版本master的id
结束新版本的worker,完成平滑回退
四、nginx负载均衡
(1)实现均衡负载
将主机ck2,ck3作为nginx调度的服务端
1、将配置文件发送给受控端ck2,ck3
[root@ck1 conf] ssh-keygen
[root@ck1 conf] ssh-copy-id ck2
[root@ck1 conf] ssh-copy-id ck3
[root@ck1 conf] scp -r /usr/local/nginx/ ck2:/usr/local/nginx/
[root@ck1 conf] scp -r /usr/local/nginx/ ck3:/usr/local/nginx/
[root@ck1 system] scp /usr/lib/systemd/system/nginx.service ck2:/usr/lib/systemd/system
[root@ck1 system] scp /usr/lib/systemd/system/nginx.service ck3:/usr/lib/systemd/system
2、设置均衡负载模式
将均衡负载模块加入
[root@ck1 conf] vim /usr/local/nginx/conf/nginx.conf 图1
[root@ck1 conf] nginx -s reload
图1
3、分别在server1,server2,server3nginx的发布目录输出文字
[root@ck1 ~] echo ck1 > /usr/local/nginx/html/index.html ck1
[root@ck2 ~] echo ck2 > /usr/local/nginx/html/index.html ck2
[root@ck3 ~] echo ck3 > /usr/local/nginx/html/index.html ck3
4、在ck2,ck3端口重启system、建立nginx用户,启动nginx服务
注意nginx是80端口,如果有http服务需要禁止掉
[root@ck2 system] systemctl daemon-reload 重启system
[root@ck2 conf] useradd -M -d /usr/local/nginx/ -s /sbin/nologin nginx
[root@ck2 conf] systemctl start nginx
--------------------------------------------------------------------
[root@ck3 system] systemctl daemon-reload
[root@ck3 conf] useradd -M -d /usr/local/nginx/ -s /sbin/nologin nginx
[root@ck3 conf] systemctl start nginx
5、在真机和ck1主机做好地址解析
6、ck1主机访问实现负载均衡
(2)增加均衡负载权重
修改配置文件
[root@ansible nginx-1.20.1] vim /usr/local/nginx/conf/nginx.conf
[root@ansible nginx-1.20.1] nginx -s reload
结果出现的ip为20的次数为ip为10的两倍
(3)设置备用机
[root@ck1 nginx-1.20.1] vim /usr/local/nginx/conf/nginx.conf
[root@ck1 nginx-1.20.1] nginx -s reload
ck2,ck3关闭nginx,查看工作情况
[root@ck2 html] systemctl stop nginx
[root@ck3 html] systemctl stop nginx
查看到备用机器启动成功
(4)cookie算法访问服务器
如果均衡负载时存在cdn反向代理时,会存在一直用cdn反向代理的ip一直访问。会存在每次访问不同的服务器都需要输入账户和密码的问题,因此可以采取用比ip更小的cookie访问的方式,能保证每次都锁定同一台服务器
1、获取额外的压缩包辅助cookie算法
2、安装解压
[root@ck1 ~] yum install unzip
[root@ck1 ~] unzip nginx-goodies-nginx-sticky-module-ng-08a395c66e42.zip
3、重现编译,并添加新的cookie软件模块
[root@ck1 nginx-1.20.1] ./configure --prefix=/usr/local/nginx --with-http_stub_status_module --with-http_ssl_module --add-module=/root/nginx-goodies-nginx-sticky-module-ng-08a395c66e42 指定功能模块
[root@ck1 nginx-1.20.1] make
[root@ck1 objs] \cp -f /root/nginx-1.20.1/objs/nginx /usr/local/nginx/sbin/nginx 覆盖原文件
[root@ck1 conf] vim /usr/local/nginx/conf/nginx.conf 图1、设置cookie调度
[root@ck1 conf] nginx -s stop
[root@ck1 conf] nginx -
图1、设置cookie调度
访问www.westos.org
先按f12,再按f5刷新,即可查看到cookie访问记录,且采用cookie的方式访问时会锁定服务器,避免切换服务器时都需要每次输出密码的麻烦
五、访问限制
对于我们发布的内容,nginx也可以对内容进行限制。
nginx可以通过limit_conn_zone 和limit_req_zone两个组件来对客户端访问目录和文件的访问频率和次数进行限制
(1)并发数量限制
[root@ck1 sbin] vim /usr/local/nginx/conf/nginx.conf 图1、修改配置文件
/
limit_conn_zone $binary_remote_addr zone=addr:10m; 这里的zone=addr:10m 表示生成一个大小为10M,名字为addr的内存区域
location /download { 访问/download目录里面时,启用限制并发的模块
limit_conn addr 1;
}
///
图1
2、新建发布相对路径下的发布目录/download,放入图片
[root@ck1 download] mkdir /usr/local/nginx/html/download
[root@ck1 download] mv redhat.jpg /usr/local/nginx/html/download
[root@ck1 download] nginx -s reload
查看到图片,图片大小为55k
3、做压测,-c并发数10,-n请求数10,即10个请求一起发送,因为我们上边做了限制,所以会有访问失败的请求
[root@ck1 download] ab -c10 -n10 http://172.25.42.100/download/redhat.jpg
因为只能处理一个并发,所以会有失败
当改为一个并发时,没有失败
[root@ck1 download] ab -c1 -n10 http://172.25.42.100/download/redhat.jpg
(2)带宽限制
[root@ck1 download] vim /usr/local/nginx/conf/nginx.conf
[root@ck1 download] nginx -s reload
[root@ck1 download] ab -c1 -n 5 http://172.25.42.100/download/redhat.jpg
图片的大小为55k,因为限制的速度为5k,所以5次请求需要50秒左右
(3)限制请求率
速率限制可用于防止 DDoS(分布式拒绝服务攻击) 攻击,或防止上游服务器同时被过多请求淹没。该方法基于以下leaky bucket 算法:请求以各种速率到达存储桶并以固定速率离开存储桶。
修改配置文件,用limit_req_zone 指令设置参数。该指令是在http {}级别上定义的,定义在全局的。设置的模块,允许相同标识的客户端的访问频次,这里限制的是每秒1次
[root@ck1 download] vim /usr/local/nginx/conf/nginx.conf 图1
[root@ck1 download] nginx -s reload
[root@ck1 download] ab -c10 -n10 http://172.25.42.100/download/redhat.jpg
图1
结果:发现只要是一个用户只能成功访问到一次,多的次数都会被拒绝掉
显然这样并不合适,请求仅限于符合limit_req_zone 指令中定义的速率。如果请求数量超过指定的速率并且共享内存区域已满,NGINX 将响应错误。由于流量往往是突发的,因此在流量突发期间返回错误以响应客户端请求并不是最好的情况。
NGINX 中这种过多的请求可以被缓冲和处理。limit_req 指令的burst参数设置等待以指定速率处理的最大请求数,超出zone限制的请求会被放入数目为5的队列中。
[root@ck1 download] vim /usr/local/nginx/conf/nginx.conf 图1
[root@ck1 download] nginx -s reload
[root@ck1 download] ab -c10 -n10 http://172.25.42.100/download/redhat.jpg
在虽然超过了我们设置的只允许一秒一个请求,但可以被放入队列中,一秒处理一个,最后只用了9秒多是因为请求本身就不用多久,第十个已经是最后一个了,请求完自然就结束了