1、瞭解WAF
1.1 什麼是WAF
Web應用防禦系統(也稱:網站應用級入侵防護系統 。英文:Web Application Firewall,簡稱: WAF)。利用國際上公認的一種說法:Web應用 防火牆 是經過執行一系列針對HTTP/HTTPS的 安全策略 來專門爲Web應用提供保護的一款產品。php
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模型3、四層,基於IP報文進行檢測。只是對端口作限制,對TCP協議作封堵。其產品設計無需理解HTTP會話,也就決定了沒法理解Web應用程序語言如HTML、SQL語言。所以,它不可能對HTTP通信進行輸入驗證或攻擊規則分析。針對Web網站的惡意攻擊絕大部分都將封裝爲HTTP請求,從80或443端口順利經過防火牆檢測。
一些定位比較綜合、提供豐富功能的防火牆,也具有必定程度的應用層防護能力,如能根據TCP會話異常性及攻擊特徵阻止網絡層的攻擊,經過IP分拆和組合也能判斷是否有攻擊隱藏在多個數據包中,但從根本上說他仍然沒法理解HTTP會話,難以應對如SQL注入、跨站腳本、cookie竊取、網頁篡改等應用層攻擊。
web應用防火牆能在應用層理解分析HTTP會話,所以能有效的防止各種應用層攻擊,同時他向下兼容,具有網絡防火牆的功能。html
2、使用nginx配置簡單實現403和404
2.1 小試身手之rerurn 403
修改nginx配置文件在server中加入如下內容java
set $block_user_agent 0;
if ( $http_user_agent ~ "Wget|AgentBench"){
set $block_user_agent 1;
}
if ($block_user_agent = 1) {
return 403 ;
}
經過其餘機器去wget,結果以下 node
2.2小試身手之rerurn 404
在nginx配置文件中加入以下內容,讓訪問sql|bak|zip|tgz|tar.gz的請求返回404linux
location ~* "\.(sql|bak|zip|tgz|tar.gz)$"{
return 404
}
在網站根目錄下放一個tar.gz
[root@iZ28t900vpcZ www]# tar zcvf abc.tar.gz wp-content/
經過瀏覽器訪問結果以下,404已生效 nginx
3、深刻實現WAF
3.1 WAF實現規劃
分析步驟以下:解析HTTP請求==》匹配規則==》防護動做==》記錄日誌
具體實現以下:git
- 解析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軟件包。github
[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-moduleweb
[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
4、防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地址,在後續的博文中會加入此功能
5、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錯誤。