nginx防止高负载的解决方案(sysgurad模块)

如果nginx被攻击或者访问量突然变大,nginx会因为负载变高或者内存不够用导致服务器宕机,最终导致站点无法访问。今天要谈到的解决方法来自淘宝开发的模块nginx-http-sysguard,主要用于当负载和内存达到一定的阀值之时,会执行相应的动作,比如直接返回503,504或者其他的.一直等到内存或者负载回到阀值的范围内,站点恢复可用。简单的说,这几个模块是让nginx有个缓冲时间,缓缓.

下载第三方软件包

wget https://github.com/alibaba/nginx-http-sysguard/archive/master.zip -O nginx-http-sysguard-master.zip

 unzip nginx-http-sysguard-master.zip

cp -r nginx-http-sysguard-master /usr/src/nginx-1.6.2/

cd /usr/src/nginx-1.6.2/

patch -p1 < nginx-http-sysguard-master/nginx_sysguard_1.3.9.patch    #打sysguard补丁

./configure --prefix=/usr/local/nginx --with-http_stub_status_module --add-module=nginx-http-sysguard-master

Make

cp objs/nginx /usr/local/nginx/sbin/nginx

2. sysguard指令

语法: sysguard [on | off]
默认值: sysguard off
配置段: http, server, location
开关模块

语法: sysguard_load load=number [action=/url]
默认值: none
配置段: http, server, location
指定负载阀值,当系统的负载超过这个值,所有的请求都会被重定向到action定义的uri请求中.如果没有定义URL action没有定义,那么服务器直接返回503

语法: sysguard_mem swapratio=ratio% [action=/url]
默认值: none
配置段: http, server, location
定义交换分区使用的阀值,如果交换分区使用超过这个阀值,那么后续的请求全部被重定向到action定义的uri请求中.如果没有定义URL action没有定义,那么服务器直接返回503

语法: sysguard_interval time
默认值: sysguard_interval 1s
配置段: http, server, location
定义系统信息更新的频率,默认1秒.

语法: sysguard_log_level info | notice | warn | error
默认值: sysguard_log_level error
配置段: http, server, location
定义sysguard的日志级别

3. sysguard使用实例

3.1 nginx配置

/usr/local/nginx/sbin/nginx -c /usr/local/nginx/conf/nginx.conf

 

负载正常情况下测试:

超负载情况下测试

 

在nginx是realserver的情况下,个人也比较推荐使用这种方法,如果服务器负载一旦爬高,一般要比较长的时间才能恢复到正常水平,在采用这个插件的情况下,负载达到阀值,nginx返回503,前端使用故障转移将请求发往其他服务器,这台服务器在无访问的情况下,便能很快的恢复到正常水平,并且能够立即投入工作。超过阀值的服务器处理请求速度也会大打折扣,使用这个模块,巧妙的将请求发送到了更快速的服务器上,在一定程度上避免了访问速度慢的问题. 前面说的是在集群环境下,在单点环境下,用不用大家斟酌一下。

猜你喜欢

转载自blog.csdn.net/Richardlygo/article/details/82460688