## How to use Nginx ngx_http_secure_link_module and PHP Three example nginx locations and the PHP required for mp4 secure_links. ``` before: https://https://mydomain.com/data/videos/file.mp4 after : https://mydomain.com/data/videos/file.mp4?md5=Vtzs2WCnCqRsE47EH6U6pQ&expires=1617601227 ``` make sure the secret password you use match in both lines below ``` $remote_addr secretword << in nginx config section $secret = 'secretword'; << in PHP file ``` #### nginx location ``` # works location ~ \.mp4$ { secure_link $arg_md5,$arg_expires; secure_link_md5 "$secure_link_expires$uri$remote_addr secretword"; if ($secure_link = "") { return 403; } # deny direct links if ($secure_link = "0") { return 410; } # deny expired links } ``` #### or use option (2) ``` # works location ~ \.(mp3|mp4) { root /srv/www/webroot; secure_link $arg_md5,$arg_expires; secure_link_md5 "$secure_link_expires$uri$remote_addr secretword"; if ($secure_link = "") { return 403; } if ($secure_link = "0") { return 410; } } ``` #### or use option (3) ``` # works location ^~ /data/videos { alias /srv/www/webroot/data/videos; secure_link $arg_md5,$arg_expires; secure_link_md5 "$secure_link_expires$uri$remote_addr secretword"; if ($secure_link = "") { return 403; } if ($secure_link = "0") { return 410; } } ``` #### another optional way of handling status errors, 404 it all ``` location ~ \.mp4$ { secure_link $arg_md5,$arg_expires; secure_link_md5 "$secure_link_expires$uri$remote_addr secretword"; # In production environment, we should not reveal to potential attacker # why authentication has failed if ($secure_link != "1") { return 404; } } ``` #### secure_links.php will build and echo the expires URL ```