How to configure Nginx anti-hotlinking

WBOY
Release: 2023-05-12 23:04:04
forward
1844 people have browsed it

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

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 "

site1

How to configure Nginx anti-hotlinking" >> site1/index.html echo -e "

site2

How to configure Nginx anti-hotlinking" >> site2/index.html
Copy after login

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

##5.Start the nginx service

systemctl restart nginx netstat -anpt | grep nginx
Copy after login

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

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

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. , visitHow 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 / { } }
Copy after login

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 "

site3

How to configure Nginx anti-hotlinking" >> site3/index.html
Copy after login
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!

Related labels:
source:yisu.com
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!