Complete steps for Nginx to configure anti-hotlinking

Complete steps for Nginx to configure anti-hotlinking

need:

Usually, sites want to prevent videos and pictures on their websites from being stolen. After all, video traffic costs a lot of money (rich people don’t have to consider this).

1. Straight to the point, first upload the nginx configuration file

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;
}

}

2. Interpretation of the core configuration file of anti-hotlink

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 links, as follows, otherwise enter $invaild_refere and return 403 forbidden.

1. none

"Referer" source header is empty

2. blocked

The "Referer" source header is not empty, but the value is deleted by a proxy or firewall, and none of the values ​​start with http:// or https://.

3. server_names

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

3. Simulation Case Test

1. Add --referer to simulate reference and see the result is 403 directly. This proves 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 


Summarize

The above is the full content of this article. I hope that the content of this article will have certain reference learning value for your study or work. Thank you for your support of 123WORDPRESS.COM.

You may also be interested in:
  • Implementation code of Nginx anti-hotlink and optimization in Linux
  • 3 ways to prevent hotlinks in Nginx
  • Nginx Anti-Hotlink Configuration Method
  • Introduction to the method of anti-hotlinking under Nginx server
  • Nginx image hotlink protection configuration example
  • How to configure Nginx's anti-hotlinking
  • The whole process of Nginx anti-hotlink and service optimization configuration

<<:  Installation tutorial of mysql 5.7 under CentOS 7

>>:  js to achieve simple calendar effect

Recommend

Detailed explanation of the payment function code of the Vue project

1. Alipay method: Alipay method: Click Alipay to ...

Mini Program to Implement the Complete Shopping Cart

The mini program implements a complete shopping c...

Detailed explanation of MySQL/Java server support for emoji and problem solving

This article describes the support and problem so...

MySQL chooses the right storage engine

When it comes to databases, one of the most frequ...

Extract specific file paths in folders based on Linux commands

Recently, there is a need to automatically search...

Rounding operation of datetime field in MySQL

Table of contents Preface 1. Background 2. Simula...

Best Practices Guide for MySQL Partitioned Tables

Preface: Partitioning is a table design pattern. ...

Bootstrap FileInput implements image upload function

This article example shares the specific code of ...

Use render function to encapsulate highly scalable components

need: In background management, there are often d...

How to force vertical screen on mobile pages

I recently wrote a mobile page at work, which was...

Implementation code for using mongodb database in Docker

Get the mongo image sudo docker pull mongo Run th...

Nginx memory pool source code analysis

Table of contents Memory Pool Overview 1. nginx d...

Implementation of static website layout in docker container

Server placement It is recommended to use cloud s...

Docker deploys Mysql, .Net6, Sqlserver and other containers

Table of contents Install Docker on CentOS 8 1. U...