location /s/ { secure_link $arg_md5,$arg_expires; secure_link_md5 "$secure_link_expires$uri$remote_addr secret"; if ($secure_link = "") { return 403; } if ($secure_link = "0") { return 410; } ... }
# ./configure --with-http_secure_link_module \ --prefix=/usr/local/nginx-1.4.2 --with-http_stub_status_module # make # make install
server { listen 80; server_name s1.down.ttlsa.com; access_log /data/logs/nginx/s1.down.ttlsa.com.access.log main; index index.html index.php index.html; root /data/site/s1.down.ttlsa.com; location / { secure_link $arg_st,$arg_e; secure_link_md5 ttlsa.com$uri$arg_e; if ($secure_link = "") { return 403; } if ($secure_link = "0") { return 403; } } }
<?php # 做用:生成 nginx secure link 連接 # 站點: www.ttlsa.com # 做者:涼白開 # 時間: 2013-09-11 $secret = 'ttlsa.com'; # 密鑰 $path = '/web/nginx-1.4.2.tar.gz'; # 下載文件 # 下載到期時間,time 是當前時間,300 表示 300 秒,也就是說從如今到 300 秒以內文件不過時 $expire = time()+300; # 用文件路徑、密鑰、過時時間生成加密串 $md5 = base64_encode(md5($secret . $path . $expire, true)); $md5 = strtr($md5, '+/', '-_'); $md5 = str_replace('=', '', $md5); # 加密後的下載地址 echo '<a href=http://s1.down.ttlsa.com/web/nginx-1.4.2.tar.gz?st='.$md5.'&e='.$expire.'>nginx-1.4.2</a>'; echo '<br>http://s1.down.ttlsa.com/web/nginx-1.4.2.tar.gz?st='.$md5.'&e='.$expire; ?>