最新消息:

nginx动态IP黑白名单构建web防火墙(ngx_white_black_list)

ip admin 3浏览

功能描述:

处在黑名单中的ip与网络,将无法访问web服务。

处在白名单中的ip,访问web服务时,将不受nginx所有安全模块的限制。

支持动态黑名单(需要与ngx_http_limit_req 配合)

具体详见下面的说明

文件配置方法说明

一、定义黑名单或白名单方法:

  1. 配置格式

配置关键字 黑名单或白名单文件 存储空间

white_black_list_conf conf/white.list zone=white:2m;

| | | |

| | | ————————————–存储空间大小 这里是2m. 空间大小决定黑白名单的容量

| | ———————————————————————————————存储空间名

| —————————————————————黑名单或白名单配置文件路径

————————————————配置命令

  1. 配置关键字 white_black_list_conf。

  2. 只能在http{} 中使用

  3. white_black_list_conf可以配置多个 只需 zone=value 其中的value不同就可

  4. 配置示例:

http{

……

white_black_list_conf conf/white.list zone=white:4m;

white_black_list_conf conf/black.list zone=black:4m;

……

server{

…….

}

…….

}

二、黑白名单作用范围

  1. 配置格式

配置关键字 on/off

配置关键字有:white_list 与 black_list 分别用来表示白名单与黑名单

  1. 能在http{}、server{}、location{}下使用, 功能默认是关闭

  2. 配置示例:

http{

……

white_black_list_conf conf/white.list zone=white1:4m;

white_black_list_conf conf/black.list zone=black1:4m;

white_list white1 on; #白名单 white1 在整个http{} 中都开启

black_list black1 on; #黑名单 black1 在整个http{} 中都开启

server{

…….

}

…….

}

http{

……

white_black_list_conf conf/white.list zone=white2:4m;

white_black_list_conf conf/black.list zone=black2:4m;

server{

…….

white_list white2 on; #白名单 white1 在整个server{} 中都开启

black_list black2 on; #黑名单 black1 在整个server{} 中都开启

…….

}

…….

}

http{

……

white_black_list_conf conf/white.list zone=white3:4m;

white_black_list_conf conf/black.list zone=black3:4m;

white_black_list_conf conf/black.list zone=black2:4m;

white_black_list_conf conf/white.list zone=white2:4m;

server{

…….

location /do {

……..

white_list white3 on; #白名单 white3 在location /do{} 中开启

black_list black3 on; #黑名单 black3 在location /do{} 中开启

……..

}

location /do1{

white_list white2 on; #白名单 white2 在整个server{} 中都开启

black_list black2 on; #黑名单 black2 在整个server{} 中都开启

}

…….

}

…….

}

http配置接口说明:

一、配置配置接口

http{

…….

server{

……

location /sec_config{

sec_config on;

}

……

}

…….

}

二、配置方法:

  1. http://xxx/sec_config 查看黑白名单定义情况

返回结果如下

{

”version”: ”nginx/1.3.0″,

”code”: ”0″,

”item”: {

”conf_type”: ”white_black_list_conf”,

”zone_name”: ”white”,

”list_path”: ”/home/john/nginx/conf/white.list”

},

”item”: {

”conf_type”: ”white_black_list_conf”,

”zone_name”: ”black”,

”list_path”: ”/home/john/nginx/conf/black.list”

},

”item”: {

”conf_type”: ”white_black_list_conf”,

”zone_name”: ”ex”,

”list_path”: ”/home/john/nginx/conf/status_ex”

}

}

  1. http://xxx/sec_config?zone_name=white 查看zone_name 为white 的 list_path中的具体内容

  2. http://xxx/sec_config?zone_name=white&add_item=192.168.141.23 向 zone_name 为white 中增加192.168.141.23

  3. http://xxx/sec_config?zone_name=white&delete_item=192.168.141.23 在 zone_name 为white 中删除192.168.141.23

查看配置方法2:

http://xxx/sec_config?for_each

三、黑白名单文件内容

conf/black.list 文件内容如下

2.2.2.2

192.168.141.1

3.3.3.3

4.4.4.5

2.3.4.4

四、动态黑名单

要使用该功能必须对 ngx_http_limit_req_module.c 进行patch

在ngx_http_limit_req_module.c中

增加#include

并修改代码找到:

if (rc == NGX_BUSY) {

ngx_log_error(lrcf->limit_log_level, r->connection->log, 0,

”limiting requests, excess: %ui.%03ui by zone ”%V””,

excess / 1000, excess % 1000,

&limit->shm_zone->shm.name);

在其下面增加:

ngx_black_add_item_interface(r, 1);

配备关键字:

dyn_black

格式:

dyn_black $zone_name time;

比如:

dyn_black black 60; //禁止访问60秒,60秒后自动解除

注意:

必须要配置black_list

配置示例:

http{

….

white_black_list_conf conf/black.list zone=black:4m;

limit_req_zone $binary_remote_addr zone=one:8m rate=4r/s;

server {

location / {

black_list black on;

limit_req zone=one burst=6;

dyn_black black 60; //禁止访问60秒,60秒后自动解除

}

location /xxx {

sec_config on;

}

}

}

参考文章

项目地址:https://github.com/codehunte/ngx_white_black_list

项目文档:https://github.com/codehunte/ngx_white_black_list/blob/master/white_black_list.txt

转载请注明:爱开源 » nginx动态IP黑白名单构建web防火墙(ngx_white_black_list)

转载请注明:爱开源 » nginx动态IP黑白名单构建web防火墙(ngx_white_black_list)