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 / { } }
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
" >> site1/index.html echo -e "site2
" >> 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
setenforce 0 firewall-cmd --zone=public --add-port=80/tcp --permanent firewall-cmd --reload
##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 / { } }
2. 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
It can be seen that the anti-hotlinking configuration plays a role
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 / { } }
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
" >> site3/index.html
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 .com192.168.204.11 site3.test.com6. 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!