Nginx + LUA Resource anti-theft chain

Source: Internet
Author: User
Tags key string lua
Instructions for use

1. Nginx installation of LUA extensions

2. Modify the configuration Nginx configuration file

    1. Set $salt "Cmstopsalt"; MD5 key string
    2. Set $expire "2"; Expiry time
    3. Set $timeType ' seconds '; Expiration time type seconds to seconds it's value is minutes

3. Save the following code as a LUA file

--Get MD5 value

function GetMd5 (time)

Return Ngx.md5 (Ngx.var.salt. Time)

End

--Get parameters

Local args = Nil

If "GET" = = Ngx.var.request_method Then

args = Ngx.req.get_uri_args ()

End

Local key = Args[' key ']

local time = Tonumber (args[' time ')

--Get the allowed time range

local ur = 0;

Local interval = 1;

If Ngx.var.timeType ~= ' seconds ' then

Interval = 60;

End

--Verify that key is valid or not expired

If time ~= nil and key ~= nil and string.len (key) = = + GETMD5 (time) = = key and Time + Tonumber (ngx.var.expire) * inte Rval >= Tonumber (Os.time ()) Then

ur = 1;

End

if 0 = = ur Then

Ngx.exit (403)

End


4. Add the following code to the Nginx configuration file for the project

Location ~ \.m3u8$ {

Rewrite_by_lua_file ' Lua file address ';

}

5. PHP generated key rules and parameter settings

The values defined in Nginx are as follows: Cmstopsalt

index.m3u8?key= &time=

The above describes the Nginx + LUA resource anti-theft chain, including the aspects of the content, I hope that the PHP tutorial interested in a friend helpful.

  • Related Article

    Contact Us

    The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

    If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

    A Free Trial That Lets You Build Big!

    Start building with 50+ products and up to 12 months usage for Elastic Compute Service

    • Sales Support

      1 on 1 presale consultation

    • After-Sales Support

      24/7 Technical Support 6 Free Tickets per Quarter Faster Response

    • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.