如何构建属于自己的nginx waf防火墙 VeryNginx

作者: cayman 分类: 编程技术 发布时间: 2018-09-06 22:08

构建自己防火墙的原因 : 需要对特定链接进行防cc攻击  但对某些链接不需要进行防cc攻击

技术选型: 基于 nginx + lua 的 verynginx 

1、首先verynginx 需要nginx   编译安装模块  或者使用openresty

  • lua-nginx-module
  • http_stub_status_module
  • http_ssl_module

2、verynginx 自带的openresty(nginx)安装脚本  可以一键安装 因为我使用的是宝塔面板 不能使用他的一键安装脚本 或者你可以自己安装openresty   安装连接http://openresty.org/cn/installation.html

3、下面开始安装  verynginx   

[root@localhost]git clone https://github.com/alexazhou/VeryNginx.git  #克隆verynginx仓库
[root@localhost]mkdir -p /opt/verynginx #创建 vernginx 安装目录
[root@localhost]cd verynginx #进入verngin目录
[root@localhost]cp -r -f ./verynginx /opt/verynginx 复制文件

4、将下面三条代码放入 nginx  http 配置块外部,http 配置块内部,server 配置块内部,

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;

5、注意如果开起了nginx vhost模式 有个server 模块就要放几个server 这时候重载 openresty (nginx)配置或者重启就可以  访问响应域名后面加上 vn/index

6、遇到的坑 首先系统后台是使用thinkphp 开发这是时候 我发现 明明开启了防止下载 sql文件 但是我访问  http://demo.com/1.sql 是无法触发拦截规则的  伪静态如下:

location / {
	if (!-e $request_filename){
		rewrite  ^(.*)$  /index.php?s=$1  last;   break;
	}
}
#问题出此案在  if这个判断  !-e $request_filename  这句代表如果不存在文件的话会直接 执行 rewrite规则  也就是说我访问的是 http://demo.php/1.sql 因为目录下没有  1.sql 其实域名实际访问的是  http://demo.php/index.php?s=1.sql 所以没有触发下载限制 但当 目录下存在 1.sql 就会出现警告 没有的话 报的404 是thinphp返回的 所以虽然没有触发 但对于安全还是有用的

7、请求访问顺序

Nginx Rewrite -> VeryNginx Rewrite -> Nginx aceess -> VeryNginx Access

8、有兴趣的小伙伴可以给我留言一起探讨 nginx waf防火墙  lua是天生适合做nginx waf语言

8条评论
  • !

    2019年3月10日 上午10:06

    大佬,用宝塔是怎么搭建的,根据你这个教程试了下,搭建后访问不了,宝塔默认应该是有这个三个模块的
    lua-nginx-module
    http_stub_status_module
    http_ssl_module

    1. cayman

      2019年3月10日 上午11:55

      将nginx 切换成openresty 了吗

      1. !

        2019年3月10日 下午12:29

        安装的Nginx 1.15.8的环境,是要使用 Nginx openresty 环境搭建的吗

        1. cayman

          2019年3月10日 下午12:32

          对的 要使用 openresty 宝塔我之前装的不带那几个模块

          1. !

            2019年3月10日 下午12:33

            好的,我在试下,感谢!

  • 心灵博客

    2019年5月19日 下午10:15

    也想搭建个waf,但是配规则好麻烦,很多意想不到的规则可能就导致网站出问题了。

  • 心灵博客

    2019年5月19日 下午10:19

    Naxsi和这个相比较怎么样?

    1. cayman

      2019年5月19日 下午10:36

      个人觉得这个好点

发表回复

您的电子邮箱地址不会被公开。 必填项已用*标注