3 ways to prevent hotlinking in Nginx

WBOY
Release: 2016-08-08 09:20:29
Original
952 people have browsed it

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;  } } 
Copy after login

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;} } 
Copy after login

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
Copy after login

You need to add the original compilation parameters above, and then execute: make && make install

  1. Modify the conf file of nginx and add the following lines:
location /download { accesskeyon; accesskey_hashmethod md5; accesskey_arg"key"; accesskey_signature"mypass$remote_addr"; }
Copy after login

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; ?>
Copy after login

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.

Related labels:
source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template
About us Disclaimer Sitemap
php.cn:Public welfare online PHP training,Help PHP learners grow quickly!