Nginx添加第三方模块实现过载保护 - ITB运维部落—http://www.itbcn.cn—ITB运维技术交流之家平台-ITB运维部落—http://www.itbcn.cn—ITB运维技术交流之家平台
记录工作点滴
分享运维知识

Nginx添加第三方模块实现过载保护

1、下载扩展补丁包,解压并打补丁  点此下载

1
2
3
[[email protected] ~]# unzip nginx-http-sysguard-master.zip
[[email protected] ~]# cd nginx-1.4.2
[[email protected] nginx-1.4.2]# patch -p1 < ../nginx-http-sysguard-master/nginx_sysguard_1.3.9.patch

2、重新编译安装Nginx

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
[[email protected] nginx-1.4.2]# ./configure
>   --prefix=/usr
>   --sbin-path=/usr/sbin/nginx
>   --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/nginx.pid 
>   --lock-path=/var/lock/nginx.lock
>   --user=nginx
>   --group=nginx
>   --with-http_ssl_module
>   --with-http_flv_module
>   --with-http_stub_status_module
>   --with-http_gzip_static_module
>   --http-client-body-temp-path=/var/tmp/nginx/client/
>   --http-proxy-temp-path=/var/tmp/nginx/proxy/
>   --http-fastcgi-temp-path=/var/tmp/nginx/fcgi/
>   --http-uwsgi-temp-path=/var/tmp/nginx/uwsgi
>   --http-scgi-temp-path=/var/tmp/nginx/scgi
>   --with-pcre
>   --add-module=/root/nginx-http-sysguard-master/
[[email protected] nginx-1.4.2]# make && make install

3、修改Nginx配置文件添加过载保护;这里说明下为了测试方便,把CPU负载调的比较低

1
2
3
4
5
6
7
8
9
10
11
12
[[email protected] nginx-1.4.2]# cd /etc/nginx/
[[email protected] nginx]# vim nginx.conf
######在"server"模块中添加如下内容
        sysguard on;   #开启过载保护功能
        sysguard_load load=0.3 action=/loadlimit;   #cpu负载超过0.3就保护,"action"过载保护动作
        #sysguard_mem swapratio=20% action=/swaplimit; #交换分区过载保
        #sysguard_mem free=100M action=/freelimit;     #内存过载保护
        location/loadlimit {
             return 503;     #系统过载返回503
        }
注释:由于新添加了功能需要重启Nginx服务
[[email protected] nginx]# service nginx restart

4、安装”htop”工具,监测负载

1
2
[[email protected] ~]# yum -y install htop
[[email protected] ~]# htop

5、在另一台主机使用apache自带的压力测试工具ab做压测;在压测前先访问一下是否正常如:

1
2
######做压力测试
[[email protected] ~]# ab -n 50000 -c 1000 http://172.16.14.1/index.html

6、查看Nginx服务器的负载状况

7、测试访问Nginx服务器是否能正常访问

未经允许不得转载:ITB运维部落—http://www.itbcn.cn—ITB运维技术交流之家平台 » Nginx添加第三方模块实现过载保护

如果文章对你有帮助,欢迎点击上方按钮打赏作者

评论 抢沙发

  • 昵称 (必填)
  • 邮箱 (必填)
  • 网址