博客
关于我
WAF安全应用防火墙(nginx+lua)
阅读量:87 次
发布时间:2019-02-26

本文共 2626 字,大约阅读时间需要 8 分钟。

一、如果使用nginx简单实现403和404,应该都不难,只需要在配置文件中的server字段中添加相应的内容即可

1、nginx实现rerurn 403
修改nginx配置文件在server中加入以下内容

set $block_user_agent 0;     if ( $http_user_agent ~ "Wget|AgentBench"){        set $block_user_agent 1;     }     if ($block_user_agent = 1) {         return 403 ;   }

2、nginx实现rerurn 404

修改nginx配置文件在server中加入以下内容,让访问sql|bak|zip|tgz|tar.gz的请求返回404

location ~* "\.(sql|bak|zip|tgz|tar.gz)$"{       return 404; }

但是针对于更高层面的功能实现,nginx自带的模块是实现不了的,例如IP白名单、IP黑名单、Cookie过滤等等。而waf安全防火墙可以实现的功能还是比较多的。

针对waf的介绍,请参考我的博客
今天我再介绍另外一种实现waf功能的方法,即”nginx+lua”

二、编译nginx的时候加载lua

1、环境准备:Nginx安装必备的Nginx和PCRE软件包

[root@linux-node2 ~]# cd /usr/local/src [root@linux-node2 src]# wget http://nginx.org/download/nginx-1.9.4.tar.gz[root@linux-node2 src]# wget https://ftp.pcre.org/pub/pcre/pcre-8.40.tar.gz其次,下载当前最新的luajit和ngx_devel_kit (NDK)[root@linux-node2 src]# wget http://luajit.org/download/LuaJIT-2.0.3.tar.gz[root@linux-node2 src]# wget https://github.com/simpl/ngx_devel_kit/archive/v0.2.19.tar.gz[root@linux-node2 src]# wget https://github.com/openresty/lua-nginx-module/archive/v0.9.16.tar.gz[root@linux-node2 src]# useradd -s /sbin/nologin -M www    #创建Nginx运行的普通用户

2、解压NDK和lua-nginx-module

[root@linux-node2 src]# tar -zxvf v0.2.19.tar.gz[root@linux-node2 src]# tar -zxvf v0.9.16.tar.gz

3、安装LuaJIT,Luajit是Lua即时编译器

[root@linux-node2 src]# tar -zxvf LuaJIT-2.0.3.tar.gz [root@linux-node2 src]# cd LuaJIT-2.0.3[root@linux-node2 LuaJIT-2.0.3]# make && make install

4、安装Nginx并加载模块

[root@linux-node2 src]# tar -zxf pcre-8.40.tar.gz[root@linux-node2 src]# tar -zxvf nginx-1.9.4.tar.gz [root@linux-node2 src]# cd nginx-1.9.4[root@linux-node2 nginx-1.9.4]# export LUAJIT_LIB=/usr/local/lib[root@linux-node2 nginx-1.9.4]# export LUAJIT_INC=/usr/local/include/luajit-2.0[root@linux-node2 nginx-1.9.4]# ./configure --prefix=/usr/local/nginx --user=www --group=www     --with-http_ssl_module --with-http_stub_status_module --with-file-aio --with-http_dav_module --add-module=../ngx_devel_kit-0.2.19/ --add-module=../lua-nginx-module-0.9.16/ --with-pcre=/usr/local/src/pcre-8.40 [root@linux-node2 nginx-1.5.12]# make -j2 && make install

[root@linux-node2 ~]# ln -s /usr/local/lib/libluajit-5.1.so.2 /lib64/libluajit-5.1.so.2 #一定创建此软连接,否则报错

如果不创建符号链接,可能出现以下异常: error while loading shared libraries: libluajit-5.1.so.2: cannot open shared object file: No such file or directory

安装完毕后,下面可以测试安装了,修改nginx.conf 增加第一个配置

location /hello {                default_type 'text/plain';                content_by_lua 'ngx.say(",lua")';        }[root@linux-node2 ~]# /usr/local/nginx-1.9.4/sbin/nginx –t[root@linux-node2 ~]# /usr/local/nginx-1.9.4/sbin/nginx

测试

转载地址:http://vomk.baihongyu.com/

你可能感兴趣的文章
net发布的dll方法和类显示注释信息(字段说明信息)[图解]
查看>>
Net和T-sql中的日期函数操作
查看>>
Net处理html页面元素工具类(HtmlAgilityPack.dll)的使用
查看>>
Net操作Excel(终极方法NPOI)
查看>>
Net操作配置文件(Web.config|App.config)通用类
查看>>
net网络查看其参数state_dict,data,named_parameters
查看>>
Net连接mysql的公共Helper类MySqlHelper.cs带MySql.Data.dll下载
查看>>
NeurIPS(神经信息处理系统大会)-ChatGPT4o作答
查看>>
neuroph轻量级神经网络框架
查看>>
Neutron系列 : Neutron OVS OpenFlow 流表 和 L2 Population(7)
查看>>
new Blob()实现不同类型的文件下载功能
查看>>
New Concept English three (35)
查看>>
NEW DATE()之参数传递
查看>>
New Journey--工作五年所思所感小记
查看>>
new Queue(REGISTER_DELAY_QUEUE, true, false, false, params)
查看>>
New Relic——手机应用app开发达人的福利立即就到啦!
查看>>
new work
查看>>
new 一个button 然后dispose,最后这个button是null吗???
查看>>
NewspaceGPT的故事续写能力太强了
查看>>
NewspaceGPT绘制时序图
查看>>