1: The general anti-hotlink protection is as follows:
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; } }
First line: gif|jpg|png|swf|flv
Indicates that files with gif, jpg, png, swf, and flv suffixes are protected against hotlinking
The second line: indicates the judgment of the two origins of www.ingnix.com
The meaning of the content in if{} is that if the source is not the specified source, it will jump to the http://www.jzxue.com/retrun.html page. Of course, it is also possible to directly return 403. of.
Two: Prevent hotlinking for image directories
location /images/ { alias /data/images/; valid_referers none blocked server_names *.xok.la xok.la ; if ($invalid_referer) {return403;} }
Three: Use the third-party module ngx_http_accesskey_module to implement Nginx anti-hotlinking
The implementation method is as follows:
1. Download the NginxHttpAccessKeyModule module file: http://wiki.nginx.org/File:Nginx-accesskey-2.0.3.tar.gz;
2. After decompressing this file, find the config file under 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 <add
You need to add the original compilation parameters above, and then execute: make && make install
location /download { accesskeyon; accesskey_hashmethod md5; accesskey_arg"key"; accesskey_signature"mypass$remote_addr"; }
Among them:
accesskey is the module switch;
accesskey_hashmethod is the encryption method MD5 or SHA-1;
accesskey_arg is the keyword parameter in the url;
accesskey_signature is an encrypted value, here it is a string composed of mypass and access IP.
Access the test script download.php:
$ipkey= md5("mypass".$_SERVER['REMOTE_ADDR']); $output_add_key=".$ipkey.">download_add_key
"; $output_org_url="download_org_path
"; echo$output_add_key; echo$output_org_url; ?>
Accessing the first download_add_key link can download normally, but the second link download_org_path will return a 403 Forbidden error.
Reference:
NginxHttpAccessKeyModule
http://xok.la/2009/03/nginx_http_accesskey_module_referer.html
The above introduces the three methods of Nginx anti-hotlinking, including the relevant content. I hope it will be helpful to friends who are interested in PHP tutorials.