使用NGINX+LUA實現WAF功能
一、了解WAF
1.1 什么是WAF
Web應用防護系統(也稱:網站應用級入侵防御系統 。英文:Web Application Firewall,簡稱: WAF)。利用國際上公認的一種說法:Web應用 防火牆 是通過執行一系列針對HTTP/HTTPS的 安全策略 來專門為Web應用提供保護的一款產品。
1.2 WAF的功能
- 支持IP白名單和黑名單功能,直接將黑名單的IP訪問拒絕。
- 支持URL白名單,將不需要過濾的URL進行定義。
- 支持User-Agent的過濾,匹配自定義規則中的條目,然后進行處理(返回403)。
- 支持CC攻擊防護,單個URL指定時間的訪問次數,超過設定值,直接返回403。
- 支持Cookie過濾,匹配自定義規則中的條目,然后進行處理(返回403)。
- 支持URL過濾,匹配自定義規則中的條目,如果用戶請求的URL包含這些,返回403。
- 支持URL參數過濾,原理同上。
- 支持日志記錄,將所有拒絕的操作,記錄到日志中去
1.3 WAF的特點
- 異常檢測協議
Web應用防火牆會對HTTP的請求進行異常檢測,拒絕不符合HTTP標准的請求。並且,它也可以只允許HTTP協議的部分選項通過,從而減少攻擊的影響范圍。甚至,一些Web應用防火牆還可以嚴格限定HTTP協議中那些過於松散或未被完全制定的選項。 - 增強的輸入驗證
增強輸入驗證,可以有效防止網頁篡改、信息泄露、木馬植入等惡意網絡入侵行為。從而減小Web服務器被攻擊的可能性。 - 及時補丁
修補Web安全漏洞,是Web應用開發者最頭痛的問題,沒人會知道下一秒有什么樣的漏洞出現,會為Web應用帶來什么樣的危害。WAF可以為我們做這項工作了——只要有全面的漏洞信息WAF能在不到一個小時的時間內屏蔽掉這個漏洞。當然,這種屏蔽掉漏洞的方式不是非常完美的,並且沒有安裝對應的補丁本身就是一種安全威脅,但我們在沒有選擇的情況下,任何保護措施都比沒有保護措施更好。 - 基於規則的保護和基於異常的保護
基於規則的保護可以提供各種Web應用的安全規則,WAF生產商會維護這個規則庫,並時時為其更新。用戶可以按照這些規則對應用進行全方面檢測。還有的產品可以基於合法應用數據建立模型,並以此為依據判斷應用數據的異常。但這需要對用戶企業的應用具有十分透徹的了解才可能做到,可現實中這是十分困難的一件事情。 - 狀態管理
WAF能夠判斷用戶是否是第一次訪問並且將請求重定向到默認登錄頁面並且記錄事件。通過檢測用戶的整個操作行為我們可以更容易識別攻擊。狀態管理模式還能檢測出異常事件(比如登陸失敗),並且在達到極限值時進行處理。這對暴力攻擊的識別和響應是十分有利的。 - 其他防護技術
WAF還有一些安全增強的功能,可以用來解決WEB程序員過分信任輸入數據帶來的問題。比如:隱藏表單域保護、抗入侵規避技術、響應監視和信息泄露保護。
1.3WAF與網絡防火牆的區別
網絡防火牆作為訪問控制設備,主要工作在OSI模型三、四層,基於IP報文進行檢測。只是對端口做限制,對TCP協議做封堵。其產品設計無需理解HTTP會話,也就決定了無法理解Web應用程序語言如HTML、SQL語言。因此,它不可能對HTTP通訊進行輸入驗證或攻擊規則分析。針對Web網站的惡意攻擊絕大部分都將封裝為HTTP請求,從80或443端口順利通過防火牆檢測。
一些定位比較綜合、提供豐富功能的防火牆,也具備一定程度的應用層防御能力,如能根據TCP會話異常性及攻擊特征阻止網絡層的攻擊,通過IP分拆和組合也能判斷是否有攻擊隱藏在多個數據包中,但從根本上說他仍然無法理解HTTP會話,難以應對如SQL注入、跨站腳本、cookie竊取、網頁篡改等應用層攻擊。
web應用防火牆能在應用層理解分析HTTP會話,因此能有效的防止各類應用層攻擊,同時他向下兼容,具備網絡防火牆的功能。
二、使用nginx配置簡單實現403和404
2.1 小試身手之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 ;
}
通過其他機器去wget,結果如下
2.2小試身手之rerurn 404
在nginx配置文件中加入如下內容,讓訪問sql|bak|zip|tgz|tar.gz的請求返回404
location ~* "\.(sql|bak|zip|tgz|tar.gz)$"{
return 404
}
在網站根目錄下放一個tar.gz
[root@iZ28t900vpcZ www]# tar zcvf abc.tar.gz wp-content/
通過瀏覽器訪問結果如下,404已生效
三、深入實現WAF
3.1 WAF實現規划
分析步驟如下:解析HTTP請求==》匹配規則==》防御動作==》記錄日志
具體實現如下:
- 解析http請求:協議解析模塊
- 匹配規則:規則檢測模塊,匹配規則庫
- 防御動作:return 403 或者跳轉到自定義界面
- 日志記錄:記錄到elk中,畫出餅圖,建議使用json格式
3.2安裝nginx+lua
由於nginx配置文件書寫不方便,並且實現白名單功能很復雜,nginx的白名單也不適用於CC攻擊,所以在這里使用nginx+lua來實現WAF,如果想使用lua,須在編譯nginx的時候配置上lua,或者結合OpenResty使用,此方法不需要編譯nginx時候指定lua
3.2.1 編譯nginx的時候加載lua
環境准備:Nginx安裝必備的Nginx和PCRE軟件包。
[root@nginx-lua ~]# cd /usr/local/src
[root@nginx-lua src]# wget http://nginx.org/download/nginx-1.9.4.tar.gz
[root@nginx-lua src]# wget ftp://ftp.csx.cam.ac.uk/pub/software/programming/pcre/pcre-8.37.tar.gz
其次,下載當前最新的luajit和ngx_devel_kit (NDK),以及春哥編寫的lua-nginx-module
[root@nginx-lua src]# wget http://luajit.org/download/LuaJIT-2.0..tar.gz
[root@nginx-lua src]# wget https://github.com/simpl/ngx_devel_kit/archive/v0.2.19.tar.gz
[root@nginx-lua src]# wget https://github.com/openresty/lua-nginx-module/archive/v0.9.16.tar.gz
最后,創建Nginx運行的普通用戶
[root@nginx-lua src]# useradd -s /sbin/nologin -M www
解壓NDK和lua-nginx-module
[root@openstack-compute-node5 src]# tar zxvf v0.2.19.tar.gz
[root@openstack-compute-node5 src]# tar zxvf v0.9.16.tar.gz
安裝LuaJIT Luajit是Lua即時編譯器
[root@openstack-compute-node5 src]# tar zxvf LuaJIT-2.0.3.tar.gz
[root@openstack-compute-node5 src]# cd LuaJIT-2.0.3
[root@openstack-compute-node5 LuaJIT-2.0.3]# make && make install
安裝Nginx並加載模塊
[root@openstack-compute-node5 src]# tar zxvf nginx-1.9.4.tar.gz
[root@openstack-compute-node5 src]# cd nginx-1.9.4
[root@openstack-compute-node5 nginx-1.9.4]# export LUAJIT_LIB=/usr/local/lib
[root@openstack-compute-node5 nginx-1.9.4]# export LUAJIT_INC=/usr/local/include/luajit-2.0
[root@openstack-compute-node5 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.37
[root@openstack-compute-node5 nginx-1.5.12]# make -j2 && make install
[root@openstack-compute-node5 ~]# ln -s /usr/local/lib/libluajit-5.1.so.2 /lib64/libluajit-5.1.so.2 #一定創建此軟連接,否則報錯
安裝完畢后,下面可以測試安裝了,修改nginx.conf 增加第一個配置
location /hello {
default_type 'text/plain';
content_by_lua 'ngx.say("hello,lua")';
}
[root@openstack-compute-node5 ~]# /usr/local/nginx-1.9.4/sbin/nginx –t
[root@openstack-compute-node5 ~]# /usr/local/nginx-1.9.4/sbin/nginx
效果如下
3.2.3 Openresty部署
安裝依賴包
[root@iZ28t900vpcZ ~]#yum install -y readline-devel pcre-devel openssl-devel
下載並編譯安裝openresty
[root@iZ28t900vpcZ ~]#cd /usr/local/src
[root@iZ28t900vpcZ src]#wget https://openresty.org/download/ngx_openresty-1.9.3.2.tar.gz
[root@iZ28t900vpcZ src]#tar zxf ngx_openresty-1.9.3.2.tar.gz
[root@iZ28t900vpcZ src]#cd ngx_openresty-1.9.3.2
[root@iZ28t900vpcZ 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
[root@iZ28t900vpcZ ngx_openresty-1.9.3.2]#gmake && gmake install
ln -s /usr/local/openresty-1.9.3.2/ /usr/local/openresty
測試openresty安裝
[root@iZ28t900vpcZ ~]#vim /usr/local/openresty/nginx/conf/nginx.conf
server {
location /hello {
default_type text/html;
content_by_lua_block {
ngx.say("HelloWorld")
}
}
}
測試並啟動nginx
[root@iZ28t900vpcZ ~]#/usr/local/openresty/nginx/sbin/nginx -t
nginx: the configuration file /usr/local/openresty-1.9.3.2/nginx/conf/nginx.conf syntax is ok
nginx: configuration file /usr/local/openresty-1.9.3.2/nginx/conf/nginx.conf test is successful
[root@iZ28t900vpcZ ~]#/usr/local/openresty/nginx/sbin/nginx
3.2.4WAF部署
在github上克隆下代碼
[root@iZ28t900vpcZ ~]#git clone https://github.com/unixhot/waf.git
[root@iZ28t900vpcZ ~]#cp -a ./waf/waf /usr/local/openresty/nginx/conf/
修改Nginx的配置文件,加入(http字段)以下配置。注意路徑,同時WAF日志默認存放在/tmp/日期_waf.log
#WAF
lua_shared_dict limit 50m; #防cc使用字典,大小50M
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/access.lua";
[root@openstack-compute-node5 ~]# /usr/local/openresty/nginx/sbin/nginx –t
[root@openstack-compute-node5 ~]# /usr/local/openresty/nginx/sbin/nginx
根據日志記錄位置,創建日志目錄
[root@iZ28t900vpcZ ~]#mkdir /tmp/waf_logs
[root@iZ28t900vpcZ ~]#chown nginx.nginx /tmp/waf_logs
3.3學習模塊
3.3.1學習配置模塊
WAF上生產之前,建議不要直接上生產,而是先記錄日志,不做任何動作。確定WAF不產生誤殺
config.lua即WAF功能詳解
[root@iZ28t900vpcZ waf]# pwd
/usr/local/nginx/conf/waf
[root@iZ28t900vpcZ waf]# cat config.lua
--WAF config file,enable = "on",disable = "off"
--waf status
config_waf_enable = "on" #是否開啟配置
--log dir
config_log_dir = "/tmp/waf_logs" #日志記錄地址
--rule setting
config_rule_dir = "/usr/local/nginx/conf/waf/rule-config"
#匹配規則縮放地址
--enable/disable white url
config_white_url_check = "on" #是否開啟url檢測
--enable/disable white ip
config_white_ip_check = "on" #是否開啟IP白名單檢測
--enable/disable block ip
config_black_ip_check = "on" #是否開啟ip黑名單檢測
--enable/disable url filtering
config_url_check = "on" #是否開啟url過濾
--enalbe/disable url args filtering
config_url_args_check = "on" #是否開啟參數檢測
--enable/disable user agent filtering
config_user_agent_check = "on" #是否開啟ua檢測
--enable/disable cookie deny filtering
config_cookie_check = "on" #是否開啟cookie檢測
--enable/disable cc filtering
config_cc_check = "on" #是否開啟防cc攻擊
--cc rate the xxx of xxx seconds
config_cc_rate = "10/60" #允許一個ip60秒內只能訪問10此
--enable/disable post filtering
config_post_check = "on" #是否開啟post檢測
--config waf output redirect/html
config_waf_output = "html" #action一個html頁面,也可以選擇跳轉
--if config_waf_output ,setting url
config_waf_redirect_url = "http://www.baidu.com"
config_output_html=[[ #下面是html的內容
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<meta http-equiv="Content-Language" content="zh-cn" />
<title>網站防火牆</title>
</head>
<body>
<h1 align="center"> # 您的行為已違反本網站相關規定,注意操作規范。詳情請聯微信公眾號:chuck-blog。
</body>
</html>
]]
3.4 學習access.lua的配置
[root@iZ28t900vpcZ waf]# pwd
/usr/local/openresty/nginx/conf/waf
[root@iZ28t900vpcZ waf]# cat access.lua
require 'init'
function waf_main()
if white_ip_check() then
elseif black_ip_check() then
elseif user_agent_attack_check() then
elseif cc_attack_check() then
elseif cookie_attack_check() then
elseif white_url_check() then
elseif url_attack_check() then
elseif url_args_attack_check() then
--elseif post_attack_check() then
else
return
end
end
waf_main()
書寫書序:先檢查白名單,通過即不檢測;再檢查黑名單,不通過即拒絕,檢查UA,UA不通過即拒絕;檢查cookie;URL檢查;URL參數檢查,post檢查;
3.5 啟用WAF並測試
3.5.1模擬sql注入即url攻擊
顯示效果如下
日志顯示如下,記錄了UA,匹配規則,URL,客戶端類型,攻擊的類型,請求的數據
3.5.2 使用ab壓測工具模擬防cc攻擊
[root@linux-node3 ~]# ab -c 100 -n 100 http://www.chuck-blog.com/index.php
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 www.chuck-blog.com (be patient).....done
Server Software: openresty
Server Hostname: www.chuck-blog.com
Server Port: 80
Document Path: /index.php
Document Length: 0 bytes
Concurrency Level: 100
Time taken for tests: 0.754 seconds
Complete requests: 10
Failed requests: 90 #config.lua中設置的,60秒內只允許10個請求
Write errors: 0
Non-2xx responses: 90
Total transferred: 22700 bytes
HTML transferred: 0 bytes
Requests per second: 132.65 [#/sec] (mean)
Time per request: 753.874 [ms] (mean)
Time per request: 7.539 [ms] (mean, across all concurrent requests)
Transfer rate: 29.41 [Kbytes/sec] received
Connection Times (ms)
min mean[+/-sd] median max
Connect: 23 69 20.2 64 105
Processing: 32 180 144.5 157 629
Waiting: 22 179 144.5 156 629
Total: 56 249 152.4 220 702
Percentage of the requests served within a certain time (ms)
50% 220
66% 270
75% 275
80% 329
90% 334
95% 694
98% 701
99% 702
100% 702 (longest request)
```
###3.5.3 模擬ip黑名單
將請求ip放入ip黑名單中
[root@iZ28t900vpcZ rule-config]# echo “1.202.193.133” >>/usr/local/openresty/nginx/conf/waf/rule-config/blackip.rule
顯示結果如下

###3.5.4 模擬ip白名單
將請求ip放入ip白名單中,此時將不對此ip進行任何防護措施,所以sql注入時應該返回404
[root@iZ28t900vpcZ rule-config]# echo “1.202.193.133” >>/usr/local/openresty/nginx/conf/waf/rule-config/whiteip.rule
顯示結果如下

###3.5.5 模擬URL參數檢測
瀏覽器輸入www.chuck-blog.com/?a=select * from table
顯示結果如下

詳細規定在arg.rule中有規定,對請求進行了規范
```bash
[root@iZ28t900vpcZ rule-config]# /usr/local/openresty/nginx/conf/waf/rule-config/cat args.rule
\.\./
\:\$
\$\{
select.+(from|limit)
(?:(union(.*?)select))
having|rongjitest
sleep\((\s*)(\d*)(\s*)\)
benchmark\((.*)\,(.*)\)
base64_decode\(
(?:from\W+information_schema\W)
(?:(?:current_)user|database|schema|connection_id)\s*\(
(?:etc\/\W*passwd)
into(\s+)+(?:dump|out)file\s*
group\s+by.+\(
xwork.MethodAccessor
(?:define|eval|file_get_contents|include|require|require_once|shell_exec|phpinfo|system|passthru|preg_\w+|execute|echo|print|print_r|var_dump|(fp)open|alert|showmodaldialog)\(
xwork\.MethodAccessor
(gopher|doc|php|glob|file|phar|zlib|ftp|ldap|dict|ogg|data)\:\/
java\.lang
\$_(GET|post|cookie|files|session|env|phplib|GLOBALS|SERVER)\[
\<(iframe|script|body|img|layer|div|meta|style|base|object|input)
(onmouseover|onerror|onload)\=
[root@iZ28t900vpcZ rule-config]# pwd
/usr/local/openresty/nginx/conf/waf/rule-config
四、防cc攻擊利器之httpgrard
4.1 httpgrard介紹
HttpGuard是基於openresty,以lua腳本語言開發的防cc攻擊軟件。而openresty是集成了高性能web服務器Nginx,以及一系列的Nginx模塊,這其中最重要的,也是我們主要用到的nginx lua模塊。HttpGuard基於nginx lua開發,繼承了nginx高並發,高性能的特點,可以以非常小的性能損耗來防范大規模的cc攻擊。
4.2 httpgrard防cc特效
- 限制訪客在一定時間內的請求次數
- 向訪客發送302轉向響應頭來識別惡意用戶,並阻止其再次訪問
- 向訪客發送帶有跳轉功能的js代碼來識別惡意用戶,並阻止其再次訪問
- 向訪客發送cookie來識別惡意用戶,並阻止其再次訪問
- 支持向訪客發送帶有驗證碼的頁面,來進一步識別,以免誤傷
- 支持直接斷開惡意訪客的連接
- 支持結合iptables來阻止惡意訪客再次連接
- 支持白名單功能
- 支持根據統計特定端口的連接數來自動開啟或關閉防cc模式
詳見github地址,在后續的博文中會加入此功能
五、WAF上線
- 初期上線只記錄日志,不開啟WAF,防止誤殺
- WAF規則管理使用saltstack工具
- 要知道並不是有了WAF就安全,存在人為因素
目前OSS提供的防盜鏈方法主要有以下兩種:
- 設置Referer。該操作通過控制台和SDK均可進行,用戶可根據自身需求進行選擇。
- 簽名URL,適合習慣開發的用戶。
本文將提供如下兩個示例:
- 通過控制台設置Referer防盜鏈
- 基於PHP SDK動態生成簽名URL防盜鏈
nginx 防盜鏈
nginx防盜鏈的方法
一般,我們做好防盜鏈之后其他網站盜鏈的本站圖片就會全部失效無法顯示,但是您如果通過瀏覽器直接輸入圖片地址,仍然會顯示圖片,仍然可以右鍵圖片另存為下載文件!
依然可以下載?這樣就不是徹底的防盜了!那么,nginx應該怎么樣徹底地實現真正意義上的防盜鏈呢?
首先,我們來看下nginx如何設置防盜鏈
如果您使用的是默認站點,也就是說,您的站點可以直接輸入服務器IP訪問的,使用root登錄,修改 /usr/local/nginx/conf/nginx.conf 這個配置文件。
修改/usr/local/nginx/conf/vhost/你的域名.conf 這個配置文件,找到:
location ~ .*\.(gif|jpg|jpeg|png|bmp|swf)$ expires 30d;
把這一段刪掉,修改成:
location ~* \.(gif|jpg|png|jpeg)$ { expires 30d; valid_referers none blocke *.hugao8.com www.hugao8.com m.hugao8.com *.baidu.com *.google.com; if ($invalid_referer) { rewrite ^/ http://ww4.sinaimg.cn/bmiddle/051bbed1gw1egjc4xl7srj20cm08aaa6.jpg; #return 404; } }
第一行: location ~ .*.(gif|jpg|jpeg|png|bmp|swf)$
其中“gif|jpg|jpeg|png|bmp|swf”設置防盜鏈文件類型,自行修改,每個后綴用“|”符號分開!
第三行:valid_referers none blocked *.it300.com it300.com;
就是白名單,允許文件鏈出的域名白名單,自行修改成您的域名!*.it300.com這個指的是子域名,域名與域名之間使用空格隔開!
第五行:rewrite ^/ http://www.it300.com/static/images/404.jpg;
這個圖片是盜鏈返回的圖片,也就是替換盜鏈網站所有盜鏈的圖片。這個圖片要放在沒有設置防盜鏈的網站上,因為防盜鏈的作用,這個圖片如果也放在防盜鏈網站上就會被當作防盜鏈顯示不出來了,盜鏈者的網站所盜鏈圖片會顯示X符號。
這樣設置差不多就可以起到防盜鏈作用了,上面說了,這樣並不是徹底地實現真正意義上的防盜鏈!
我們來看第三行:valid_referers none blocked *.it300.com it300.com;
valid_referers 里多了“none blocked”
我們把“none blocked”刪掉,改成
valid_referers *.it300.com it300.com;
nginx徹底地實現真正意義上的防盜鏈完整的代碼應該是這樣的:
location ~* \.(gif|jpg|png|jpeg)$ { expires 30d; valid_referers *.hugao8.com www.hugao8.com m.hugao8.com *.baidu.com *.google.com; if ($invalid_referer) { rewrite ^/ http://ww4.sinaimg.cn/bmiddle/051bbed1gw1egjc4xl7srj20cm08aaa6.jpg; #return 404; } }
這樣您在瀏覽器直接輸入圖片地址就不會再顯示圖片出來了,也不可能會再右鍵另存什么的。
第五行:rewrite ^/ http://www.it300.com/static/images/404.jpg;
這個是給圖片防盜鏈設置的防盜鏈返回圖片,如果我們是文件需要防盜鏈下載,把第五行:
rewrite ^/ http://www.it300.com/static/images/404.jpg;
改成一個鏈接,可以是您主站的鏈接,比如把第五行改成:
rewrite ^/ http://www.it300.com;
這樣,當別人輸入文件下載地址,由於防盜鏈下載的作用就會跳轉到您設置的這個鏈接!
最后,配置文件設置完成別忘記重啟nginx生效!
一:一般的防盜鏈如下:
location ~* \.(gif|jpg|png|swf|flv)$ { valid_referers none blocked www.jzxue.com jzxue.com ; if ($invalid_referer) { rewrite ^/ http://www.jzxue.com/retrun.html; #return 403; } }
第一行:gif|jpg|png|swf|flv
表示對gif、jpg、png、swf、flv后綴的文件實行防盜鏈
第二行: 表示對www.ingnix.com這2個來路進行判斷
if{}里面內容的意思是,如果來路不是指定來思是,如果來路不是指定來路就跳轉到http://www.jzxue.com/retrun.html頁面,當然直接返回403也是可以的。
二:針對圖片目錄防止盜鏈
location /images/ {
alias /data/images/;
valid_referers none blocked server_names *.xok.la xok.la ;
if ($invalid_referer) {return 403;}
}
三:使用第三方模塊ngx_http_accesskey_module實現Nginx防盜鏈
實現方法如下:
1. 下載NginxHttpAccessKeyModule模塊文件:http://wiki.nginx.org/File:Nginx-accesskey-2.0.3.tar.gz;
2. 解壓此文件后,找到nginx-accesskey-2.0.3下的config文件。編輯此文件:替換其中的”$HTTP_ACCESSKEY_MODULE”為”ngx_http_accesskey_module”;
3. 用一下參數重新編譯nginx:
./configure --add-module=path/to/nginx-accesskey
上面需要加上原有到編譯參數,然后執行: make && make install
4. 修改nginx的conf文件,添加以下幾行:
location /download {
accesskey on;
accesskey_hashmethod md5;
accesskey_arg "key";
accesskey_signature "mypass$remote_addr";
}
其中:
accesskey為模塊開關;
accesskey_hashmethod為加密方式MD5或者SHA-1;
accesskey_arg為url中的關鍵字參數;
accesskey_signature為加密值,此處為mypass和訪問IP構成的字符串。
訪問測試腳本download.PHP:
<? $ipkey= md5("mypass".$_SERVER['REMOTE_ADDR']); $output_add_key="<a href=http://www.jzxue.com/download/G3200507120520LM.rar?key=".$ipkey.">download_add_key</a><br />"; $output_org_url="<a href=http://www.jzxue.com/download/G3200507120520LM.rar>download_org_path</a><br />"; echo $output_add_key; echo $output_org_url; ?>
訪問第一個download_add_key鏈接可以正常下載,第二個鏈接download_org_path會返回403 Forbidden錯誤。