一.OpenResty安裝和測試html
官方網站:https://openresty.org/cn/ linux
OpenResty® 是一個基於 Nginx 與 Lua 的高性能 Web 平臺,其內部集成了大量精良的 Lua 庫、第三方模塊以及大多數的依賴項。用於方便地搭建可以處理超高併發、擴展性極高的動態 Web 應用、Web 服務和動態網關。nginx
OpenResty® 經過匯聚各類設計精良的 Nginx 模塊(主要由 OpenResty 團隊自主開發),從而將 Nginx 有效地變成一個強大的通用 Web 應用平臺。這樣,Web 開發人員和系統工程師可使用 Lua 腳本語言調動 Nginx 支持的各類 C 以及 Lua 模塊,快速構造出足以勝任 10K 乃至 1000K 以上單機併發鏈接的高性能 Web 應用系統。apache
OpenResty® 的目標是讓你的Web服務直接跑在 Nginx 服務內部,充分利用 Nginx 的非阻塞 I/O 模型,不單單對 HTTP 客戶端請求,甚至於對遠程後端諸如 MySQL、PostgreSQL、Memcached 以及 Redis 等都進行一致的高性能響應。vim
LUA學習:http://blog.jobbole.com/70480/後端
1.安裝OpenResty:安全
# yum install -y readline-devel pcre-devel openssl-develbash
# cd /usr/local/src併發
下載並編譯安裝openrestyapp
# wget https://openresty.org/download/ngx_openresty-1.9.3.2.tar.gz
# tar zxf ngx_openresty-1.9.3.2.tar.gz
# cd ngx_openresty-1.9.3.2
# ./configure --prefix=/usr/local/ngx_openresty-1.9.3.2 \
--with-luajit --with-http_stub_status_module \
--with-pcre --with-pcre-jit
# gmake && gmake install
# ln -s /usr/local/openresty-1.9.3.2/ /usr/local/openresty
#報錯提醒:
在./configure時的報錯:
/usr/bin/env: perl: No such file or directory
出現這種錯誤多是沒有安裝perl,解決yum install perl -y
[root@localhost ngx_openresty-1.9.3.2]# ./configure --prefix=/usr/local/openresty-1.9.3.2 \--with-luajit --with-http_stub_status_module \--with-pcre --with-pcre-jit platform: linux (linux) cp -rp bundle/ build cd build cd LuaJIT-2.1-20151028 Can't exec "cc": No such file or directory at ./configure line 588. gmake TARGET_STRIP=@: CCDEBUG=-g CC=cc PREFIX=/usr/local/openresty-1.9.3.2/luajit ==== Building LuaJIT 2.1.0-beta1 ==== gmake -C src gmake[1]: cc: Command not found gmake[1]: Entering directory `/usr/local/ngx_openresty-1.9.3.2/build/LuaJIT-2.1-20151028/src' gmake[1]: cc: Command not found gmake[1]: cc: Command not found gmake[1]: cc: Command not found gmake[1]: cc: Command not found gmake[1]: cc: Command not found Makefile:237: *** Unsupported target architecture. Stop. gmake[1]: Leaving directory `/usr/local/ngx_openresty-1.9.3.2/build/LuaJIT-2.1-20151028/src' gmake: *** [default] Error 2 ERROR: failed to run command: gmake TARGET_STRIP=@: CCDEBUG=-g CC=cc PREFIX=/usr/local/openresty-1.9.3.2/luajit
解決:
yum install *gcc* -y
2.測試openresty安裝:
vim /usr/local/openresty/nginx/conf/nginx.conf
server { listen 80; server_name localhost; location / { root html; index index.html index.htm; } ......此處省略 }
在此處省略處,添加一行
location /hello { default_type text/html; content_by_lua_block { ngx.say("HelloWorld") }
3.啓動openresty並測試:
/usr/local/openresty/nginx/sbin/nginx -t
/usr/local/openresty/nginx/sbin/nginx
[root@localhost ~]# curl http://10.0.0.50/hello HelloWorld
4. 性能測試
-- 1. 安裝壓力測試工具
[root@localhost ~]# yum install httpd-tools -y
-- 2. 測試
[root@localhost ~]# ab -c10 -n5000 http://10.0.0.50/hello This is ApacheBench, Version 2.3 <$Revision: 1430300 $> Copyright 1996 Adam Twiss, Zeus Technology Ltd, http://www.zeustech.net/ Licensed to The Apache Software Foundation, http://www.apache.org/ Benchmarking 10.0.0.50 (be patient) Completed 500 requests Completed 1000 requests Completed 1500 requests Completed 2000 requests Completed 2500 requests Completed 3000 requests Completed 3500 requests Completed 4000 requests Completed 4500 requests Completed 5000 requests Finished 5000 requests Server Software: openresty/1.9.3.2 Server Hostname: 10.0.0.50 Server Port: 80 Document Path: /hello Document Length: 11 bytes Concurrency Level: 10 Time taken for tests: 0.505 seconds Complete requests: 5000 Failed requests: 0 Write errors: 0 Total transferred: 790000 bytes HTML transferred: 55000 bytes Requests per second: 9901.83 [#/sec] (mean) Time per request: 1.010 [ms] (mean) Time per request: 0.101 [ms] (mean, across all concurrent requests) Transfer rate: 1527.82 [Kbytes/sec] received Connection Times (ms) min mean[+/-sd] median max Connect: 0 0 0.2 0 1 Processing: 0 1 0.3 1 2 Waiting: 0 0 0.2 0 2 Total: 1 1 0.3 1 3 Percentage of the requests served within a certain time (ms) 50% 1 66% 1 75% 1 80% 1 90% 1 95% 2 98% 2 99% 2 100% 3 (longest request)
...
需求產生 因爲原生態的Nginx的一些安全防禦功能有限,就研究能不能本身編寫一個WAF,參考(照抄)Kindle大神的ngx_lua_waf,本身嘗試寫一個了,使用兩天時間,邊學Lua,邊寫。不過不是安全專業,只實現了一些比較簡單的功能:
####功能列表:
支持IP白名單和黑名單功能,直接將黑名單的IP訪問拒絕。
支持URL白名單,將不須要過濾的URL進行定義。
支持User-Agent的過濾,匹配自定義規則中的條目,而後進行處理(返回403)。
支持CC***防禦,單個URL指定時間的訪問次數,超過設定值,直接返回403。
支持Cookie過濾,匹配自定義規則中的條目,而後進行處理(返回403)。
支持URL過濾,匹配自定義規則中的條目,若是用戶請求的URL包含這些,返回403。
支持URL參數過濾,原理同上。
支持日誌記錄,將全部拒絕的操做,記錄到日誌中去。
日誌記錄爲JSON格式,便於日誌分析,例如使用ELKStack進行***日誌收集、存儲、搜索和展現。
####WAF實現 WAF一句話描述,就是解析HTTP請求(協議解析模塊),規則檢測(規則模塊),作不一樣的防護動做(動做模塊),並將防護過程(日誌模塊)記錄下來。因此本文中的WAF的實現由五個模塊(配置模塊、協議解析模塊、規則模塊、動做模塊、錯誤處理模塊)組成。