News

4 Jan 2018

[Blog] Using MistServer through a reverse proxy

Sometimes, it can be convenient to direct requests to MistServer through a reverse proxy, for example to limit the amount of ports that will be used or to use a prettier url.
In this blog, I'll take you through the steps to configure your web server and MistServer to work in with this. For this example I'll consider the use case of having an existing website that is running on port 80, with MistServer on the same machine, and that we want MistServer's HTTP endpoint (by default that's port 8080) to be reached through http://example.com/mistserver/.

Using a reverse proxy for MistServer does have a cost to efficiency, however.
Using this method means that instead of once, output data needs to be moved four times. Furthermore, MistServer tries to reuse connections, but it can't if they're behind a proxy server.

Configure your web server

The first step is to configure your web server to reverse proxy to MistServer's HTTP port.

For Apache:
Enable the mod_proxy and mod_proxy_http modules and add the following to your configuration file:

<Location "/mistserver/">
  ProxyPass "http://localhost:8080/"
</Location>

For Nginx:
Add the following to your configuration's server block, or nested into your website's location block:

location /mistserver/ {
  proxy_pass http://localhost:8080/;
  proxy_set_header X-Real-IP $remote_addr;
  proxy_buffering off;
}

For Lighttpd:
Add mod_proxy to your list of server modules, or add the following to your configuration file:

server.modules = ("mod_proxy")

Also, to disable response buffering, add:

server.stream-response-body = 2

and for the reverse proxy itself:

$HTTP["url"] =~ "(^/mistserver/)" {
  proxy.server  = ( "" => ( "" => ( "host" => "127.0.0.1", "port" => 8080 )))
  proxy.header = ( "map-urlpath" => ( "/mistserver/" => "/" ))
}

You should now be able to watch your stream directly at http://example.com/mistserver/STREAMNAME.mp4 (MP4) or http://example.com/mistserver/hls/STREAMNAME/index.m3u8 (HLS).

Configure MistServer

MistServer also outputs information about its streams, including the various urls under which the stream can be accessed. However, if reverse proxying is being used, these urls cannot be accessed externally. That can be fixed by configuring the "Public address"-setting of the HTTP protocol through the MistServer Management Interface.

You should now also be able to watch your stream at http://example.com/mistserver/STREAMNAME.html.

Edit your web pages

Lastly, the urls with which streams are embedded on your webpage will need to be updated. If you are using MistServer's embed code, update any urls.

<div class="mistvideo" id="nVyzrqZSm7PJ">
  <noscript>
    <a href="http://example.com/mistserver/STREAMNAME.html" target="_blank">
      Click here to play this video
    </a>
  </noscript>
  <script>
    var a = function(){
      mistPlay("STREAMNAME",{
        target: document.getElementById("nVyzrqZSm7PJ")
      });
    };
    if (!window.mistplayers) {
      var p = document.createElement("script");
      p.src = "http://example.com/mistserver/player.js"
      document.head.appendChild(p);
      p.onload = a;
    }
    else { a(); }
  </script>
</div>

That's all folks! Your website should now access MistServer through the reversed proxy.

On our next blog, Erik will talk about the upcoming lock-free internal metadata structure Jaron mentioned last time.