Skip to content

Instantly share code, notes, and snippets.

@seanhsu0102
Last active January 25, 2020 21:49
Show Gist options
  • Save seanhsu0102/03b839b3cbfa9a87bcb9dc403efe793d to your computer and use it in GitHub Desktop.
Save seanhsu0102/03b839b3cbfa9a87bcb9dc403efe793d to your computer and use it in GitHub Desktop.
nginx secure link
# Nginx config example
```
location /asset {
secure_link $arg_md5,$arg_expires;
secure_link_md5 "secret$uri$arg_expires";
if ($secure_link = "") {
return 403;
}
if ($secure_link = "0") {
return 410;
}
}
```
# python md5 example
```
from hashlib import md5
import time
# sign url to nginx secure link
def sign_url(url, expire, secret):
expire_time = str(int(time.time()+expire))
signurl = md5(secret + url + expire_time).digest().encode('base64')[:-1].replace('+/', '-_').strip('=')
return signurl, expire_time
```
# URL example
http://example.com/asset/cool.mp3?md5=1OULnuvGJVQvca6QnATewQ&expires=1461305812
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment