OpenResty安装使用教程(CentOS 6)

一、安装OpenResty

Linux官方建议直接通过官方提供的预编译包安装:http://openresty.org/cn/linux-packages.html

# 确保yum周边工具已经安装
yum install yum-utils -y
# 添加仓库
yum-config-manager --add-repo https://openresty.org/package/centos/openresty.repo
# 安装openresty
yum install openresty -y

openresty默认安装在/usr/local/openresty,其中已自带nginx。

 

二、使用安全规则ngx_lua_waf

2.1 ngx_lua_waf介绍

我不明白为什么很多软件不提供默认配置,比较很多暴力破解软件不提供弱口令文件snort有段时间也不提供默认规则,openresty也不提供默认规则。

不过还好有人自己写了规则分享出来:https://github.com/loveshell/ngx_lua_waf

 

2.2 ngx_lua_waf加载到nginx

下载规则文件:

git clone https://github.com/loveshell/ngx_lua_waf.git
mv ngx_lua_waf/ /usr/local/openresty/nginx/conf/waf

纠正nginx位置。因为该规则库默认nginx安装在/usr/local/nginx,我们需要编缉config.lua纠正nginx位置,主要是RulePath和logdir两项:

RulePath = "/usr/local/openresty/nginx/conf/waf/wafconf/"
logdir = "/usr/local/openresty/nginx/logs/hack/"

创建拦截日志文件目录。上面我们设置拦截日志文件目录为/usr/local/openresty/nginx/logs/hack/但该目录尚未存在需要事先创建,且需要赋权给nobody不然没法写:

mkdir /usr/local/openresty/nginx/logs/hack/
chown -R nobody:nobody hack/

加载规则。编缉/usr/local/openresty/nginx/conf/nginx.conf,在http{}内server{}前加入:

lua_shared_dict limit 10m;
lua_package_path "/usr/local/openresty/nginx/conf/waf/?.lua";
init_by_lua_file  /usr/local/openresty/nginx/conf/waf/init.lua; 
access_by_lua_file /usr/local/openresty/nginx/conf/waf/waf.lua;

使用-t确认配置没有错误:

 

三、拦截效果测试

默认只对.php和.jsp文件进行规则检测,但因为是先进行规则检测后进行查询文件是否存在,所以虽然当前nginx没有反向代理php和jsp也没有php和jsp文件但这两个都不用管。

但要注意默认对来自本机的请求是不进行规则过滤的,所以不要在安装OpenResty的机器上curl http://127.0.0.1/test.php?id=../etc/passwd半天,然后说规则怎么没生效。

启动nginx后在另外一台机器上访问(192.168.220.133是我安装OpenResty的机器的ip),效果如下:

进入拦截日志目录检看拦截记录如下:

 

posted on 2018-11-23 18:25  诸子流  阅读(2143)  评论(0编辑  收藏  举报