Three methods for Nginx anti-leech Protection

Source: Internet
Author: User
Tags 403 forbidden error

I. general anti-leech protection is as follows:
Copy codeThe Code is as follows: location ~ * \. (Gif | jpg | png | swf | flv) $ {
Valid_referers none blocked www.jb51.net jb51.net;
If ($ invalid_referer ){
Rewrite ^/http://www.jb51.net/retrun.html;
# Return 403;
}
}

Line 1: gif | jpg | png | swf | flv
Anti-leech protection is enabled for gif, jpg, png, swf, and flv files.
Row 2: Judge the two routes www.ingnix.com.
The content in if {} indicates that, if a route is not specified, it will jump to the http://www.jb51.net/retrun.html page. Of course, returning 403directly is also possible.

Ii. Preventing leeching for image Directories
Copy codeThe Code is as follows: location/images /{
Alias/data/images /;
Valid_referers none blocked server_names *. xok. la xok. la;
If ($ invalid_referer) {return 403 ;}
}

Iii. Use the third-party module ngx_http_accesskey_module to implement Nginx anti-leech Protection
The implementation method is as follows:

The implementation method is as follows:
1. Download NginxHttpAccessKeyModule module File: Nginx-accesskey-2.0.3.tar.gz;
2. Unzip the file and find the config file under the nginx-accesskey-2.0.3. Edit this file: Replace "$ HTTP_ACCESSKEY_MODULE" with "ngx_http_accesskey_module ";
3. recompile nginx with the following parameters:
./configure --add-module=path/to/nginx-accesskey
4. Modify the nginx conf file and add the following lines:
location /download {
accesskey on;
accesskey_hashmethod md5;
accesskey_arg "key";
accesskey_signature "mypass$remote_addr";
}

Where:
The accesskey is a module switch;
Accesskey_hashmethod is the MD5 or SHA-1 encryption method;
Accesskey_arg is the keyword parameter in the url;
Accesskey_signature is the encryption value. Here it is a string consisting of mypass and access IP.

Access test script download. php:
<?
$ipkey= md5("mypass".$_SERVER['REMOTE_ADDR']);
$output_add_key="<a href=http://www.jb51.net/download/G3200507120520LM.rar?key=".$ipkey.">download_add_key</a><br />";
$output_org_url="<a href=http://www.jb51.net/download/G3200507120520LM.rar>download_org_path</a><br />";
echo $output_add_key;
echo $output_org_url;
?>

The first download_add_key link can be downloaded normally. The second link download_org_path will return the 403 Forbidden error.

Refer:
NginxHttpAccessKeyModule

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.