Since our generic proxy approach does not work out of the box for the niltalk chat server, we will see in this post how to put everything together for this case.

We assume thet we will run the talk server at the address

Installing niltalk

Let’s login to out target machine and be sure, that we have no open port 9000 to the outer world.

If we do not want to build the server by ourselves, we get the binary:$ wget

After unpacking, we create a target directory$ sudo mkdir /opt/niltalk

and copy the binary there$ sudo cp niltalk /opt/niltalk

For the static web templates, we need to clone the niltalk git-respository$ git clone

and to copy the static directory also to the target folder:$ sudo cp niltalk/static /opt/niltalk

To create a configuration, we have to run the server with the command$ sudo niltalk --new-config

This creates a file config.toml that we can amend later on as needed. For now, it makes sense to set the storage to memory:

# Storage kind, one of redis|memory|fs.
storage = "memory"

# Redis cache server.
# Rooms are cached until they expires. Messages are not cached.
#address = "redis:6379" # Eg:
#password = ""
#db = 0
#active_conns = 100
#idle_conns = 20
#timeout = "3s"

#prefix_room = "NIL:ROOM:%s"
#prefix_session = "NIL:SESS:ROOM:%s"

# InMemory store config.
# no options available.

# FileSystem store config.
# [store]
# path = "db.json"

At this point we have the chat server installed. Running it by typing sudo /opt/nilserver/nilserver should result in an open port 9000.

Creating the service

Since we want to start the talk server by default, we create a service file /lib/systemd/system/niltalk.service with the following contents:

Description=niltalk server

ExecStart=/opt/niltalk/niltalk --static-dir=/opt/niltalk/static
ExecReload=/bin/kill -HUP $MAINPID


We enable the service by linking it to the right place:$ sudo ln -s /lib/systemd/system/niltalk.service .

Now we should be able to start and stop the service using the sudo service niltalk start and sudo service niltalk stop commands, respectively.

Apache reverse proxy

The standard approach does not work here. The talk server is using web sockets, which are not proxied/rewritten by default. So we have to amend our configuration to do that.

But first, we can go with the “standard” approach to get the certificate. The Apache configuration reads as

<VirtualHost *:80>
        ServerAdmin webmaster@localhost
        ProxyPreserveHost On
        ProxyPass /
        ProxyPassReverse /

So we get our certificate via sudo certbot --apache -d

But now we have to add some configuration on the newly created

<IfModule mod_ssl.c>
<VirtualHost *:443>
        ServerAdmin webmaster@localhost

        ProxyPreserveHost On
        ProxyRequests Off
        RewriteEngine On

        RewriteCond %{HTTP:UPGRADE} ^WebSocket$           [NC,OR]
        RewriteCond %{HTTP:CONNECTION} ^Upgrade$          [NC]
        RewriteRule .* ws://{REQUEST_URI}  [P,QSA,L]
        RewriteCond %{DOCUMENT_ROOT}/%{REQUEST_FILENAME} !-f
        RewriteRule .*{REQUEST_URI} [P,QSA,L]
        RequestHeader set X-Forwarded-Proto "https"

        <Location />
                Require all granted

SSLCertificateFile /etc/letsencrypt/live/
SSLCertificateKeyFile /etc/letsencrypt/live/
Include /etc/letsencrypt/options-ssl-apache.conf

So basically, we are enabling the websocket use for our proxy.

In order to avoid client reconnection every 5 minutes, we use a (maybe dirty, I’m open for better solutions) workaround and set the connection timeout in /etc/apache2/apache2.conf to 12 hours:

Timeout 43200

In order to get this all working, we have to enable the Apache headers module and to restart the webserver:

$ sudo a2enmod headers
$ sudo service apache2 restart

Finally, we have to set the right IP and URL in our niltalk config file /opt/niltalk/config.toml:

# Address to listen, use "tor" to run an hidden service.
# address = ""
address = ""

# No trailing slashes.
root_url = ""

After restarting the chat server by

$ sudo service niltalk stop
$ sudo service niltalk start

we are done.

That’s it! Now we have a niltalk chatserver behind an Apache reverse https proxy.

Main source for the Apache config part: mattermost.