点赞
评论
收藏
分享
举报
nginx-module-sysguard
发表于2020-10-11 08:28

浏览 1.2k

文章标签

授权协议:
Apache License 2.0
原作者联系方式:
vozltx@gmail.com
功能说明:
如果服务器cpu使用率过高或者内存使用率过高,就启动限流对服务器进行保护

Nginx sysguard module

Table of Contents

Version

This document describes nginx-module-sysguard v0.1.0 released on 23 Feb 2017.

Dependencies

  • nginx
  • sysinfo(2) | getloadavg(3)
  • /proc/meminfo

Compatibility

  • 1.11.x (last tested: 1.11.10)

Earlier versions is not tested.

Installation

  1. Clone the git repository.
shell> git clone git://github.com/vozlt/nginx-module-sysguard.git
  1. Add the module to the build configuration by adding
--add-module=/path/to/nginx-module-sysguard
  1. Build the nginx binary.

  2. Install the nginx binary.

Synopsis

http {

    ...

    server {

        ...

        sysguard on;
        sysguard_mode or;

        sysguard_load load=10.5 action=/loadlimit;
        sysguard_mem swapratio=20% action=/swaplimit;
        sysguard_mem free=100M action=/freelimit;
        sysguard_rt rt=0.01 period=5s method=AMM:10 action=/rtlimit;

        location /loadlimit {
            return 503;
        }

        location /swaplimit {
            return 503;
        }

        location /freelimit {
            return 503;
        }

        location /rtlimit {
            return 503;
        }
    }

    ...

    server {

        ...

        location /api {
            sysguard on;
            sysguard_mode or;
            sysguard_load load=20 action=/limit;
            sysguard_mem swapratio=10% action=/limit;
            sysguard_rt rt=2.01 period=5s method=WMA:10 action=/limit;

            ... 

        }

        location /images {
            sysguard on;
            sysguard_mode and;
            sysguard_load load=20 action=/limit;
            sysguard_mem swapratio=10% action=/limit;
            sysguard_rt rt=2.01 period=5s method=WMA:10 action=/limit;

            ...

        }

        location /limit {
            return 503;
        }
    }

}

Description

This module can be used to protect your server in case system load, memory use goes too high or requests are responded too slow. This is a porting version of the sysguard in tengine to the pure NGINX so as to support the same features.

Caveats: Note this module requires the sysinfo(2) system call, or getloadavg(3) function in glibc. It also requires the /proc file system to get memory information.

Embedded Variables

The following embedded variables are provided:

  • $sysguard_load
    • The load of system. If $sysguard_load's value is 100, then load is 0.1(100/1000). (/msec)
  • $sysguard_swapstat
    • The ratio of using swap. (/per)
  • $sysguard_free
    • The real free space of memory. (/byte)
  • $sysguard_rt
    • The average of request processing times. If $sysguard_rt's value is 100, then response time is 0.1sec(100/1000). (/msec)
  • $sysguard_meminfo_totalram
    • The total memory of meminfo. (/byte)
  • $sysguard_meminfo_freeram
    • The free memory of meminfo. (/byte)
  • $sysguard_meminfo_bufferram
    • The buffer memory of meminfo. (/byte)
  • $sysguard_meminfo_cachedram
    • The cached memory of meminfo. (/byte)
  • $sysguard_meminfo_totalswap
    • The total swap of meminfo. (/byte)
  • $sysguard_meminfo_freeswap
    • The free swap of meminfo. (/byte)

Directives

sysguard

--
Syntaxsysguard <on|off>
Defaultoff
Contexthttp, server, location

Description: Enables or disables the module working.

sysguard_load

--
Syntaxsysguard_load load=number [action=/url]
Default-
Contexthttp, server, location

Description: Specify the load threshold. When the system load exceeds this threshold, all subsequent requests will be redirected to the URL specified by the 'action' parameter. It will return 503 if there's no 'action' URL defined. This directive also support using ncpuratio to instead of the fixed threshold, 'ncpu' means the number of cpu's cores, you can use this directive like this: load=ncpu1.5

sysguard_mem

--
Syntaxsysguard_mem swapratio=ratio% free=size [action=/url]
Default-
Contexthttp, server, location

Description: Specify the used swap memory or free memory threshold. When the swap memory use ratio exceeds this threshold or memory free less than the size, all subsequent requests will be redirected to the URL specified by the 'action' parameter. It will return 503 if there's no 'action' URL. Sysguard uses this strategy to calculate memory free: "memfree = free + buffered + cached"

sysguard_rt

--
Syntaxsysguard_rt rt=second period=time [method=<AMM|WMA>:number] [action=/url]
Default-
Contexthttp, server, location

Description: Specify the response time threshold. Parameter rt is used to set a threshold of the average response time, in second. Parameter period is used to specifiy the period of the statistics cycle. If the average response time of the system exceeds the threshold specified by the user, the incoming request will be redirected to a specified url which is defined by parameter 'action'. If no 'action' is presented, the request will be responsed with 503 error directly. The method is a formula that calculate the average of response processing times. The number in method is the number of samples to calculate the average. The default method is set to be method=AMM:period.

sysguard_mode

--
Syntaxsysguard_mode <and|or>
Defaultor
Contexthttp, server, location

Description: If there are more than one type of monitor, this directive is used to specified the relations among all the monitors which are: 'and' for all matching and 'or' for any matching.

sysguard_interval

--
Syntaxsysguard_interval time
Default1s
Contexthttp, server, location

Description: Specify the time interval to update your system information. The default value is one second, which means sysguard updates the server status once a second.

sysguard_log_level

--
Syntaxsysguard_log_level <info|notice|warn|error>
Defaulterror
Contexthttp, server, location

Description: Specify the log level of sysguard.

See Also

TODO

Donation

License

Author

  • Copyright (C) 2010-2015 Alibaba Group Holding Limited
  • Copyright (C) 2017, YoungJoo.Kim <vozltx@gmail.com>


已修改于2023-03-08 02:18
创作不易,留下一份鼓励
皮皮鲁

暂无个人介绍

关注



写下您的评论
发表评论
全部评论(0)

按点赞数排序

按时间排序

关于作者
皮皮鲁
这家伙很懒还未留下介绍~
85
文章
2
问答
42
粉丝
相关文章
概述 Nginx 从 1.9.0 开始加入了 stream 模块支持四层的代理,转发和负载均衡。但是,stream 模块的功能相对简单。对需要 ALG 处理的协议比如 FTP 的支持也远远不够。我试着去修改了 Nginx 的源代码,添加了alg模块。使之支持了 FTP主动模式和被动模式下的 ALG 功能。 Github 的源码地址为 : https://github.com/pei-jikui/nginx-alg。代码本身不困难,困难的是如何把代码模块化,有机地融入nginx原有的框架结构中,尽量少地修改已有的框架代码。而后者,需要对stream模块乃至nginx本身的框架和代码有一定的熟悉程度。图 1:FTP被动模式 数据连接 图2 :FTP主动模式 数据连接可能大家会说,Passive 模式不需要ALG 。准确
点赞 6
浏览 3.8k
使用配置方式:install./configure--add-module={module_dir}&&make&&makeinstallconfserver{ listen80; client_max_body_size100m; location/{ roothtml/upload; } #Uploadformshouldbesubmittedtothislocation location/upload{ #Passalteredrequestbodytothislocation upload_pass/example.php; #Storefilestothisdirectory #Thedirectoryishashed,subdirectories0123456789shouldexist
点赞 3
浏览 2.8k
使用方法:1.创建tableCREATETABLE oauth_access_token (id int(10)NOTNULLAUTO_INCREMENT,access_token varchar(255)DEFAULTNULL,expires_in int(10)NOTNULL,last_used_time int(10)NOTNULL,PRIMARYKEY(id),KEY ACCESS_TOKEN (access_token))ENGINE=InnoDBDEFAULTCHARSET=utf8;2.安装Oauth模块cd/work/nginx-1.8.0&&./configure--add-module=/work/nginx-http-oauth-module&&make3.添加配置请参照源码连接中的nginx.conf 4.使用Oauth模块a)创建访问tokenhttp://192.168.1.104/token?appid=
点赞 3
浏览 2.1k