Serving large video files efficiently requires specific Nginx configuration beyond typical web content serving — this guide covers the key settings for smooth, efficient large file/video delivery.
Why Default Nginx Configuration Isn't Optimal for Large Files
Default settings are tuned for typical web content (smaller files, many concurrent requests) — large video file delivery has different characteristics (sustained transfer, seeking/range requests, sensitivity to buffering configuration) warranting specific tuning.
Enabling Range Request Support
location /videos/ {
add_header Accept-Ranges bytes;
}
Range requests let clients request specific byte ranges (essential for video seeking/scrubbing without downloading the entire file) — Nginx supports this by default for static files, but explicitly confirming the header helps ensure clients recognize the capability.
Using sendfile for Efficient File Serving
sendfile on;
sendfile_max_chunk 1m;
sendfile lets the OS handle file transfer directly (kernel-level, bypassing userspace copying) — significantly more efficient than Nginx reading and writing file data through userspace for large file transfers.
Configuring tcp_nopush and tcp_nodelay
tcp_nopush on;
tcp_nodelay on;
tcp_nopush optimizes packet sending for sendfile-based transfers; tcp_nodelay ensures the final small chunk isn't delayed — together, a commonly recommended combination for efficient large file delivery.
Tuning Buffer Sizes for Large Files
output_buffers 2 1m;
Larger output buffers can improve throughput for large file transfers compared to default smaller buffer sizes tuned for typical web content.
Disabling Unnecessary Gzip for Already-Compressed Video
location ~ \.(mp4|mkv|webm)$ {
gzip off;
}
Video files are already compressed — attempting gzip compression wastes CPU without meaningful size benefit; explicitly disable gzip for video file types.
Setting Appropriate Timeouts for Large Transfers
send_timeout 300s;
client_body_timeout 300s;
Default timeouts tuned for typical web requests may be too short for large file transfers, particularly for slower connections — increase timeouts appropriately for your expected file sizes and typical client connection speeds.
Rate Limiting to Prevent Bandwidth Monopolization
location /videos/ {
limit_rate 5000k;
}
Consider whether limiting per-connection transfer rate is appropriate — prevents a single download from monopolizing all available bandwidth, potentially degrading experience for other concurrent users on a shared VPS.
Using a CDN for Genuinely High-Traffic Video Delivery
See How to Optimize Video Delivery with a CDN for Streaming Content — for significant traffic volume, offloading video delivery to a CDN reduces load on your origin server far more effectively than origin-level Nginx tuning alone can achieve.
Testing Actual Delivery Performance
Test seeking/scrubbing behavior and sustained transfer speed under realistic conditions after configuration changes — confirms genuine improvement rather than assuming configuration changes automatically translate to better real-world video delivery experience.
Common Errors
Video seeking/scrubbing doesn't work properly — verify range request support is genuinely functioning (test with curl -r for a specific byte range), and confirm no intermediate proxy/CDN layer is stripping range request support.
Continue Reading
- How to Optimize Video Delivery with a CDN for Streaming Content
- How to Set Up HLS Streaming with Nginx
- VPS Requirements for Media Streaming Servers
Browse more articles in Media & Streaming Servers.