How to build file anti-leeching service in Nginx
Preface
Everyone knows that many websites now charge for downloading information. Whether it is points or gold coins, if you want to free it, you can only say that there are very few. So these websites How to prevent resource hotlinking?
Here is a relatively easy-to-use artifact. nginx itself provides secure_link to complete the anti-leeching function. It can add timestamps and check codes to server file links, thereby protecting server files from being downloaded and stolen.
Timing diagram
nginx configuration
How to install nginx I won’t go into details here. Remember to turn it on when installing. ngx_http_secure_link_module is enough.
./configure --with-http_secure_link_module #编译nginx时加入
Installation completion detection:
nginx -v
If the following appears, the configuration is successful:
configure arguments: --with-http_secure_link_module --prefix=/usr/local /nginx --with-http_stub_status_module
Instance configuration
server { listen 80; server_name download.52itstyle.com; charset utf-8; location / { #这里配置了2个参数一个是md5,一个是expires secure_link $arg_md5,$arg_expires; #md5的哈希格式为 secret+url+expires,expires为时间戳单位s,url为请求地址 secure_link_md5 52itstyle$uri$arg_e; #这里我们的md5是我们按照secure_link_md5的方式计算的哈希,secure_link会比对它计算的哈希值是否与我们的md5参数一致 if ($secure_link = "") { #资源不存在或哈希比对失败 return 402; } if ($secure_link = "0") { #失效超时 return 405; } #重命名文件名 add_header content-disposition "attachment;filename=$arg_f"; alias /data/site/down.52itstyle.com/; } error_page 500 502 503 504 /50x.html; error_page 402 405 /40x.html; location = /50x.html { root html; } location = /40x.html { root html; } }
Parameter details
secure_link
Syntax: secure_link expression;
Default value: None
Configuration section: http, server, location
expression is expired by the check value and It consists of time, in which the check value will be compared with the md5 hash value of the specified parameter in secure_link_md5.
If the two values are inconsistent, the value of the $secure_link variable is empty; if the two values are consistent, an expiration check is performed; if it has expired, the value of the $secure_link variable is 0; if it has not expired, it is 1 .
If the link is time-sensitive, then the expiration time is set with a timestamp, declared after the md5 hash value, separated by commas. If no expiration time is set, the link will be valid forever.
secure_link_md5
Syntax: secure_link_md5 expression;
Default value: None
Configuration section: http, server, location
expression specifies the parameters for calculating the md5 hash value. The md5 value will be compared and verified with the md5 value passed in the URL. Expression generally contains uri (for example, demo.com/s/link uri is /s/link) and encryption key secret. If the link is time-sensitive, expression needs to contain $secure_link_expires. Expression can also add client information, such as access IP, browser version information, etc.
java backend configuration
Case, for reference only:
import org.apache.commons.codec.binary.base64; import org.apache.commons.codec.digest.digestutils; /** * 生成加密連接 */ public class securelink { private static string site = "https://down.52itstyle.com/"; private static string secret = "52itstyle"; public static string createlink(string path,string filename){ string time = string.valueof((system.currenttimemillis() / 1000) + 300); // 5分钟有效 string md5 = base64.encodebase64urlsafestring(digestutils.md5(secret + path + time)); string url = site + path + "?md5=" + md5 + "&expires=" + time + "&f="+filename; return url; } public static void main(string[] args) { //https://down.52itstyle.com/2018101025689452.pdf?md5=fndyyfzcooi9q8sh1ffkxg&expires=1539847995&f=分布式秒杀架构.pdf system.out.println(createlink("2018101025689452.pdf","分布式秒杀架构.pdf")); } }
The above is the detailed content of How to build file anti-leeching service in Nginx. For more information, please follow other related articles on the PHP Chinese website!

Hot AI Tools

Undresser.AI Undress
AI-powered app for creating realistic nude photos

AI Clothes Remover
Online AI tool for removing clothes from photos.

Undress AI Tool
Undress images for free

Clothoff.io
AI clothes remover

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

Hot Article

Hot Tools

Notepad++7.3.1
Easy-to-use and free code editor

SublimeText3 Chinese version
Chinese version, very easy to use

Zend Studio 13.0.1
Powerful PHP integrated development environment

Dreamweaver CS6
Visual web development tools

SublimeText3 Mac version
God-level code editing software (SublimeText3)

Hot Topics

You can query the Docker container name by following the steps: List all containers (docker ps). Filter the container list (using the grep command). Gets the container name (located in the "NAMES" column).

How to configure Nginx in Windows? Install Nginx and create a virtual host configuration. Modify the main configuration file and include the virtual host configuration. Start or reload Nginx. Test the configuration and view the website. Selectively enable SSL and configure SSL certificates. Selectively set the firewall to allow port 80 and 443 traffic.

How to configure an Nginx domain name on a cloud server: Create an A record pointing to the public IP address of the cloud server. Add virtual host blocks in the Nginx configuration file, specifying the listening port, domain name, and website root directory. Restart Nginx to apply the changes. Access the domain name test configuration. Other notes: Install the SSL certificate to enable HTTPS, ensure that the firewall allows port 80 traffic, and wait for DNS resolution to take effect.

How to confirm whether Nginx is started: 1. Use the command line: systemctl status nginx (Linux/Unix), netstat -ano | findstr 80 (Windows); 2. Check whether port 80 is open; 3. Check the Nginx startup message in the system log; 4. Use third-party tools, such as Nagios, Zabbix, and Icinga.

The methods that can query the Nginx version are: use the nginx -v command; view the version directive in the nginx.conf file; open the Nginx error page and view the page title.

Create a container in Docker: 1. Pull the image: docker pull [mirror name] 2. Create a container: docker run [Options] [mirror name] [Command] 3. Start the container: docker start [Container name]

Docker container startup steps: Pull the container image: Run "docker pull [mirror name]". Create a container: Use "docker create [options] [mirror name] [commands and parameters]". Start the container: Execute "docker start [Container name or ID]". Check container status: Verify that the container is running with "docker ps".

Starting an Nginx server requires different steps according to different operating systems: Linux/Unix system: Install the Nginx package (for example, using apt-get or yum). Use systemctl to start an Nginx service (for example, sudo systemctl start nginx). Windows system: Download and install Windows binary files. Start Nginx using the nginx.exe executable (for example, nginx.exe -c conf\nginx.conf). No matter which operating system you use, you can access the server IP
