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),效果如下:
進入攔截日誌目錄檢看攔截記錄如下: