3 ways to prevent hotlinking in Nginx
1: The general anti-hotlink protection is as follows:
<code>location ~* \.(gif|jpg|png|swf|flv)$ { valid_referers none blocked www<span>.jzxue</span><span>.com</span> jzxue<span>.com</span><span>; </span> if ($invalid_referer) { rewrite ^/ http://www<span>.jzxue</span><span>.com</span>/retrun<span>.html</span><span>; </span><span>#return 403; </span> } } </code>
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
<code>location /images<span>/</span> { alias /<span>data</span>/images<span>/</span>; valid_referers <span>none</span> blocked server_names <span>*</span><span>.</span>xok<span>.</span>la xok<span>.</span>la ; <span>if</span> (<span>$invalid_referer</span>) {<span>return</span><span>403</span>;} } </code>
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:
<code>./configure --<span>add</span>-<span>module</span>=path/<span>to</span>/nginx-accesskey <<pestd <span>add</span></code>
You need to add the original compilation parameters above, and then execute: make && make install
- Modify the conf file of nginx and add the following lines:
<code><span>location</span> /download { <span>accesskey</span><span>on</span>; <span>accesskey_hashmethod</span> md5; <span>accesskey_arg</span><span>"key"</span>; <span>accesskey_signature</span><span>"mypass<span>$remote_addr</span>"</span>; }</code>
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:
<code><span><span><?</span><span>$ipkey</span>= md5(<span>"mypass"</span>.<span>$_SERVER</span>[<span>'REMOTE_ADDR'</span>]); <span>$output_add_key</span>=<span>"<a href=http://www.jzxue.com/download/G3200507120520LM.rar?key="</span>.<span>$ipkey</span>.<span>">download_add_key</a><br />"; <span>$output_org_url</span>=<span>"<a href=http://www.jzxue.com/download/G3200507120520LM.rar>download_org_path</a><br />"</span>; <span>echo</span><span>$output_add_key</span>; <span>echo</span><span>$output_org_url</span>; <span>?></span></code>
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.

Hot AI Tools

Undresser.AI Undress
AI-powered app for creating realistic nude photos

AI Clothes Remover
Online AI tool for removing clothes from photos.

Undress AI Tool
Undress images for free

Clothoff.io
AI clothes remover

Video Face Swap
Swap faces in any video effortlessly with our completely free AI face swap tool!

Hot Article

Hot Tools

Notepad++7.3.1
Easy-to-use and free code editor

SublimeText3 Chinese version
Chinese version, very easy to use

Zend Studio 13.0.1
Powerful PHP integrated development environment

Dreamweaver CS6
Visual web development tools

SublimeText3 Mac version
God-level code editing software (SublimeText3)

Hot Topics

You can query the Docker container name by following the steps: List all containers (docker ps). Filter the container list (using the grep command). Gets the container name (located in the "NAMES" column).

The methods that can query the Nginx version are: use the nginx -v command; view the version directive in the nginx.conf file; open the Nginx error page and view the page title.

How to configure an Nginx domain name on a cloud server: Create an A record pointing to the public IP address of the cloud server. Add virtual host blocks in the Nginx configuration file, specifying the listening port, domain name, and website root directory. Restart Nginx to apply the changes. Access the domain name test configuration. Other notes: Install the SSL certificate to enable HTTPS, ensure that the firewall allows port 80 traffic, and wait for DNS resolution to take effect.

How to configure Nginx in Windows? Install Nginx and create a virtual host configuration. Modify the main configuration file and include the virtual host configuration. Start or reload Nginx. Test the configuration and view the website. Selectively enable SSL and configure SSL certificates. Selectively set the firewall to allow port 80 and 443 traffic.

How to confirm whether Nginx is started: 1. Use the command line: systemctl status nginx (Linux/Unix), netstat -ano | findstr 80 (Windows); 2. Check whether port 80 is open; 3. Check the Nginx startup message in the system log; 4. Use third-party tools, such as Nagios, Zabbix, and Icinga.

Create a container in Docker: 1. Pull the image: docker pull [mirror name] 2. Create a container: docker run [Options] [mirror name] [Command] 3. Start the container: docker start [Container name]

Starting an Nginx server requires different steps according to different operating systems: Linux/Unix system: Install the Nginx package (for example, using apt-get or yum). Use systemctl to start an Nginx service (for example, sudo systemctl start nginx). Windows system: Download and install Windows binary files. Start Nginx using the nginx.exe executable (for example, nginx.exe -c conf\nginx.conf). No matter which operating system you use, you can access the server IP

Docker container startup steps: Pull the container image: Run "docker pull [mirror name]". Create a container: Use "docker create [options] [mirror name] [commands and parameters]". Start the container: Execute "docker start [Container name or ID]". Check container status: Verify that the container is running with "docker ps".
