MENU

WAF之VeryNginx

December 31, 2016 • Nginx

针对上一篇写的nginx使用nginx_lua模块配置nginx_lua_waf实现web应用防火墙,后来还发现一个很友好的库VeryNginx由春哥开源。

什么是VeryNginx

VeryNginx 是一个功能强大而对人类友好的 Nginx 扩展程序。VeryNginx 基于 lua_nginx_module(openrestry) 开发,实现了高级的防火墙、访问统计和其他的一些功能。 集成在 Nginx 中运行,扩展了 Nginx 本身的功能,并提供了友好的 Web 交互界面。

编译LuaJIT

和上一篇中的一样,同样需要先编译LuaJIT环境,因为VeryNginx依赖lua。

### 下载
wget http://lepig.qiniudn.com/LuaJIT-2.1.0-beta2.tar.gz
### 解压
tar -zxvf LuaJIT-2.1.0-beta2.tar.gz
### 进入目录
cd cd LuaJIT-2.1.0-beta2/
### 开始编译
make && make install
### 建立软连接
ln -s /usr/local/lib/libluajit-5.1.so.2 /lib64/libluajit-5.1.so.2
### 设置环境变量
export LUAJIT_LIB=/usr/local/lib
export LUAJIT_INC=/usr/local/include/luajit-2.1 #这个根据版本来选,我下的是luajit2.1版本

添加lua-nginx-module模块

克隆lua-nginx-module/usr/local/src中,如果没有git环境你也可以去下载压缩包,然后在加压过去都是一样的。

cd /usr/local/src
git clone https://github.com/openresty/lua-nginx-module.git

需要说明一下的是,VeryNginx依赖与下面3个模块

http_stub_status_module
http_ssl_module
lua-nginx-module
所以你必须先把前2个也一起安装了,因为我使用的是lnmp一键包,所以默认都已经加了参数,如果你自己编译的nginx请自己加上这2个模块参数
--with-http_ssl_module \
--with-http_stub_status_module

然后就可以执行./install.sh脚本进行编译安装了。

安装VeryNginx

上面所有环境依赖安装好后就可以正式开始安装VeryNginx了。

### 克隆仓库到本地
git clone https://github.com/alexazhou/VeryNginx.git
### 进入目录
cd VeryNginx 
### 安装verynginx (这条命令只会安装VeryNginx而不会一并把OpenResty也安装了)
python install.py install verynginx

### 参考:https://github.com/alexazhou/VeryNginx/wiki/Use-own-nginx

配置VeryNginx

VeryNginx安装好以后,会在/opt/verynginx目录下生成3条对应的配置文件,分别为:

  • include /opt/verynginx/verynginx/nginx_conf/in_external.conf;
  • include /opt/verynginx/verynginx/nginx_conf/in_http_block.conf;
  • include /opt/verynginx/verynginx/nginx_conf/in_server_block.conf;

以上三条指令分别放在 http 配置块外部,http 配置块内部,server 配置块内部,在修改时请保留这三条。
当然也可以使用自己的 nginx.conf 配置文件,把这三条 include 指令加入对应的地方即可。
如果添加了新的 Server 配置块或 http 配置块,也需要在新的块内部加入对应的 include 行。

重启NGINX,配置完成。


WEB配置界面简单使用

进入管理界面

输入http://your.domain/verynginx/index.html进入。

选项介绍

BASIC分类

  • Matcher 设定匹配规则
  • Response 设定block时响应模板

Custom Action分类

  • Redirect 匹配成功后地址重定向
  • URI Rewrite 匹配成功重写url地址
  • Filter 匹配到规则后选项拒绝还是接受

碰到的问题

当我在保存reponse模板的时候出现Ajax request failed[status code = 200]错误,去看了issue,然后看下面的疑难问题

参考:https://github.com/alexazhou/VeryNginx/wiki/Trouble-Shooting

Last Modified: November 10, 2019
Leave a Comment