Get the App
SLTechnology News&Howtos  ›  Servers  › 

How to configure hotlink protection for Nginx

Shulou Source: shulou.com Published: 2022-06-01 05:17:38 11月14日 Update

This article is about how to configure hotlink protection in Nginx. The editor thinks it is very practical, so share it with you as a reference and follow the editor to have a look.

First, go straight to the nginx configuration file first.

Server {listen 80; server_name www.test.com; root / data/web/; index index.php index.html; access_log / data/logs/nginx/biao.madacode.access.log main; location / {root / home/data/;} error_page 404 / usr/local/nginx/html/404.html Location ~. *\. (wma | wmv | asf | mp3 | mp4 | mmf | zip | rar | jpg | gif | png | swf | flv) ${valid_referers none blocked server_names * .test.com http://IP; if ($invalid_referer) {return 403;} expires 24h; access_log off;} location ~ /\. {deny all;}}

Second, explanation of hotlink protection core configuration file

Location ~. *\. (wma | wmv | asf | mp3 | mp4 | mmf | zip | rar | jpg | gif | png | swf | flv) ${valid_referers none blocked server_names * .test.com http://IP; if ($invalid_referer) {return 403;} expires 24h; access_log off;}

Vaild_referers valid reference connection, as follows, otherwise enter $invaild_refere and return 403 forbiden.

1. None

If the header of the "Referer" source is empty

2. Blocked

The "Referer" source header is not empty, but the values are deleted by the proxy or firewall, none of which starts with http:// or https://.

3. Server_names

The "Referer" source header contains the current server_names (current domain name)

Third, simulated case test

Add-- referer simulation reference, see the result directly 403. Prove that the above configuration is OK.

[root@test] # curl-- referer http://baidu.com-I http://www.test.com/temp/T19254/20190820/video_out_out/1/0011.mp4 HTTP/1.1 403 Forbidden Server: Tengine Date: Wed, 21 Aug 2019 09:54:44 GMT Content-Type: text/html Content-Length: 639 Connection: keep-alive

Thank you for reading! This is the end of the article on "how to configure hotlink protection in Nginx". I hope the above content can be of some help to you, so that you can learn more knowledge. if you think the article is good, you can share it for more people to see!

Tags: Configuration anti-theft header source content file more article good practical effective straight into single knife domain name beginning situation article core case see Apple Docker Huawei Linux macOS MariaDB Microsoft MySQL NVidia OPPO Reno Xiaomi MariaDB macOS Shulou Tech Info Shulou Technology