- 目标
- 1. 安装 OpenResty
- 2. 部署 Best-Nginx-Waf
- 2.1 下载 Best-Nginx-Waf
- 2.2 解压缩
- 2.3 将 best-nginx-waf 文件夹复制到openresty目录下
- 2.4 依赖包安装
- 3. 配置 nginx.conf 启用best-nginx-waf
- 4. 至此基于OpenResty 和 Best-Nginx-Waf搭建的WAF就可以拦截和记录攻击啦;
- 4.1 具体项目链接
基于 Centos -7、 OpenResty、Best-Nginx-Waf 快速搭建起企业级的网站WAF防火墙;1. 安装 OpenResty
yum -y install wget gcc perl unzip openssl openssl-devel #下载并解压源码包 wget https://github.com/zylhahah/best-nginx-waf/archive/refs/heads/master.zip tar zxf openresty-1.13.6.1.tar.gz #编译安装 cd openresty-1.13.6.1 ./configure make make install #默认会安装到/usr/local/openresty目录 #openresty 自带nginx, 启动nginx /usr/local/openresty/nginx/sbin/nginx -t #检查配置文件语法是否正确 /usr/local/openresty/nginx/sbin/nginx #启动nginx2. 部署 Best-Nginx-Waf 2.1 下载 Best-Nginx-Waf
wget https://github.com/ddonline/nginx-lua-waf/archive/master.zip2.2 解压缩
unzip master.zip #解压后得到文件夹 best-nginx-waf2.3 将 best-nginx-waf 文件夹复制到openresty目录下
cp -r best-nginx-waf /usr/local/openresty2.4 依赖包安装
cd best-nginx-waf 目录下 cp http_headers.lua /usr/local/openresty/lualib/resty/ cp http.lua /usr/local/openresty/lualib/resty/ cd best-nginx-waf/luasocket-2.0.2 1、执行 make 命令 2、mkdir -p /usr/local/openresty/luajit/lib/lua/5.1/socket cp src/socket.so.2.0.2 /usr/local/openresty/luajit/lib/lua/5.1/socket/core.so mkdir -p /usr/local/openresty/luajit/lib/lua/5.1/mime cp src/mime.so.1.0.2 /usr/local/openresty/luajit/lib/lua/5.1/mime/core.so3. 配置 nginx.conf 启用best-nginx-waf
vi /usr/local/openresty/nginx/conf/nginx.conf
在http级别添加以下内容:
lua_package_path "/usr/local/openresty/best-nginx-waf/?.lua;/usr/local/openresty/best-nginx-waf/lua-resty-redis/lib/?.lua;/usr/local/openresty/best-nginx-waf/lua-resty-lrucache/lib/?.lua;/usr/local/openresty/best-nginx-waf/luasocket-2.0.2/src/?.lua;/usr/local/openresty/lualib/?.lua;";
lua_shared_dict limit 50m;
lua_code_cache on;
lua_regex_cache_max_entries 4096;
init_worker_by_lua_file /usr/local/openresty/best-nginx-waf/job.lua;
access_by_lua_file /usr/local/openresty/best-nginx-waf/access.lua;
body_filter_by_lua_file /usr/local/openresty/best-nginx-waf/response_filter.lua;
在server级别修改server_name:
#在每个vhost中(server级别)定义server_name时,建议设置一个以上的主机名,默认第一个将做为规则中的主机区别标志,例如
server_name api api.test.com;
# 修改日志目录权限,使nginx对目录可写 具体目录位置可在conf.lua文件中修改
mkdir -p /var/log/best-nginx-waf/
chmod o+w /var/log/best-nginx-waf/
# 重载nginx使配置生效
/usr/local/openresty/nginx/sbin/nginx -t #检查配置文件语法是否正确
/usr/local/openresty/nginx/sbin/nginx -s reload #重载nginx
4. 至此基于OpenResty 和 Best-Nginx-Waf搭建的WAF就可以拦截和记录攻击啦;
4.1 具体项目链接
Best-Nginx-Waf: link
OpenResty: link



