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
What does linux cd mean? In Linux, cd means chang...
<br />Without any warning, I saw news on cnB...
Table of contents Problem Analysis Why encapsulat...
The spread operator allows an expression to be ex...
To display the JSON data in a beautiful indented ...
Important data must be backed up, and must be bac...
0x0 Parameter verification Most of the parameter ...
I would like to share the Windows Server 2016 act...
When I surf the Net, I often see web sites filled...
Table of contents Partitioning mechanism SELECT q...
The most common way is to set a primary key or un...
Often when building a SPA, you will need to prote...
Table of contents Application Scenario Ideas Proj...
9 great JavaScript framework scripts for drawing ...
The above article has temporarily concluded my int...