Home >Operation and Maintenance >Nginx >How to configure Nginx anti-hotlinking

How to configure Nginx anti-hotlinking

WBOY
WBOYforward
2023-05-12 23:04:041951browse

Experimental environment

•A minimally installed centos 7.3 virtual machine
•Configuration: 1 core/512mb
•nginx version 1.12.2

1. Configure the hotlink website

1. Start an nginx virtual machine and configure two websites

vim /etc/nginx/conf .d/vhosts.conf

Add the following content

server {
 listen 80;
 server_name site1.test.com;
 root /var/wwwroot/site1;
 index index.html;

 location / {
 }
}

server {
 listen 80;
 server_name site2.test.com;
 root /var/wwwroot/site2;
 index index.html;

 location / {
 }
}

How to configure Nginx anti-hotlinking

2. Edit c:\windows\system32\ on the host machine drivers\etc\hosts file

192.168.204.11 site1.test.com
192.168.204.11 site2.test.com

##3 .Create the website root directory

mkdir /var/wwwroot
cd /var/wwwroot
mkdir site1
mkdir site2
echo -e "<h1>site1</h1><img  src=&#39;1.jpg&#39; alt="How to configure Nginx anti-hotlinking" >" >> site1/index.html
echo -e "<h1>site2</h1><img  src=&#39;http://site1.test.com/1.jpg&#39; alt="How to configure Nginx anti-hotlinking" >" >> site2/index.html

4.Upload 1.jpg to the /var/wwwroot/site1 directory

##5.Start the nginx service

systemctl restart nginx
netstat -anpt | grep nginx

How to configure Nginx anti-hotlinking

6. Open port 80 through the firewall

setenforce 0
firewall-cmd --zone=public --add-port=80/tcp --permanent
firewall-cmd --reload

7.Access on the host machine

How to configure Nginx anti-hotlinking

How to configure Nginx anti-hotlinking##2. Configure site1.test.com to prevent hotlinking

1. Edit the nginx configuration file

server {
 listen 80;
 server_name site1.test.com;
 root /var/wwwroot/site1;
 index index.html;

 location / {
 }

 location ~ \.(jpg|png|gif|jpeg)$ {
  valid_referers site1.test.com;
  if ($invalid_referer) {
   return 403;
  }
 }
}
server {
 listen 80;
 server_name site2.test.com;
 root /var/wwwroot/site2;
 index index.html;

 location / {
 }
}

How to configure Nginx anti-hotlinking2. Restart the nginx service

systemctl restart nginx

3. On the host machine, visit

to clear the browser cache, and visit

to clear the browser cache. , visit How to configure Nginx anti-hotlinking

It can be seen that the anti-hotlinking configuration plays a roleHow to configure Nginx anti-hotlinking

3. Configure anti-hotlinking to return other resources

1. Edit nginx configuration file

Add a virtual host and rewrite the resources protected by anti-hotlinking

server {
 listen 80;
 server_name site1.test.com;
 root /var/wwwroot/site1;
 index index.html;
 location / {
 }
 location ~ \.(jpg|png|gif|jpeg)$ {
  valid_referers site1.test.com;
  if ($invalid_referer) {
   rewrite ^/ http://site3.test.com/notfound.jpg;
   #return 403;
  }
 }
}
server {
 listen 80;
 server_name site2.test.com;
 root /var/wwwroot/site2;
 index index.html;
 location / {
 }
}
server {
 listen 80;
 server_name site3.test.com;
 root /var/wwwroot/site3;
 index index.html;
 location / {
 }
}

Explanation

location ~ \ .(jpg|png|gif|jpeg)$ {} is the file type for setting anti-hotlinking, separated by vertical bars |.

valid_referers site1.test.com *.nginx.org; is a whitelist, separated by spaces, and * can be used to set the pan-domain name.

if ($invalid_referer) {} is used to determine whether it matches the whitelist. If it does not match the whitelist, the content in {} will be executed.

rewrite ^/ ; is a rewrite resource. If it does not meet the whitelist, it will be rewritten to this address.
return 403; means the returned status code is 403.


2. Create the site3 root directory

cd /var/wwwroot
mkdir site3
echo -e "<h1>site3</h1><img  src=&#39;notfound.jpg&#39; alt="How to configure Nginx anti-hotlinking" >" >> site3/index.html
3. Upload the notfound.jpg file to the /var/wwwroot/site3 directory

4. Restart nginx service

systemctl restart nginx

5. Edit c:\windows\system32\ on the host machine drivers\etc\hosts file

Add mapping to site3.test.com

192.168.204.11 site1.test.com

192.168.204.11 site2.test .com
192.168.204.11 site3.test.com



6. Visit

on the host machine and you can see that site1 was stolen in site2 The 1.jpg file was redirected to the notfound.jpg file on site3

The above is the detailed content of How to configure Nginx anti-hotlinking. For more information, please follow other related articles on the PHP Chinese website!

Statement:
This article is reproduced at:yisu.com. If there is any infringement, please contact admin@php.cn delete