NGINX is widely used to deliver MP4 and FLV video content using progressive downloads or HTTP pseudo-streaming. Progressive downloads rely on the efficiency of the streaming server to handle disk I/O and concurrent connections, while MP4 or FLV pseudo-streaming capabilities also allow the clients to use a simple “trick-play” technique to seek to the desired position in a video stream without needing to download the entire resource.

NGINX Plus extends this capability to support adaptive streaming for video-on-demand (VOD) applications with Apple HLS and Adobe HDS, and Flash-based services using RTMP. Among other benefits, adaptive streaming allows the video player to select the most appropriate bit-rate in real-time.

NGINX Plus can also impose smart bandwidth limits on individual MP4 streams to prevent fast clients and download accelerators from using unnecessary resources, and the enhanced session logging capabilities of NGINX Plus reduce the overhead of the standard HTTP request logging with HTTP streaming media.

Delivering Streaming Media with NGINX Plus

HLS Video on Demand

The HLS/VOD module in NGINX Plus provides HTTP Live Streaming (HLS) support for H.264/AAC encoded content packaged in MP4 file containers (filename extensions .mp4, .m4v, and .m4a). With the HLS/VOD module, there’s no need to repackage existing MP4 content when introducing adaptive streaming to users – the content is “transformed” or “transmultiplexed” on the fly from the MP4 file container to HLS. The NGINX Plus module performs real-time segmentation, packetization, and multiplexing from the MP4 file container to HLS/MPEG-TS without recoding the content.

Before clients begin downloading media segments, they first request a manifest (filename extension .m3u8). The HLS/VOD module generates the playlist on the fly, so you do not need to manually describe the segment structure.

You can configure NGINX to serve HLS streams from a particular location, as in this example:


location /hls/ {
    hls;  # Use the HLS handler to manage requests

    # Serve content from the following location
    alias /var/www/video;

    # HLS parameters
    hls_fragment            8s;
    hls_buffers         10 10m;
    hls_mp4_buffer_size     1m;
    hls_mp4_max_buffer_size 5m;
}

HLS clients first request the .m3u8 manifest for a file in the location, then begin downloading the segments of video specified in the file. Again, NGINX Plus handles the task of segmenting MP4-packaged content on the fly for HLS streaming.

In addition, the HLS functionality can be used along with the NGINX secure link module to generate authorized, time-limited links based on unique client data such as a cookie or source IP address. This provides a strong degree of protection against misuse of the video service.

HDS Adaptive Streaming

Adobe’s HTTP Dynamic Streaming (HDS) provides an alternative method for delivering adaptive streaming media to your users. It functions in a similar fashion to Apple’s HLS, but uses different file formats.

Video is first prepared using Adobe’s f4fpackager tool, which generates manifest (filename extension .f4m), fragment (.f4f), and index (.f4x) files. These files are then published to the web server, and NGINX Plus’ f4f handler delivers them.

location /hds/ {
    f4f;  # Use the HDS handler to manage requests

    # Serve content from the following location
    alias /var/www/video;
}

Session Log Aggregation

Video clients that access content using HDS or HLS generally issue a great many HTTP requests for the video fragments. The resulting request log can be very verbose and difficult to analyze.

To make logs of streaming media downloads more useful, NGINX Plus’ session log aggregation feature generates more concise logs that combine multiple HTTP requests into a single log file entry. All of the requests in a single time-limited session are combined, and the total data transferred is logged. All other parameters are taken from the first request in the session.

You can enable session log aggregation for requests to the media location and use standard logging for all other client requests, to preserve the detail in your request logs.

MP4 Bandwidth Control

To apply limits to HTTP traffic downloads, use the limit_rate and limit_rate_after directives. These directives define limits in terms of bandwidth (bytes per second).

NGINX Plus also offers smart limits for MP4 content. The mp4_limit_rate and mp4_limit_rate_after directives define bandwidth limits in terms of the bit-rate of the downloaded file, and the time after which to apply the bandwidth limit.

The following sample configuration allows MP4 files to stream without limit for 15 seconds to let clients read ahead, then applies a bandwidth limit of 120% the bit-rate of the MP4 file.

location /video/ {
    mp4;
    mp4_limit_rate_after 15s;
    mp4_limit_rate       1.2;
}

This helps to avoid excessive waste of bandwidth resources when clients use “trick-play” capabilities to skip forwards through video files, and it reduces the impact of video download accelerators.