Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

How to build file hotlink protection service with Nginx

2025-01-17 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Internet Technology >

Share

Shulou(Shulou.com)05/31 Report--

This article mainly explains "how to build file hotlink protection service in Nginx". Interested friends may wish to take a look. The method introduced in this paper is simple, fast and practical. Next let the editor to take you to learn "Nginx how to build file hotlink protection service" it!

Preface

We all know that many sites now download materials are to charge, whether points or gold coins, want to be free can only say very little, so how do these websites achieve resource hotlink protection?

A relatively easy-to-use artifact is recommended here. Nginx itself provides secure_link to complete hotlink protection, which can add timestamps and check codes to server file links to protect server files from being downloaded and embezzled.

Time sequence diagram

Nginx configuration

How to install nginx will not be discussed here, just remember to turn on ngx_http_secure_link_module during installation.

. / configure-- with-http_secure_link_module # added when compiling nginx

Installation completion Test:

Nginx-v

If the following indicates that 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 / {# two parameters are configured here, one is md5, the other is expires secure_link $arg_md5,$arg_expires; # md5, the hash format is secret+url+expires,expires, the timestamp unit is sjingurl, and the request address secure_link_md5 52itstyle$uri$arg_e # here our md5 is the hash we calculated according to secure_link_md5. Secure_link will compare whether the hash calculated by it is consistent with our md5 parameter if ($secure_link = "") {# resource does not exist or hash pair failed return 402;} if ($secure_link = "0") {# invalidation timeout return 405 } # rename the file name add_header content-disposition "attachment;filename=$arg_f"; alias / data/site/down.52itstyle.com/;} error_page 500502 503 504 / 50x.htl; error_page 402405 / 40x.htl; location = / 50x.html {root html;} location = / 40x.html {root html;}}

Detailed explanation of parameters

Secure_link

Syntax: secure_link expression

Default value: none

Configuration segment: http, server, location

Expression consists of a check value and an expiration time, where the check value is 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 the same, the expiration check is performed; if it expires, the value of the $secure_link variable is 0; if it does not expire, it is 1.

If the link is timed, the expiration time is set with a timestamp, declared after the md5 hash value, separated by a comma. If the expiration time is not set, the link is permanent.

Secure_link_md5

Syntax: secure_link_md5 expression

Default value: none

Configuration segment: http, server, location

Expression specifies the parameter that calculates the md5 hash value, which will be checked against the MD5 value passed in url. Expression generally contains uri (for example, demo.com/s/link uri is / s/link) and encryption key secret. If the link is aged, expression needs to contain $secure_link_expires,expression and can also add client information, such as accessing 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;/** * generate encrypted connection * / 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-minute valid 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= distributed second kill architecture .pdf system.out.println (createlink ("2018101025689452.pdf", "distributed second kill architecture .pdf"));}} so far, I believe you have a better understanding of "how to build file hotlink protection service in Nginx". You might as well do it in practice! Here is the website, more related content can enter the relevant channels to inquire, follow us, continue to learn!

Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.

Views: 0

*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.

Share To

Internet Technology

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report