Search The Hostwinds Guides Knowledge Base

Live Streaming from a VPS with Nginx + RTMP

Share This Article

Whether you simply want a server to act as a relay for you to be able to stream to multiple services at once, re-encode your video stream into different formats, resolutions, or bitrates, or to just stream from pre-recorded videos, a good method to do so is with a server running Nginx compiled with the RTMP module.

What is RTMP?

RTMP stands for Real-Time Messaging Protocol and is a common, standardized way many services (such as YouTube, Twitch, etc.) use, or at least offer, to broadcast video online.

Installation

Install Dependencies

  1. Update your system
  2. Install CentOS development tools
  3. Install EPEL repository
  4. Install dependencies
  5. Add ffmpeg GPG key, and install ffmpeg

Download Software

  1. Create or go to a working directory
  2. Get the latest version of Nginx from here

  3. Clone the Nginx RTMP Module from here
  4. Remove unneeded tar files

Compile Software

  1. Move into your downloaded Nginx folder
  2. Run the configuration script to add SSL and the RTMP module
    Update the --add-module file path as necessary to point to where you cloned the nginx-rtmp-module
  3. Compile Nginx
  4. Install Nginx
  5. Start and enable Nginx

Configuration

To configure the RTMP service, you need to add an RTMP section to your Nginx configuration file, which should be /etc/nginx/nginx.conf by default.

Add an RTMP section to the conf file as follows:

In that configuration, we are simply telling the server to listen for any requests on port 1935 (the default port for RTMP), and specified an application called “live” to accept our streams.

This configuration is enough to have the server accept a stream, but there are further configurations you can set for additional functionality.

Then when you stream, you will set your destination as rtmp://server_address/application, where server_address is the IP or domain address of your server, and application is the name of the application you configured in the Nginx configuration (“live” in this example).

Example: rtmp://127.0.0.1/live

Multiple Applications

Within the server { } block, you can configure as many applications as you want. The only restriction in place is that each application must have a unique name.

Permissions

You can specify permissions for what IPs are able to stream to the server, as well as what IPs are allowed to play the stream from this server.

To add a permissions rule, add a line either within the server { } or a application { } block in the following format:

Using “allow” or “deny” specifies whether the rule is to allow or deny the connection from the specified source.

Using “publish” specifies that the source location is able to stream to this server. Using “play” specifies that the source location can playback a stream from this server.

Using “all” as the location acts as a catch-all for any address that connects to this server. Otherwise, you can set a specific IP address.

Putting the permission rule in the server { } block makes the rule global across all applications. Putting the permission rule in a application { } block applies the rule to only that application.

Examples:

  • Allow 127.0.0.1 to stream to this server, but deny any other sources:

  • Allow playback to all sources except 127.0.0.1:

Re-Encoding

You can use an exec statement from within an application { } block to execute a command. Using this method, you can pass the received RTMP stream to a video processor, such as ffmpeg, for encoding.

Example: Encode the received stream to a mp4 format and save it as a file

Pushing to Other Services

You can push the stream from an application to another receiver simply by using the push statement.

For example, you can add the following statement to any application { } block to push the received stream to Twitch, using one of their ingest addresses (in this case the Seattle address). Just replace {stream_key} with your Twitch stream key.

You can do the same for any other platform that offers RTMP ingest addresses you can stream to, and can even list multiple push statements to essentially allow streaming to multiple platforms at once.

HLS

HLS, or HTTP Live Streaming, is a fairly popular format to be able to stream video over the HTTP and HTTPS protocols, making it significantly easier to embed the stream into a web page.

To enable HLS, simply add the following statements to any of your application { } blocks.

Then simply make sure the hls_path directory exists by running the command:

Then you also need to add a http { } block to your Nginx configuration. This block should be a top level block, on the same level as the existing rtmp { } block. This new block should be as follows:

Afterward saving those changes to your Nginx configuration file, simply restart Nginx by running the command:

HLS Playback

With HLS enabled, you can now embed the stream into your web pages using any video player that supports HLS playback, such as Video.JS or PlayerJS.

You will just provide your player with the source address in the following format:

{server_address} will be your IP or domain of your server, {app_name} will be the name of the application { } block you are streaming to, and {secret_key} will be the secret key you set in your streaming software (such as OBS).

Example:

SSL

You can serve HLS over HTTPS as well. To do so, you simply need to provide an SSL certificate and private key within the server { } block with the following statements:

The certificate must be for the domain that you have specified in the server_name statement.

You will then also need to edit/add the listen statements to listen on port 443 (the https port), and optionally force usage of SSL. The listen statements for that should be as follows:

Other Configurations

There are plenty of other configuration statements you can add to expand the functionality of the RTMP module of the Nginx configuration.

The full documentation for what you can add and configure can be found here.

Streaming

After you’ve configured your server, you can stream from any video streaming software that supports streaming to an RTMP address. You may consider using OBS as it is very powerful, has plugin support, popular with a large community, and open source.

To stream to your server, all you have to do is set the stream destination as your server’s address, with the application name, and set a private key. The format for the server address should be as follows:

{server_address} is your IP or a domain you have set to point to this server. {app_name} is the name of the application { } block that you set in the Nginx configuration. Following our example configuration from above, that application name would be “live”.

OBS stream settings

Providing a secret key that is unique will ensure that the stream itself is unique, even if other sources are streaming to the same application.

Following the example above, the stream will then be live at rtmp://example.com/live/secret_key, which you can pick up using any video playing that can open an RTMP stream, such as VLC.

Related Articles