使用NGINX+LUA實現WAF功能 和nginx 防盜鏈

使用NGINX+LUA實現WAF功能

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

  1. set $block_user_agent 0;
  2. if ( $http_user_agent ~ "Wget|AgentBench"){
  3. set $block_user_agent 1;
  4. }
  5. if ($block_user_agent = 1) {
  6. return 403 ;
  7. }

經過其餘機器去wget,結果以下 
node

2.2小試身手之rerurn 404

在nginx配置文件中加入以下內容,讓訪問sql|bak|zip|tgz|tar.gz的請求返回404linux

  1. location ~* "\.(sql|bak|zip|tgz|tar.gz)$"{
  2. return 404
  3. }

在網站根目錄下放一個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

  1. [root@nginx-lua ~]# cd /usr/local/src
  2. [root@nginx-lua src]# wget http://nginx.org/download/nginx-1.9.4.tar.gz
  3. [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

  1. [root@nginx-lua src]# wget http://luajit.org/download/LuaJIT-2.0..tar.gz
  2. [root@nginx-lua src]# wget https://github.com/simpl/ngx_devel_kit/archive/v0.2.19.tar.gz
  3. [root@nginx-lua src]# wget https://github.com/openresty/lua-nginx-module/archive/v0.9.16.tar.gz

最後,建立Nginx運行的普通用戶

  1. [root@nginx-lua src]# useradd -s /sbin/nologin -M www

解壓NDK和lua-nginx-module

  1. [root@openstack-compute-node5 src]# tar zxvf v0.2.19.tar.gz
  2. [root@openstack-compute-node5 src]# tar zxvf v0.9.16.tar.gz

安裝LuaJIT Luajit是Lua即時編譯器

  1. [root@openstack-compute-node5 src]# tar zxvf LuaJIT-2.0.3.tar.gz
  2. [root@openstack-compute-node5 src]# cd LuaJIT-2.0.3
  3. [root@openstack-compute-node5 LuaJIT-2.0.3]# make && make install

安裝Nginx並加載模塊

  1. [root@openstack-compute-node5 src]# tar zxvf nginx-1.9.4.tar.gz
  2. [root@openstack-compute-node5 src]# cd nginx-1.9.4
  3. [root@openstack-compute-node5 nginx-1.9.4]# export LUAJIT_LIB=/usr/local/lib
  4. [root@openstack-compute-node5 nginx-1.9.4]# export LUAJIT_INC=/usr/local/include/luajit-2.0
  5. [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
  6. [root@openstack-compute-node5 nginx-1.5.12]# make -j2 && make install
  7. [root@openstack-compute-node5 ~]# ln -s /usr/local/lib/libluajit-5.1.so.2 /lib64/libluajit-5.1.so.2 #必定建立此軟鏈接,不然報錯

安裝完畢後,下面能夠測試安裝了,修改nginx.conf 增長第一個配置

  1. location /hello {
  2. default_type 'text/plain';
  3. content_by_lua 'ngx.say("hello,lua")';
  4. }
  5. [root@openstack-compute-node5 ~]# /usr/local/nginx-1.9.4/sbin/nginx t
  6. [root@openstack-compute-node5 ~]# /usr/local/nginx-1.9.4/sbin/nginx

效果以下 

3.2.3 Openresty部署

安裝依賴包

  1. [root@iZ28t900vpcZ ~]#yum install -y readline-devel pcre-devel openssl-devel

下載並編譯安裝openresty

  1. [root@iZ28t900vpcZ ~]#cd /usr/local/src
  2. [root@iZ28t900vpcZ src]#wget https://openresty.org/download/ngx_openresty-1.9.3.2.tar.gz
  3. [root@iZ28t900vpcZ src]#tar zxf ngx_openresty-1.9.3.2.tar.gz
  4. [root@iZ28t900vpcZ src]#cd ngx_openresty-1.9.3.2
  5. [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
  6. [root@iZ28t900vpcZ ngx_openresty-1.9.3.2]#gmake && gmake install
  7. ln -s /usr/local/openresty-1.9.3.2/ /usr/local/openresty

測試openresty安裝

  1. [root@iZ28t900vpcZ ~]#vim /usr/local/openresty/nginx/conf/nginx.conf
  2. server {
  3. location /hello {
  4. default_type text/html;
  5. content_by_lua_block {
  6. ngx.say("HelloWorld")
  7. }
  8. }
  9. }

測試並啓動nginx

  1. [root@iZ28t900vpcZ ~]#/usr/local/openresty/nginx/sbin/nginx -t
  2. nginx: the configuration file /usr/local/openresty-1.9.3.2/nginx/conf/nginx.conf syntax is ok
  3. nginx: configuration file /usr/local/openresty-1.9.3.2/nginx/conf/nginx.conf test is successful
  4. [root@iZ28t900vpcZ ~]#/usr/local/openresty/nginx/sbin/nginx

3.2.4WAF部署

在github上克隆下代碼

  1. [root@iZ28t900vpcZ ~]#git clone https://github.com/unixhot/waf.git
  2. [root@iZ28t900vpcZ ~]#cp -a ./waf/waf /usr/local/openresty/nginx/conf/

修改Nginx的配置文件,加入(http字段)如下配置。注意路徑,同時WAF日誌默認存放在/tmp/日期_waf.log

  1. #WAF
  2. lua_shared_dict limit 50m; #防cc使用字典,大小50M
  3. lua_package_path "/usr/local/openresty/nginx/conf/waf/?.lua";
  4. init_by_lua_file "/usr/local/openresty/nginx/conf/waf/init.lua";
  5. access_by_lua_file "/usr/local/openresty/nginx/conf/waf/access.lua";
  6. [root@openstack-compute-node5 ~]# /usr/local/openresty/nginx/sbin/nginx t
  7. [root@openstack-compute-node5 ~]# /usr/local/openresty/nginx/sbin/nginx

根據日誌記錄位置,建立日誌目錄

  1. [root@iZ28t900vpcZ ~]#mkdir /tmp/waf_logs
  2. [root@iZ28t900vpcZ ~]#chown nginx.nginx /tmp/waf_logs

3.3學習模塊

3.3.1學習配置模塊

WAF上生產以前,建議不要直接上生產,而是先記錄日誌,不作任何動做。肯定WAF不產生誤殺 
config.lua即WAF功能詳解

  1. [root@iZ28t900vpcZ waf]# pwd
  2. /usr/local/nginx/conf/waf
  3. [root@iZ28t900vpcZ waf]# cat config.lua
  4. --WAF config file,enable = "on",disable = "off"
  5. --waf status
  6. config_waf_enable = "on" #是否開啓配置
  7. --log dir
  8. config_log_dir = "/tmp/waf_logs" #日誌記錄地址
  9. --rule setting
  10. config_rule_dir = "/usr/local/nginx/conf/waf/rule-config"
  11. #匹配規則縮放地址
  12. --enable/disable white url
  13. config_white_url_check = "on" #是否開啓url檢測
  14. --enable/disable white ip
  15. config_white_ip_check = "on" #是否開啓IP白名單檢測
  16. --enable/disable block ip
  17. config_black_ip_check = "on" #是否開啓ip黑名單檢測
  18. --enable/disable url filtering
  19. config_url_check = "on" #是否開啓url過濾
  20. --enalbe/disable url args filtering
  21. config_url_args_check = "on" #是否開啓參數檢測
  22. --enable/disable user agent filtering
  23. config_user_agent_check = "on" #是否開啓ua檢測
  24. --enable/disable cookie deny filtering
  25. config_cookie_check = "on" #是否開啓cookie檢測
  26. --enable/disable cc filtering
  27. config_cc_check = "on" #是否開啓防cc攻擊
  28. --cc rate the xxx of xxx seconds
  29. config_cc_rate = "10/60" #容許一個ip60秒內只能訪問10此
  30. --enable/disable post filtering
  31. config_post_check = "on" #是否開啓post檢測
  32. --config waf output redirect/html
  33. config_waf_output = "html" #action一個html頁面,也能夠選擇跳轉
  34. --if config_waf_output ,setting url
  35. config_waf_redirect_url = "http://www.baidu.com"
  36. config_output_html=[[ #下面是html的內容
  37. <html>
  38. <head>
  39. <meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
  40. <meta http-equiv="Content-Language" content="zh-cn" />
  41. <title>網站防火牆</title>
  42. </head>
  43. <body>
  44. <h1 align="center"> # 您的行爲已違反本網站相關規定,注意操做規範。詳情請聯微信公衆號:chuck-blog。
  45. </body>
  46. </html>
  47. ]]

3.4 學習access.lua的配置

  1. [root@iZ28t900vpcZ waf]# pwd
  2. /usr/local/openresty/nginx/conf/waf
  3. [root@iZ28t900vpcZ waf]# cat access.lua
  4. require 'init'
  5. function waf_main()
  6. if white_ip_check() then
  7. elseif black_ip_check() then
  8. elseif user_agent_attack_check() then
  9. elseif cc_attack_check() then
  10. elseif cookie_attack_check() then
  11. elseif white_url_check() then
  12. elseif url_attack_check() then
  13. elseif url_args_attack_check() then
  14. --elseif post_attack_check() then
  15. else
  16. return
  17. end
  18. end
  19. waf_main()

書寫書序:先檢查白名單,經過即不檢測;再檢查黑名單,不經過即拒絕,檢查UA,UA不經過即拒絕;檢查cookie;URL檢查;URL參數檢查,post檢查;

3.5 啓用WAF並測試

3.5.1模擬sql注入即url攻擊

顯示效果以下 

日誌顯示以下,記錄了UA,匹配規則,URL,客戶端類型,攻擊的類型,請求的數據 

3.5.2 使用ab壓測工具模擬防cc攻擊

  1. [root@linux-node3 ~]# ab -c 100 -n 100 http://www.chuck-blog.com/index.php
  2. This is ApacheBench, Version 2.3 <$Revision: 1430300 $>
  3. Copyright 1996 Adam Twiss, Zeus Technology Ltd, http://www.zeustech.net/
  4. Licensed to The Apache Software Foundation, http://www.apache.org/
  5. Benchmarking www.chuck-blog.com (be patient).....done
  6. Server Software: openresty
  7. Server Hostname: www.chuck-blog.com
  8. Server Port: 80
  9. Document Path: /index.php
  10. Document Length: 0 bytes
  11. Concurrency Level: 100
  12. Time taken for tests: 0.754 seconds
  13. Complete requests: 10
  14. Failed requests: 90 #config.lua中設置的,60秒內只容許10個請求
  15. Write errors: 0
  16. Non-2xx responses: 90
  17. Total transferred: 22700 bytes
  18. HTML transferred: 0 bytes
  19. Requests per second: 132.65 [#/sec] (mean)
  20. Time per request: 753.874 [ms] (mean)
  21. Time per request: 7.539 [ms] (mean, across all concurrent requests)
  22. Transfer rate: 29.41 [Kbytes/sec] received
  23. Connection Times (ms)
  24. min mean[+/-sd] median max
  25. Connect: 23 69 20.2 64 105
  26. Processing: 32 180 144.5 157 629
  27. Waiting: 22 179 144.5 156 629
  28. Total: 56 249 152.4 220 702
  29. Percentage of the requests served within a certain time (ms)
  30. 50% 220
  31. 66% 270
  32. 75% 275
  33. 80% 329
  34. 90% 334
  35. 95% 694
  36. 98% 701
  37. 99% 702
  38. 100% 702 (longest request)
  39. ```
  40. ###3.5.3 模擬ip黑名單
  41. 將請求ip放入ip黑名單中

[root@iZ28t900vpcZ rule-config]# echo 「1.202.193.133」 >>/usr/local/openresty/nginx/conf/waf/rule-config/blackip.rule

  1. 顯示結果以下
  2. ![](ca1fa711-48e6-4041-a367-b15db7bf2d2f_128_files/c71edccb-507c-4340-a1fc-ad03e300a2fa.png)
  3. ###3.5.4 模擬ip白名單
  4. 將請求ip放入ip白名單中,此時將不對此ip進行任何防禦措施,因此sql注入時應該返回404

[root@iZ28t900vpcZ rule-config]# echo 「1.202.193.133」 >>/usr/local/openresty/nginx/conf/waf/rule-config/whiteip.rule

  1. 顯示結果以下
  2. ![](ca1fa711-48e6-4041-a367-b15db7bf2d2f_128_files/3ded0e02-480f-48f4-8aab-41aff0fc5538.png)
  3. ###3.5.5 模擬URL參數檢測
  4. 瀏覽器輸入www.chuck-blog.com/?a=select * from table
  5. 顯示結果以下
  6. ![](ca1fa711-48e6-4041-a367-b15db7bf2d2f_128_files/744e4a20-5558-43fc-9f50-1546cbc765a3.png)
  7. 詳細規定在arg.rule中有規定,對請求進行了規範
  8. ```bash
  9. [root@iZ28t900vpcZ rule-config]# /usr/local/openresty/nginx/conf/waf/rule-config/cat args.rule
  10. \.\./
  11. \:\$
  12. \$\{
  13. select.+(from|limit)
  14. (?:(union(.*?)select))
  15. having|rongjitest
  16. sleep\((\s*)(\d*)(\s*)\)
  17. benchmark\((.*)\,(.*)\)
  18. base64_decode\(
  19. (?:from\W+information_schema\W)
  20. (?:(?:current_)user|database|schema|connection_id)\s*\(
  21. (?:etc\/\W*passwd)
  22. into(\s+)+(?:dump|out)file\s*
  23. group\s+by.+\(
  24. xwork.MethodAccessor
  25. (?: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)\(
  26. xwork\.MethodAccessor
  27. (gopher|doc|php|glob|file|phar|zlib|ftp|ldap|dict|ogg|data)\:\/
  28. java\.lang
  29. \$_(GET|post|cookie|files|session|env|phplib|GLOBALS|SERVER)\[
  30. \<(iframe|script|body|img|layer|div|meta|style|base|object|input)
  31. (onmouseover|onerror|onload)\=
  32. [root@iZ28t900vpcZ rule-config]# pwd
  33. /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錯誤。

相關文章
相關標籤/搜索