How to use nginx to prevent hotlinking

王林
Release: 2023-06-11 13:25:41
Original
3961 people have browsed it

With the popularity of the Internet, more and more websites provide external link functions for pictures, videos and other resources. However, this external link function is easy to be stolen. Hotlinking means that other websites use pictures, videos and other resources on your website to directly display these resources on their own website through the reference address instead of downloading them to their own server. In this way, hotlink websites can use your website's traffic and bandwidth resources for free, which wastes resources and affects website speed.

To address this problem, Nginx can be used to prevent hotlinking. Nginx is a common web server and reverse proxy server, and it also has a good effect in preventing hot links. Next we will introduce how to use Nginx to prevent hotlinking.

Step 1: Turn on the anti-hotlink module

The core module of Nginx already has anti-hotlink related functions. Before configuring Nginx, we need to first confirm whether Nginx has the anti-hotlink module enabled. If it is not enabled, you need to recompile Nginx or install the corresponding module.

Step 2: Configure anti-hotlinking rules

Nginx’s anti-hotlinking function can be implemented through the location directive in the configuration file. We need to add the following content to the Nginx virtual host configuration file:

location ~* .(gif|jpg|jpeg|png|bmp)$ { valid_referers none blocked yourdomain.com; if ($invalid_referer) { return 403; } }
Copy after login

The above code indicates that external links are only allowed on yourdomain.com site. If the source is not yourdomain.com, a 403 error will be returned and direct access will not be possible.

Step 3: Add whitelist

If you need to allow certain websites or IP addresses to link externally, you can add them to the whitelist. In Nginx, whitelisting can be achieved through the valid_referers directive. For example, we can add the following code to the anti-hotlinking rules in the configuration file to add a whitelist:

location ~* .(gif|jpg|jpeg|png|bmp)$ { valid_referers none blocked yourdomain.com example.com 192.168.0.1; if ($invalid_referer) { return 403; } }
Copy after login

In the above code, we add yourdomain.com and example.com and the IP address 192.168.0.1 Added to the whitelist, these websites can directly access your resources.

Step 4: Enable Rewrite module

Nginx’s Rewrite module can be used to rewrite URLs. We can use the Rewrite module to hide the image URL of the origin site, thereby increasing the difficulty of hotlinking. Protect website resources. We need to add the following content to the configuration file:

if ($http_referer !~ ^http://(www.)?yourdomain.com(/|$)) { return 403; }
Copy after login

The above code means that if the source is not your website, a 403 error will be returned. In this way, hotlinking can be effectively prevented.

Summary

The above are some methods of using Nginx to prevent hotlinking. Through these methods, we can protect our website resources, avoid wasting bandwidth and resources, and at the same time increase the security of the website. In actual applications, you need to configure and adjust it according to your own needs to ensure the effectiveness of the anti-leeching strategy.

The above is the detailed content of How to use nginx to prevent hotlinking. For more information, please follow other related articles on the PHP Chinese website!

Related labels:
source:php.cn
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!