Configure an NGINX / Let's Encrypt Reverse Proxy for Parse Server

February 6, 2019

Across the previous articles, we set up a Parse server with a minimal configuration. Now, to finalize this configuration and be able to use it on a production server, we will see how to add a reverse proxy with HTTPS using NGINX and Let's Encrypt.

The goal of this article is to add the reverse proxy simply, with as little configuration as possible. To do that, we will use the following projects:

Thanks to this, we will only have to add these two images to our current docker-compose configuration and add the right environment variables.

To begin, we will assume that our Parse server will be accessible from the following domain:

parse.example.com

The configuration used here is the one set up in this article.

Now we can start. First, we will add the two new services, nginx-proxy and docker-letsencrypt-nginx-proxy-companion, to our current services in the docker-compose.yml file:

version: "2"
services:
  mongo:
    image: "bitnami/mongodb:latest"
    ...
  api:
    build: ./parse-server
    image: tutorial/parse-server
    container_name: "tutorial-parse-server"
    ...
  nginx-proxy:
    image: jwilder/nginx-proxy
    container_name: tutorial-nginx-proxy
    restart: always
    ports:
      - "80:80"
      - "443:443"
    volumes:
      - /var/run/docker.sock:/tmp/docker.sock:ro
      - /etc/nginx/vhost.d
      - /usr/share/nginx/html
      - /apps/web/ssl:/etc/nginx/certs:ro
  ssl-companion:
    image: jrcs/letsencrypt-nginx-proxy-companion
    container_name: tutorial-ssl-companion
    restart: always
    volumes:
      - /apps/web/ssl:/etc/nginx/certs:rw
      - /var/run/docker.sock:/var/run/docker.sock:ro
    volumes_from:
      - nginx-proxy
    depends_on:
      - nginx-proxy

Now that we have added the services, we need to add the environment variables that let the nginx-proxy service generate the NGINX configuration for our api service (our Parse server).

VIRTUAL_HOST: 'parse.example.com'
LETSENCRYPT_HOST: 'parse.example.com'
LETSENCRYPT_EMAIL: '[email protected]'

Add these variables to the environment variables of the api service:

api:
  build: ./parse-server
  image: tutorial/parse-server
  container_name: 'tutorial-parse-server'
  restart: always
  ports:
    - '1337:1337'
  environment:
    PARSE_MOUNT: '/parse'
    PORT: 1337
    DATABASE_URI: mongodb://user:MONGODB_PASSWORD@mongo:27017/db_name
    APP_ID: 'APP_ID'
    MASTER_KEY: 'MASTER'
    SERVER_URL: 'http://localhost:1337/parse'
    LOG_LEVEL: 'error'
    ALLOW_INSECURE_HTTP_DASHBOARD: 'true'
    APP_NAME: 'Parse Server Sample'
    DASHBOARD_USER: 'User'
    DASHBOARD_PASSWORD: 'password'
    VIRTUAL_HOST: 'parse.example.com'
    LETSENCRYPT_HOST: 'parse.example.com'
    LETSENCRYPT_EMAIL: '[email protected]'
  depends_on:
    - mongo

Finally, all that remains is to update the value of the SERVER_URL variable with the following value:

SERVER_URL: 'https://parse.example.com/parse'

And that's it: the configuration is complete. The next time you run docker-compose up on your server, the NGINX configuration and HTTPS certificate will be generated automatically.

One final note: you must run this configuration on a server. It will not work locally on your machine.

You also need to add a DNS record with the provider where you bought your domain so that the domain points to your server's IP address.