本文為您介紹EdgeScript的定製化鑒權邏輯、定製化要求標頭和回應標頭控制、定製化改寫和重新導向、定製化緩衝控制和定製化限速的情境樣本。
定製化鑒權規則
自訂鑒權規則情境樣本如下:
需求
請求URL格式:
/path/digest/?.ts?key=&t=。針對
.ts類請求,自訂防盜鏈需求如下:規則1:參數
t或參數key不存在,響應403,增加回應標頭X-AUTH-MSG標識鑒權失敗原因。規則2:參數
t表示到期時間,若參數t小於目前時間,則響應403,增加回應標頭X-AUTH-MSG標識鑒權失敗原因(此類鑒權注意關注用戶端與CDN側取時間可能會有差異,用戶端請求URL中攜帶的時間快慢都可能會影響到鑒權結果,導致鑒權失敗)。規則3:
md5(私密金鑰 + path + 檔案名稱.尾碼)==digest;若digest不匹配,響應403。
對應的EdgeScript規則
#鑒權類型的判斷 if eq(substr($uri, -3, -1), '.ts') { #參數是否存在判斷 if or(not($arg_t), not($arg_key)) { add_rsp_header('X-AUTH-MSG', 'auth failed - missing necessary arg') exit(403) } #是否為數字判斷 t = tonumber($arg_t) if not(t) { add_rsp_header('X-AUTH-MSG', 'auth failed - invalid time') exit(403) } #鑒權時間是否到期判斷 if gt(now(), t) { add_rsp_header('X-AUTH-MSG', 'auth failed - expired url') exit(403) } #鑒權演算法,對請求進行正則提取 pcs = capture_re($request_uri,'^/([^/]+)/([^/]+)/([^?]+)%?(.*)') sec1 = get(pcs, 1) sec2 = get(pcs, 2) sec3 = get(pcs, 3) if or(not(sec1), not(sec2), not(sec3)) { add_rsp_header('X-AUTH-MSG', 'auth failed - malformed url') exit(403) } key = 'b98d643a-9170-4937-8524-6c33514bbc23' #待簽算的鑒權串拼接 signstr = concat(key, sec1, sec3) digest = md5(signstr) #簽算和請求中的token串比較 if ne(digest, sec2) { add_rsp_header('X-AUTH-DEBUG', concat('signstr: ', signstr)) add_rsp_header('X-AUTH-MSG', 'auth failed - invalid digest') exit(403) } }
定製化要求標頭和回應標頭控制
檔案自動重新命名情境樣本如下:
樣本:
add_rsp_header('Content-Disposition', concat('attachment;filename=', tochar(34), filename, tochar(34)))通過在HTTP應答中添加回應標頭“Content-Disposition:attachment”來實現訊息體的強制下載,並且有參數
filename時,自動重新命名為filename,無參數時,使用預設命名。filename增加雙引號,34為雙引號的ascii,可經tochar轉回字串。
輸出:
Content-Disposition: attachment;filename="monitor.apk"對應的EdgeScript規則:
if $arg_filename {
hn = 'Content-Disposition'
hv = concat('attachment;filename=', $arg_filename)
add_rsp_header(hn, hv)
}定製化改寫和重新導向
定製化改寫和重新導向情境樣本如下:
精確URI改寫
需求
將使用者請求
/hello在CDN內部改寫成/index.html,回源和緩衝的URI都將變成/index.html,參數保持原樣。對應的EdgeScript規則
if match_re($uri, '^/hello$') { rewrite('/index.html', 'break') }
檔案尾碼改寫
需求
將使用者請求的URI資訊
/1.txt,在CDN節點上改寫成/1.<url參數type>,即,使用請求URL中攜帶的type參數值來替換URI中的檔案尾碼,例如:/1.txt?type=mp4將會被改成/1.mp4?type=mp4然後回源擷取檔案,並緩衝在CDN節點上。對應的EdgeScript規則
if and(match_re($uri, '^/1.txt$'), $arg_type) { rewrite(concat('/1.', $arg_type), 'break') }
檔案尾碼小寫化
需求
將URI改成小寫。
對應的EdgeScript規則
pcs = capture_re($uri, '^(.+%.)([^.]+)') section = get(pcs, 1) postfix = get(pcs, 2) if and(section, postfix) { rewrite(concat(section, lower(postfix)), 'break') }
添加URI首碼
需求
將使用者請求
^/nn_live/(.*),在CDN節點上改寫為/3rd/nn_live/$1。對應的EdgeScript規則
pcs = capture_re($uri, '^/nn_live/(.*)') sec = get(pcs, 1) if sec { dst = concat('/3rd/nn_live/', sec) rewrite(dst, 'break') }
302重新導向
需求
將根目錄
/,302重新導向到/app/movie/pages/index/index.html頁面。對應的EdgeScript規則
if eq($uri, '/') { rewrite('/app/movie/pages/index/index.html', 'redirect') }
302重新導向HTTPS
需求
將如下URI(對根目錄匹配,
^/$)跳轉到https://demo.aliyundoc.com/index.html,跳轉後的URI可按需填寫。http://demo.aliyundoc.comhttps://demo.aliyundoc.com
對應的EdgeScript規則
if eq($uri, '/') { rewrite('https://demo.aliyundoc.com/index.html', 'redirect') }
定製化緩衝控制
自訂緩衝時間長度的情境範例如下:
需求
根據各類條件,自訂資源緩衝時間長度。
對應的EdgeScript規則
if match_re($uri, '^/image') { set_cache_ttl('code', '301=10,302=5') } if eq(substr($uri, -4, -1), '.mp4') { set_cache_ttl('path', 5) } if match_re($uri, '^/201801/mp4/') { set_cache_ttl('path', 50) } if match_re($uri, '^/201802/flv/') { set_cache_ttl('path', 10) }說明/image開頭的URI,針對響應碼設定緩衝時間長度,301緩衝10s,302緩衝5s。
定製化限速
自訂限速值的情境範例如下:
需求
如果有參數
sp和unit,則實施限速。sp參數指明限速數值,unit為參數單位,KB或MB。對應的EdgeScript規則
if and($arg_sp, $arg_unit) { sp = tonumber($arg_sp) if not(sp) { add_rsp_header('X-LIMIT-DEBUG', 'invalid sp') return false } if and(ne($arg_unit, 'k'), ne($arg_unit, 'm')) { add_rsp_header('X-LIMIT-DEBUG', 'invalid unit') return false } add_rsp_header('X-LIMIT-DEBUG', concat('set on: ', sp, $arg_unit)) limit_rate(sp, $arg_unit) return true }
地區、電訊廠商訪問限制
地區、電訊廠商訪問限制的情境範例如下:
需求
通過識別用戶端請求中攜帶的IP地址的地區、電訊廠商歸屬來實現訪問限制。
識別用戶端IP的地區、電訊廠商歸屬的函數如下。詳細請參見請求判斷相關。
client_region:使用該函數可以返回用戶端IP歸屬地區的編碼。client_isp:使用該函數可以返回用戶端IP歸屬電訊廠商的編碼。
對應的EdgeScript規則
# 省份限制,省份不匹配的,禁止訪問 ip_region_id=client_region() if not(match_re(ip_region_id, '440000|370000')) { add_rsp_header('X-REGION-BLOCK-DEBUG', concat('hit ip_region_id:', ip_region_id)) exit(403) } # 電訊廠商限制,電訊廠商不匹配的,禁止訪問 ip_isp_id=client_isp() if not(match_re(ip_isp_id, '100017|100025')) { add_rsp_header('X-REGION-BLOCK-DEBUG', concat('hit ip_isp_id:', ip_isp_id)) exit(403) }