Decentralized Game Streaming with Nginx and RTMP

Want to stream to your friends, but don't want to give your video to some centralized service? Here's how.

Thinking about streaming, but uncomfortable with the likes of Twitch and Mixer? Fortunately, they're not the only game in town. Here's how you can set up your own streaming server, with nothing but open source tools.

Before You Begin

Caveats

Unfortunately, this method does have some disadvantages. It's relatively easy to set up, but it has no web interface; your viewers will need VLC (or some other video player that can watch RTMP streams) to watch your stream. It also has no built-in commenting facilities. You can use something else like Mumble, a self-hosted IRC server, or your favorite chat program for that.

Also, if you're running the server on your own network, keep in mind this will give your audience your IP address. Whether this is anything to worry about depends on how much you feel the need to stay anonymous from your audience.

And of course, it won't have any of the inbuilt social features a centralized service brings. You'll need to tell your audience about your stream somehow.

With that out of the way, let's get started!

Installation

You'll need a machine running Unix of some sort; Ubuntu contains all the packages you'll need for this, and it's what this guide will focus on. It can be your gaming machine, a spare computer, or a VPS in the cloud.

Once you have your server machine set up, you'll need to install nginx, as well as nginx's RTMP module. On Ubuntu, you can do it with this command:

sudo apt install nginx libnginx-mod-rtmp

Once you have nginx installed, you should be able to start and stop it with the service command:

sudo service nginx start
sudo service nginx stop

If you're running the server on a machine you use for other things, you may want to stop Nginx from starting whenever you boot the computer.

sudo systemctl disable nginx

Configuration

Now that you have nginx installed, it's time to set up RTMP.

Open /etc/nginx/nginx.conf as root, and add this at the bottom:

rtmp {
    server {
        listen 1935;
        chunk_size 4096;
        application stream {
            live on;
            record off;
        }
    }
}

This tells the RTMP module to listen on port 1935, the default RTMP port, and not to record the video. (If you do want it saved, you can record as you stream with OBS.)

Now save it and restart the server to pick up your changes:

sudo service nginx restart

Streaming

On your gaming machine, you'll want to install OBS Studio. If you're on Windows or Mac or it's not in your Linux package manager, you can get it at https://obsproject.com.

Once you've installed it, open the settings (File > Settings). In the Stream settings, switch the service to Custom. For the server, enter in your server URL, rtmp://<hostname>/stream. (If your server is on the same machine, this will be localhost; if it's somewhere else on your local network, it'll be <hostname>.local.)

Leave the stream key blank, and you don't need any authentication. Now if you apply the settings and hit Start Streaming, it should go live!

Watching The Stream

Note
If you've got your server set up with a domain name, you can skip finding the IP and just use that instead.

First you'll need to find your server's external IP address. Unless you're running it on a VPS, DuckDuckGo can tell you this if you do a web search for "what is my IP".

Give the stream URL to your audience. rtmp://<ip>/stream

For the audience

To watch the stream, you'll need VLC. If you're on Windows or Mac or it's not in your Linux package manager, you can get it from https://videolan.org/vlc.

Then hit Media > Open Network Stream..., and paste in the stream URL. If nothing's gone wrong, after a few seconds of loading you should be able to see the stream!