How to Install Discourse Using Nginx for SSL: Step-by-Step Tutorial

Ewan MakEwan Mak
2 min read

Learn how to install Discourse with Nginx managing the SSL port. Follow this comprehensive step-by-step guide to seamlessly set up your Discourse forum with Nginx handling SSL.

Prerequisites

  • Ubuntu server

  • Domain name configured

  • Root/sudo access

  • Minimum 2GB RAM recommended

Step 1: Install Required Packages

sudo apt update -y && apt upgrade -y
sudo apt install wget curl zip git docker.io nginx -y
sudo reboot

Step 2: Install Discourse

sudo mkdir /var/discourse
sudo git clone https://github.com/discourse/discourse_docker.git /var/discourse
cd /var/discourse

Step 3: Configure Discourse

Edit the app.yml configuration file:

sudo nano /var/discourse/containers/app.yml

Make these important changes:

# Comment out these lines
#- "templates/web.ssl.template.yml"
#- "templates/web.letsencrypt.ssl.template.yml"

# Add this line
- "templates/web.socketed.template.yml"

# Comment out expose section
#expose:
#  - "80:80"
#  - "443:443"

# Add in env section
env:
  DISCOURSE_FORCE_HTTPS: true

Step 4: Build Discourse

cd /var/discourse
./launcher rebuild app

Step 5: Configure Nginx

Create a new Nginx configuration:

sudo nano /etc/nginx/sites-available/discourse

Add this configuration (replace example.com with your domain):

server {
    listen 80;
    listen [::]:80;
    server_name example.com;
    return 301 https://$host$request_uri;
}

server {
    listen 443 ssl http2;
    listen [::]:443 ssl http2;
    server_name example.com;

    ssl_certificate /etc/ssl/certs/your_ssl_cert.bundle;
    ssl_certificate_key /etc/ssl/private/your_ssl_cert.key;
    ssl_protocols TLSv1 TLSv1.1 TLSv1.2;
    ssl_prefer_server_ciphers on;

    location / {
        proxy_pass http://unix:/var/discourse/shared/standalone/nginx.http.sock;
        proxy_set_header Host $http_host;
        proxy_http_version 1.1;
        proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
        proxy_set_header X-Forwarded-Proto $scheme;
        proxy_set_header X-Real-IP $remote_addr;
    }
}

Step 6: Enable and Start Nginx

sudo ln -s /etc/nginx/sites-available/discourse /etc/nginx/sites-enabled/
sudo nginx -t
sudo systemctl enable nginx
sudo systemctl start nginx

Step 7: SSL Certificate Setup

If using Let's Encrypt:

sudo apt install certbot python3-certbot-nginx
sudo certbot --nginx -d example.com

Important Notes

  • Make sure your domain's DNS is properly configured before starting

  • The Nginx configuration assumes you're using SSL certificates

  • Discourse will be accessible directly through HTTPS on your domain

  • All HTTP traffic will automatically redirect to HTTPS

  • Check the logs if you encounter any issues: /var/discourse/shared/standalone/log/rails/production.log

After completing these steps, Discourse should be accessible via HTTPS through your domain, with Nginx handling all SSL termination[1][4].

Citations: [1] https://maker-tutorials.com/en/install-discourse-with-docker-in-subfolder-with-ssl-and-serve-other-content-under-the-same-domain/ [2] https://blog.khophi.co/install-run-discourse-behind-nginx-right-way-first-time/ [3] https://www.digitalocean.com/community/tutorials/how-to-install-discourse-behind-nginx-on-ubuntu-14-04 [4] https://meta.discourse.org/t/installing-discourse-behind-reverse-proxy-using-recommended-supported-installation/300191 [5] https://www.howtoforge.com/tutorial/how-to-install-and-configure-discourse-forum-with-nginx-on-ubuntu-1604/

0
Subscribe to my newsletter

Read articles from Ewan Mak directly inside your inbox. Subscribe to the newsletter, and don't miss out.

Written by

Ewan Mak
Ewan Mak

Crafting seamless user experiences with a passion for headless CMS, Vercel deployments, and Cloudflare optimization. I'm a Full Stack Developer with expertise in building modern web applications that are blazing fast, secure, and scalable. Let's connect and discuss how I can help you elevate your next project!