The nginx rtmp module is an open-source extension for the Nginx web server that enables real-time video streaming using the Real-Time Messaging Protocol (RTMP). It supports live broadcasting, video on demand, and conversion to HLS or DASH formats for broad viewer compatibility. Developers use it as a lightweight, cost-effective alternative to dedicated media servers for embedding streaming capabilities into web applications.
Low-latency live streaming is a core requirement for modern applications, from interactive broadcasts to real-time event coverage. Nginx, known primarily as a high-performance web server, becomes a capable streaming solution when extended with the nginx rtmp module. This module provides a lightweight, efficient way to ingest, process, and distribute live video without the overhead of a full commercial media server. By the end of this guide, you will understand the module's architecture, learn how to approach installation and configuration, explore advanced use cases like stream relay and transcoding, and know how to troubleshoot common issues.

Understanding the nginx rtmp module

The nginx rtmp module is defined as a third-party Nginx extension that implements the RTMP protocol to handle audio and video streaming. It works by receiving media streams from publishers, such as OBS or FFmpeg, and distributing them to viewers or downstream services. Core capabilities include live broadcasting, video on demand (VOD), recording streams to disk, and relaying streams to other servers. It also supports on-the-fly conversion to HLS or DASH, allowing browsers and mobile devices to play streams over standard HTTP connections.
The architecture is straightforward and highly modular. A publisher sends a video stream over RTMP to the Nginx server. The module processes this stream within an application context, which can then route the media to multiple viewers, push it to another server, or transcode it for adaptive bitrate delivery. This approach gives developers precise control over how media flows through their infrastructure, making it easy to build custom streaming pipelines.
Architecture Diagram

Installing the nginx rtmp module

Setting up the module requires careful preparation to ensure compatibility and performance. You need a compatible version of Nginx, a Linux-based operating system, and the Nginx HTTP SSL module if you plan to secure your streams. While Windows is technically supported for development purposes, it lacks the network I/O performance and stability of Linux for high-concurrency streaming, so a Linux environment is strongly recommended for production.
There are two primary installation paths: building from source or using pre-built packages. Building from source offers the most control, allowing you to compile the module directly into Nginx. This involves downloading the Nginx source code, cloning the module repository from GitHub, and running the configuration script to include the module before compilation. Package managers, such as those providing nginx-extras on certain Linux distributions, offer a faster route but may lag behind the latest module updates and security patches.
During compilation, developers often encounter missing dependencies. The module requires libraries for SSL and PCRE, and missing these will halt the build process immediately. Ensuring your build environment has the development headers for these libraries resolves most installation failures. For production deployments, building from source is generally preferred to ensure you have the latest security patches, module features, and optimal performance tailored to your specific hardware.

Core configuration directives

Configuring the module involves adding a dedicated RTMP block to your main Nginx configuration file, separate from the standard HTTP block. This block defines how Nginx listens for RTMP connections and how it processes incoming streams. The hierarchy is strict and must be followed precisely: the main block contains server blocks, which in turn contain application blocks. Each application acts as a distinct endpoint for your streams.
Key directives control every aspect of streaming behavior. The server directive specifies the listening port, typically 1935 for standard RTMP. The application directive defines a named endpoint, such as live or video on demand, that publishers and viewers connect to. Inside the application, the live directive enables live broadcasting, while the record directive determines whether streams are saved to disk. The push and pull directives enable stream relaying to other servers, and the chunk size directive influences the size of data chunks sent to viewers, directly affecting latency and throughput.
The rtmp auto push directive is critical for scaling. Because Nginx uses a multi-worker process model, an incoming stream is typically handled by a single worker. Without auto-push, only viewers connected to that specific worker can see the stream. Enabling this directive allows workers to share streams internally, ensuring any viewer can access the stream regardless of which worker received the initial publish request.
Architecture Diagram

Setting up a basic live stream

Creating a basic live stream involves defining a simple application within your configuration. You would create an application named live and enable the live directive. This establishes a dedicated endpoint where publishers can send their video and viewers can connect to watch. The simplicity of this setup makes it an excellent starting point for developers new to streaming infrastructure.
The publisher URL format follows a standard structure. It combines the server address, the application name, and a unique stream key. For example, a publisher using OBS would enter the server address as the Nginx host and port, and set the stream key to a unique identifier like myliveevent. Nginx matches the application name in the URL to the configured application block, routing the stream accordingly.
Viewers can access the stream in two primary ways. They can connect directly via RTMP using the same URL structure, which offers the lowest latency but requires a compatible player like VLC. Alternatively, you can configure the module to convert the RTMP stream into HLS. This involves adding an HTTP location block that serves the generated HLS playlist and segment files. Viewers can then watch the stream in any standard web browser or mobile player using a simple HTTP URL.
Testing the setup requires common broadcasting tools. You can use OBS to publish a stream and a player like VLC to verify playback. This confirms that the publisher connection is stable, the application routing is correct, and the stream is being distributed to viewers without errors. For more advanced interactive streaming needs, developers often explore VideoSDK's interactive live streaming capabilities.

Advanced features and use cases

The module supports several advanced features that extend its utility beyond simple live streaming. Multi-worker live streaming, enabled by the rtmp auto push directive, is essential for production deployments where high concurrency is expected. This ensures that all Nginx workers can serve the same stream, maximizing resource utilization and preventing viewer connection failures.
Stream relay is another powerful feature. Using the push directive, you can forward incoming streams to external servers, such as YouTube Live or a commercial CDN. The pull directive does the opposite, allowing Nginx to fetch streams from remote sources. This is useful for building distributed streaming architectures or aggregating content from multiple sources into a single distribution point.
On-the-fly transcoding is possible through FFmpeg integration. By configuring an exec directive, you can pass incoming streams to FFmpeg for processing. This allows you to transcode video to different bitrates, resize resolutions, or change codecs to support a wider range of devices. However, transcoding is highly CPU-intensive and should be used judiciously to avoid overwhelming the server and degrading stream quality.
Recording streams to disk is straightforward with the record directive. You can save streams as FLV or MP4 files, which is useful for archiving live events or creating VOD content. Managing storage is critical, as continuous recording can quickly consume disk space. Implementing a rotation strategy or offloading recordings to cloud storage is recommended for long-term deployments.
HTTP callbacks provide a way to integrate streaming events with your application backend. The module can send HTTP requests to your backend when a publisher starts or stops a stream, or when a recording is completed. This enables real-time monitoring, automated workflows, and dynamic stream management.
Security is a major consideration for any public streaming deployment. The module supports allow and deny directives to restrict access based on IP addresses. For more robust security, you can implement token-based authentication using HTTP callbacks to validate publishers before they begin streaming, ensuring only authorized sources can broadcast to your server.

Performance tuning and monitoring

Optimizing performance requires careful attention to buffering and memory settings. The chunk size directive affects latency and throughput. Smaller chunks reduce latency but increase CPU overhead, while larger chunks improve throughput at the cost of higher latency. Finding the right balance depends on your specific use case, network conditions, and viewer expectations.
The module includes a built-in statistics module that provides real-time metrics in XML format. By configuring an HTTP endpoint with XSLT transformation, you can view active streams, client counts, and bandwidth usage in a web browser. This visibility is crucial for identifying issues before they impact viewers and for planning capacity upgrades.
Common bottlenecks include network bandwidth and worker process limits. If your server's network connection is saturated, viewers will experience buffering and dropped frames. Increasing server capacity or distributing streams via a CDN can mitigate this. Worker process limits can be adjusted in the main Nginx configuration to handle more concurrent connections, but this must be balanced against available CPU and memory resources.
When deciding between the module and a dedicated media server, consider your scale and feature requirements. For small to medium deployments, the module offers excellent performance at no cost. For large-scale deployments requiring advanced features like adaptive bitrate streaming or DRM, a dedicated media server may be more appropriate.

Troubleshooting common issues

Publisher connection failures are often caused by firewall restrictions. Ensure that port 1935, the standard RTMP port, is open on your server and accessible from the publisher's network. If the port is blocked, publishers will not be able to send streams, and the connection will time out.
Viewer latency spikes can result from improper chunk size settings or issues with rtmp auto push. If auto-push is not enabled, viewers connected to different workers may experience inconsistent stream availability or delays. Adjusting chunk size can also help smooth out playback by optimizing how data is packetized and sent.
Recording errors typically stem from disk permissions or incorrect paths. The Nginx worker process must have write access to the directory where recordings are saved. Verifying permissions and paths resolves most recording issues. Additionally, ensure there is sufficient disk space available before starting a long broadcast.
Windows-specific limitations are a frequent source of frustration for developers. The module's performance on Windows is significantly lower than on Linux due to how Nginx handles network I/O operations on different operating systems. For production streaming, a Linux server is strongly recommended to ensure stability and performance.

Choosing the right streaming stack

Selecting the right streaming stack depends on your specific requirements, budget, and scale. The nginx rtmp module is ideal for cost-effective, simple deployments where you need basic live streaming and HLS conversion. It integrates well with existing Nginx infrastructure and requires minimal resources, making it a popular choice for developers and small teams.
A full-featured media server like Wowza or Red5 is preferable when you need advanced features such as adaptive bitrate streaming, DRM, or extensive protocol support out of the box. These servers offer commercial support and are designed for enterprise-scale deployments where reliability and feature completeness are critical.
Commercial CDNs are the best choice when global distribution and massive scale are your primary concerns. They handle the complexity of edge delivery, allowing you to focus on content creation rather than infrastructure management. For interactive, sub-second latency experiences, consider exploring VideoSDK's real-time communication platform.

Definitions Glossary

RTMP: Real-Time Messaging Protocol, a TCP-based protocol designed for high-performance transmission of audio and video streams.
HLS: HTTP Live Streaming, an adaptive streaming protocol that breaks streams into small HTTP-based segments for web and mobile playback.
Nginx Worker Process: A single-threaded process in Nginx that handles client connections; multiple workers run concurrently to maximize resource use.
Stream Relay: The process of forwarding a media stream from one server to another using push or pull directives.
Chunk Size: The size of data chunks sent over RTMP, directly influencing the balance between streaming latency and throughput.

Key Takeaways

  • The nginx rtmp module is a lightweight, cost-effective solution for adding live streaming capabilities to an existing Nginx server.
  • Proper configuration of the rtmp auto push directive is essential for scaling streams across multiple Nginx worker processes.
  • The module supports advanced features like stream relay, on-the-fly transcoding via FFmpeg, and HTTP callbacks for event integration.
  • Security should be enforced using IP restrictions and token-based authentication via HTTP callbacks.
  • For large-scale deployments requiring adaptive bitrate or DRM, a dedicated media server may be a better choice than the module.

Conclusion

The nginx rtmp module provides a powerful, lightweight way to implement live streaming without investing in expensive media server software. By understanding its architecture, configuration, and performance characteristics, you can build a robust streaming infrastructure tailored to your needs. For more details, explore the official nginx rtmp module documentation and join the VideoSDK Discord community to discuss real-time communication strategies. What are you building with the nginx rtmp module? Drop a comment below and share your streaming deployment experiences.

Free $20 Balance for AI Voice Agents & Video Calls

FAQ