Skip to main content

Mocha behind a reverse proxy

Sometimes, individuals & organisations like to put applications behind a reverse proxy, the main reason for this is to allow applications to be accessed on specific domains. An example of this would be a helpdesk company running Mocha having it accessible to employees through their domain name.
Behind the scenes you’ll most likely use nginx, trafik or haproxy to achieve this goal. In this example we will be using nginx on debian. In the end, you should have a setup like so:
I know two different subdomains may seem a bit overkill, but this is the only way to achieve this goal.

Setting up the box & nginx

In this example, I’m going to be using a node provided by linode pre-installed with docker, but any vm should be able to achieve this provided they have a static i.p address. I wont be going into any detail regarding secuirty or best practices for setting up a linux machine, or we’ll be here all day :) This is going to be a straight forward, down to business guide. With docker pre installed, we need to get the second piece of the puzzle installed, which is nginx. Since this our first interaction which the machine, we’ll need to update the package manager to get the latest listings:
After this we can installed nginx
Before testing Nginx, the firewall software needs to be adjusted to allow access to the service. List the application configurations that ufw knows how to work with by typing:
This should show the list of application profiles
As you can see, there are three profiles available for Nginx:
  • Nginx Full: This profile opens both port 80 (normal, unencrypted web traffic) and port 443 (TLS/SSL encrypted traffic)
  • Nginx HTTP: This profile opens only port 80 (normal, unencrypted web traffic)
  • Nginx HTTPS: This profile opens only port 443 (TLS/SSL encrypted traffic)
It is recommended that you enable the most restrictive profile that will still allow the traffic you’ve configured. Since we haven’t configured SSL for our server yet in this guide, we will only need to allow traffic for HTTP on port 80. Lets enable that now
Use https if you have SSL cert enabled.

Checking nginx

To make sure nginx is running okay when can run the command:
Which should display
Another way you can test this is working is by going to:
And if everything is good you should see the welcome to nginx sign.

Setting up Mocha

Now nginx is out the way and configured, we can now simply download the docker compose file from github:
After this we should be able to run:
and we’ll be presented with an output like so:
You could also reverse proxy the backend as well if you really wanted too, but for this example we’ll just be doing the frontend. Now for your base url: this is where your subdomain url is going to, if youre using nginx https with SSL, then make sure you change it accordingly. After you enter the correct base url hit ctrl + x and then y to save. When this is complete run the command:
This will pull the Mocha image & postgres and start the process of both containers. Once up, we have one finally more nginx config to take care of.

Nginx config

Now that nginx is set up & both are containers are working, we can now implement the config file which is going to route our proxy to our subdomains. I like to save this in the conf.d folder of nginx, it works for me and i never run into issues. Start off by running:
This will bring an editor up, in which you will paste

Client Proxy Config

Replace the server name with your url of choice, including subdomain and procced to save the file as

API Proxy Config

Replace the server name with your url of choice, including subdomain and procced to save the file as, make sure this matches what you put in the docker-compose.yml file

Restarting nginx

The final touch to the tale is you need to restart nginx for this to take full effect. This can be achieved by running:
You should now be able to see Mocha running on your chosen subdomain. I hope you found this guide usual :)

Enabling SSL

To enable SSL, you will need to open some additional ports. You can do that by using the below command.
We will be using Certbot to do the heavy listing. We are going to assume that this is a fresh new install of an OS of your choice.

Installing Certbot

The below commands will install Python3, the Python VENV module, and the Augeas for the Apache plugin. For APT based systems (Debian, Ubuntu)
For RPM based systems (Fedora, CentOS)
We now need to set up a Python virtual environment using these commands
Once that is done, we can install Certbot using this command
Next, we need to run this command to ensure that the certbot command can be run
Congrats! Certbot is now installed! Now we can automatically enable SSL by using the below command, and following all of the prompts!
Certificates issued by Certbot do expire, so you can opt to either manually renew, or you can use cron.
You should now be able to visit your site using https!

Issues i discovered

An issue i discovered was that the api was not able to serve requests if it wasnt HTTPS, just bear that in mind.