Experimental environment • A minimally installed CentOS 7.3 virtual machine 1. Configure hotlink website 1. Start an nginx virtual machine and configure two websites 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 the C:\Windows\System32\drivers\etc\hosts file on the host machine
3. Create the website root directory mkdir /var/wwwroot cd /var/wwwroot mkdir site1 mkdir site2 echo -e "<h1>site1</h1><img src='1.jpg'>" >> site1/index.html echo -e "<h1>site2</h1><img src='http://site1.test.com/1.jpg'>" >> site2/index.html 4. Upload 1.jpg to the /var/wwwroot/site1 directory 5. Start nginx service systemctl restart nginx netstat -anpt | grep nginx 6. Open port 80 on the firewall setenforce 0 firewall-cmd --zone=public --add-port=80/tcp --permanent firewall-cmd --reload 7. Access on the host machine http://site1.test.com http://site2.test.com 2. Configure site1.test.com anti-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 nginx service 3. Access on the host machine Clear the browser cache and visit http://site1.test.com Clear the browser cache and visit http://site2.test.com It can be seen that the anti-hotlink configuration has played a role 3. Configure anti-hotlinking to return other resources 1. Edit the nginx configuration file Add a virtual host to 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 / { } } explain location ~ \.(jpg|png|gif|jpeg)$ {} is the file type for setting anti-hotlinking, separated by a vertical line |. 2. Create the site3 root directory cd /var/wwwroot mkdir site3 echo -e "<h1>site3</h1><img src='notfound.jpg'>" >> site3/index.html 3. Upload the notfound.jpg file to the /var/wwwroot/site3 directory 4. Restart nginx service 5. Edit the C:\Windows\System32\drivers\etc\hosts file on the host machine Add mapping for site3.test.com
6. Visit http://site2.test.com on the host machine As you can see, the 1.jpg file from site1 stolen in site2 is redirected to the notfound.jpg file on site3. Summarize The above is the operation method of configuring Nginx's anti-hotlink that I introduced to you. I hope it will be helpful to you. If you have any questions, please leave me a message and I will reply to you in time. I would also like to thank everyone for their support of the 123WORDPRESS.COM website! You may also be interested in:
|
<<: Detailed explanation of Vue's SSR server-side rendering example
>>: Analysis of the problems and solutions encountered in importing large amounts of data into MySQL
Table of contents 1. Prototype 2. Prototype point...
CSS3 can change the color of pictures. From now o...
Preface When the WeChat mini program project invo...
<br />Related articles: 9 practical suggesti...
This article shares the installation and configur...
What is a table? Table is an Html table, a carrie...
Table of contents 1. Cross-domain filter CorsFilt...
This also caused the inability to upload png files...
Implementing process analysis (1) How to call rep...
Table of contents Vue monitor properties What is ...
A Textbox with Dropdown allows users to select an...
Today when I was writing a flash advertising code,...
Please open the test page in a mainstream browser...
1. Install the express library and generator Open...
Error scenario Use the mysql command in cmd to ad...