search
HomeOperation and MaintenanceNginxHow to configure Nginx anti-hotlinking

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 id="site">site1</h1><img  src="/static/imghwm/default1.png"  data-src="https://img.php.cn/upload/article/000/887/227/168390384638564.jpg?x-oss-process=image/resize,p_40"  class="lazy"  src=&#39;1.jpg&#39; alt="How to configure Nginx anti-hotlinking" >" >> site1/index.html
echo -e "<h1 id="site">site2</h1><img  src="/static/imghwm/default1.png"  data-src="https://img.php.cn/upload/article/000/887/227/168390384638564.jpg?x-oss-process=image/resize,p_40"  class="lazy"  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 id="site">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:亿速云. If there is any infringement, please contact admin@php.cn delete
NGINX Unit's Purpose: Running Web ApplicationsNGINX Unit's Purpose: Running Web ApplicationsApr 30, 2025 am 12:06 AM

The purpose of NGINXUnit is to simplify the deployment and management of web applications. Its advantages include: 1) Supports multiple programming languages, such as Python, PHP, Go, Java and Node.js; 2) Provides dynamic configuration and automatic reloading functions; 3) manages application lifecycle through a unified API; 4) Adopt an asynchronous I/O model to support high concurrency and load balancing.

NGINX: An Introduction to the High-Performance Web ServerNGINX: An Introduction to the High-Performance Web ServerApr 29, 2025 am 12:02 AM

NGINX started in 2002 and was developed by IgorSysoev to solve the C10k problem. 1.NGINX is a high-performance web server, an event-driven asynchronous architecture, suitable for high concurrency. 2. Provide advanced functions such as reverse proxy, load balancing and caching to improve system performance and reliability. 3. Optimization techniques include adjusting the number of worker processes, enabling Gzip compression, using HTTP/2 and security configuration.

NGINX vs. Apache: A Look at Their ArchitecturesNGINX vs. Apache: A Look at Their ArchitecturesApr 28, 2025 am 12:13 AM

The main architecture difference between NGINX and Apache is that NGINX adopts event-driven, asynchronous non-blocking model, while Apache uses process or thread model. 1) NGINX efficiently handles high-concurrent connections through event loops and I/O multiplexing mechanisms, suitable for static content and reverse proxy. 2) Apache adopts a multi-process or multi-threaded model, which is highly stable but has high resource consumption, and is suitable for scenarios where rich module expansion is required.

NGINX vs. Apache: Examining the Pros and ConsNGINX vs. Apache: Examining the Pros and ConsApr 27, 2025 am 12:05 AM

NGINX is suitable for handling high concurrent and static content, while Apache is suitable for complex configurations and dynamic content. 1. NGINX efficiently handles concurrent connections, suitable for high-traffic scenarios, but requires additional configuration when processing dynamic content. 2. Apache provides rich modules and flexible configurations, which are suitable for complex needs, but have poor high concurrency performance.

NGINX and Apache: Understanding the Key DifferencesNGINX and Apache: Understanding the Key DifferencesApr 26, 2025 am 12:01 AM

NGINX and Apache each have their own advantages and disadvantages, and the choice should be based on specific needs. 1.NGINX is suitable for high concurrency scenarios because of its asynchronous non-blocking architecture. 2. Apache is suitable for low-concurrency scenarios that require complex configurations, because of its modular design.

NGINX Unit: Key Features and CapabilitiesNGINX Unit: Key Features and CapabilitiesApr 25, 2025 am 12:17 AM

NGINXUnit is an open source application server that supports multiple programming languages ​​and provides functions such as dynamic configuration, zero downtime updates and built-in load balancing. 1. Dynamic configuration: You can modify the configuration without restarting. 2. Multilingual support: compatible with Python, Go, Java, PHP, etc. 3. Zero downtime update: Supports application updates that do not interrupt services. 4. Built-in load balancing: Requests can be distributed to multiple application instances.

NGINX Unit vs. Other Application ServersNGINX Unit vs. Other Application ServersApr 24, 2025 am 12:14 AM

NGINXUnit is better than ApacheTomcat, Gunicorn and Node.js built-in HTTP servers, suitable for multilingual projects and dynamic configuration requirements. 1) Supports multiple programming languages, 2) Provides dynamic configuration reloading, 3) Built-in load balancing function, suitable for projects that require high scalability and reliability.

NGINX Unit: The Architecture and How It WorksNGINX Unit: The Architecture and How It WorksApr 23, 2025 am 12:18 AM

NGINXUnit improves application performance and manageability with its modular architecture and dynamic reconfiguration capabilities. 1) Modular design includes master processes, routers and application processes, supporting efficient management and expansion. 2) Dynamic reconfiguration allows seamless update of configuration at runtime, suitable for CI/CD environments. 3) Multilingual support is implemented through dynamic loading of language runtime, improving development flexibility. 4) High performance is achieved through event-driven models and asynchronous I/O, and remains efficient even under high concurrency. 5) Security is improved by isolating application processes and reducing the mutual influence between applications.

See all articles

Hot AI Tools

Undresser.AI Undress

Undresser.AI Undress

AI-powered app for creating realistic nude photos

AI Clothes Remover

AI Clothes Remover

Online AI tool for removing clothes from photos.

Undress AI Tool

Undress AI Tool

Undress images for free

Clothoff.io

Clothoff.io

AI clothes remover

Video Face Swap

Video Face Swap

Swap faces in any video effortlessly with our completely free AI face swap tool!

Hot Tools

SublimeText3 Linux new version

SublimeText3 Linux new version

SublimeText3 Linux latest version

VSCode Windows 64-bit Download

VSCode Windows 64-bit Download

A free and powerful IDE editor launched by Microsoft

Dreamweaver CS6

Dreamweaver CS6

Visual web development tools

Dreamweaver Mac version

Dreamweaver Mac version

Visual web development tools

WebStorm Mac version

WebStorm Mac version

Useful JavaScript development tools