Building a Private CDN for HLS Video Distribution with Nginx Secure Link
This guide explains how to build a private Content Delivery Network (CDN) for distributing HLS video with long-term download protection using Nginx Secure Link. The system uses...
This guide explains how to build a private Content Delivery Network (CDN) for distributing HLS video with long-term download protection using Nginx Secure Link. The system uses Nginx as an edge server with the Secure Link module to generate short-lived signed URLs, preventing unauthorized downloads. The guide covers Nginx configuration, secure key generation, signed URL creation, and verification. This solution is ideal for businesses requiring high-security delivery of internal or copyrighted video content.
Overview of Private CDN and HLS
A private CDN allows organizations to distribute video content securely and efficiently over their own infrastructure. In this setup, we use HLS (HTTP Live Streaming), a widely adopted format for online video, especially on mobile devices and web browsers. HLS splits video into small segments and creates a playlist (m3u8) for sequential playback. To protect content from unauthorized downloading, we use Nginx Secure Link, a module that generates URLs with short lifetimes and digital signatures. Only URLs with valid signatures are served, preventing downloads via tools like wget or unauthorized browsers.
Prerequisites and Environment Setup
To deploy this system, you need a VPS or dedicated server running Ubuntu 20.04/22.04. We will use Nginx with the Secure Link module pre-compiled. If you don't have a server, consider NVMe Cloud VPS for Docker, Nginx & Databases or High-Performance Dedicated Servers for Production Clusters.
Prepare the following tools:
- SSH client to access the server
- Python or Bash scripting language for generating signed URLs
- HLS video content already split into .ts segments and an .m3u8 playlist
Configuring Nginx with Secure Link
First, install Nginx if it's not already present:
sudo apt update
sudo apt install nginx -y
Ensure Nginx is compiled with the Secure Link module. For Ubuntu repository packages, this is usually included. Verify with:
nginx -V 2>&1 | grep -o "nginx/.*\smodules" | grep -o "secure_link"
If the command returns a line containing "secure_link", the module is available. Otherwise, you must compile Nginx from source with the --with-http_secure_link_module option.
Create a directory for HLS video content:
sudo mkdir -p /var/www/hls
sudo chown -R www-data:www-data /var/www/hls
Copy your .ts segments and .m3u8 playlist files into this directory.
Edit the Nginx configuration file, typically located at /etc/nginx/sites-available/default or create a new file in /etc/nginx/sites-available/:
server {
listen 80;
server_name your-domain.com;
location /hls/ {
alias /var/www/hls/;
secure_link $arg_st $arg_e;
secure_link_md5 "$arg_st$arg_e$arg_key";
if ($secure_link = "") {
return 403;
}
if ($secure_link = "0") {
return 403;
}
}
}
Explanation:
secure_link $arg_st $arg_e;: Extracts the start time (st) and expiration time (e) from the URL query parameters.secure_link_md5 "$arg_st$arg_e$arg_key";: Generates a signature using MD5 from st, e, and the secret key (arg_key).if ($secure_link = "") { return 403; }: Returns 403 if the st parameter is missing.if ($secure_link = "0") { return 403; }: Returns 403 if the signature is invalid.
The secret key (arg_key) must be kept secure and changed periodically. You can store it in the configuration file or use environment variables.
Generating Signed URLs and Testing
To generate signed URLs, you need a script. Here is a Python example:
import hashlib
import time
import urllib.parse
def generate_secure_link(base_url, key, duration=300):
# Start time is current time
start_time = int(time.time())
# Expiration time is current time + duration (seconds)
expiration_time = start_time + duration
# Create signature
signature = hashlib.md5(f"{start_time}{expiration_time}{key}".encode()).hexdigest()
# Create URL
url = f"{base_url}?st={start_time}&e={expiration_time}&key={key}&signature={signature}"
return url
# Example usage
base_url = "https://your-domain.com/hls/video.m3u8"
key = "your_secret_key"
secure_url = generate_secure_link(base_url, key, duration=300)
print(secure_url)
Replace your_secret_key with your actual secret key. The duration can be adjusted as needed, typically between 300 and 3600 seconds.
Run the script to generate a URL. Then, open the URL in a browser or VLC to test playback. If configured correctly, the video should play. If not, check the Nginx error log at /var/log/nginx/error.log.
Testing and Maintenance
After configuration, test the download protection by attempting to download the video using wget:
wget "https://your-domain.com/hls/video.m3u8?st=1600000000&e=1600000000&key=your_secret_key&signature=invalid_signature"
It should return a 403 error. If it returns 200, recheck your Secure Link configuration.
For better security, consider combining with additional measures:
- Restrict access to specific IP addresses using
allowanddenydirectives in Nginx. - Use HTTPS to encrypt all communication.
- Rotate the secret key periodically.
For larger systems, you can deploy multiple edge servers and use a database or key distribution service to synchronize the secret keys.
References
For more information on Nginx Secure Link, refer to the official documentation: https://nginx.org/en/docs/http/ngx_http_secure_link_module.html.
Related infrastructure & engineering guides: Building a MicroVM Cluster with AWS Firecracker on Ubuntu VPS for Isolating Unsafe Background Automation Scripts
Recommended NVMe Cloud VPS Plans to Deploy This Setup
Enterprise RAID 10 NVMe SSDs, CMC & FPT District 9 uplinks with Anycast Anti-DDoS and 99.99% SLA.
VPS-3E
10% VAT included
- CPU: 2 vCPU
- RAM: 4 GB
- SSD: 50 GB NVMe
