> For the complete documentation index, see [llms.txt](https://wisdom.gitbook.io/gyan/llms.txt). Markdown versions of documentation pages are available by appending `.md` to page URLs; this page is available as [Markdown](https://wisdom.gitbook.io/gyan/nginx/streaming-and-media-delivery-in-nginx.md).

# Streaming & Media Delivery in NGINX

***

### 1. Configuring RTMP & HLS Streaming

#### Overview

For live streaming applications, RTMP (Real-Time Messaging Protocol) is a common choice to ingest video streams from broadcasters. However, delivering these streams to diverse devices often requires converting them into HLS (HTTP Live Streaming), a protocol that supports adaptive bitrate and works on most platforms.

#### Key Configuration Elements

* **RTMP Module:** Enables NGINX to accept and process RTMP streams.
* **HLS Conversion:** Automatically segments the incoming stream into small chunks that can be served via HLS.
* **Live Streaming Settings:** Options such as chunk size, recording behavior, and HLS-specific parameters like fragment duration and playlist length.

#### Example Configuration

Below is an example of an NGINX configuration that sets up a basic RTMP server with HLS output:

```nginx
rtmp {
    server {
        listen 1935;          # Standard RTMP port
        chunk_size 4096;      # Size of each data chunk

        application live {
            live on;                  # Enable live streaming
            record off;               # Disable recording of the stream

            # HLS Settings: Convert RTMP stream to HLS
            hls on;                   # Enable HLS
            hls_path /tmp/hls;        # Directory to store HLS segments
            hls_fragment 5s;          # Duration of each HLS segment
            hls_playlist_length 1m;   # Total duration of the HLS playlist
        }
    }
}
```

#### Why This Configuration?

* **Low Latency and Adaptability:** RTMP is excellent for ingesting live streams with low latency, while HLS provides adaptive streaming across a variety of devices.
* **Simplicity and Performance:** The configuration offloads much of the heavy lifting to NGINX, enabling efficient use of server resources.
* **Scalability:** By segmenting streams into HLS chunks, you allow for easy scaling using standard HTTP caching and CDN distribution.

***

### 2. Video Caching & Optimization Strategies

#### Overview

Serving video content efficiently is a critical aspect of a high-performance media delivery system. Video caching minimizes redundant requests to backend servers, reduces latency, and saves bandwidth. NGINX can be configured as a caching proxy to store frequently accessed video segments.

#### Key Techniques

* **Proxy Caching:** Define cache zones to store video content for a specified duration.
* **Cache Validation:** Set appropriate caching rules based on HTTP status codes.
* **Header Management:** Use custom headers to monitor cache performance and status.

#### Example Configuration

Here’s how you can configure NGINX to cache video files coming from a backend server:

```nginx
http {
    # Define a caching zone for video content
    proxy_cache_path /var/cache/nginx levels=1:2 keys_zone=video_cache:10m max_size=10g inactive=60m use_temp_path=off;

    server {
        listen 80;
        server_name video.example.com;

        location /videos/ {
            proxy_pass http://backend_video_server;  # Upstream video server
            proxy_cache video_cache;                 # Enable caching using the defined zone
            proxy_cache_valid 200 302 10m;             # Cache successful responses for 10 minutes
            proxy_cache_valid 404 1m;                  # Shorter cache for not found responses
            add_header X-Cache-Status $upstream_cache_status;  # Expose cache status for debugging
        }
    }
}
```

#### Why These Strategies?

* **Reduced Server Load:** By caching popular video segments, you offload repetitive requests from backend servers, enabling them to serve new or dynamic content more effectively.
* **Enhanced User Experience:** Lower latency and faster load times improve playback smoothness and reduce buffering.
* **Operational Insight:** Custom headers like `X-Cache-Status` provide insight into caching efficiency, which is critical for ongoing performance tuning in production.

***

### 3. WebSockets & Low-Latency Streaming with NGINX

#### Overview

For applications that demand real-time interactivity—such as live chats, gaming, or financial tickers—WebSockets are the protocol of choice. They offer full-duplex communication channels over a single TCP connection, reducing latency significantly compared to traditional HTTP polling.

#### Key Configuration Elements

* **HTTP Upgrade Headers:** Ensure proper handling of the protocol upgrade from HTTP to WebSocket.
* **Persistent Connections:** Maintain long-lived connections without timeouts or interruptions.
* **Proxy Settings:** Correctly forward WebSocket requests to backend servers that handle real-time messaging.

#### Example Configuration

Below is an NGINX configuration example to support WebSocket connections:

```nginx
server {
    listen 80;
    server_name ws.example.com;
    
    location /ws/ {
        proxy_pass http://backend_websocket_server;  # Upstream WebSocket server
        proxy_http_version 1.1;                      # Use HTTP/1.1 for WebSocket compatibility
        proxy_set_header Upgrade $http_upgrade;      # Pass the Upgrade header from client
        proxy_set_header Connection "upgrade";       # Ensure connection is upgraded
        proxy_set_header Host $host;                 # Forward original host header
        proxy_set_header X-Real-IP $remote_addr;     # Forward client IP for logging/security
    }
}
```

#### Why Use WebSockets with NGINX?

* **True Real-Time Communication:** WebSockets eliminate the need for constant HTTP polling, reducing latency and server overhead.
* **Scalability:** Properly configured WebSocket support enables efficient handling of numerous simultaneous connections.
* **Enhanced Interactivity:** Ideal for applications requiring continuous, low-latency updates—whether it's for collaborative apps, live data feeds, or interactive media services.

***
