Skip to main content

How to add Nginx as a reverse proxy

The following commands are tailored for Ubuntu/Debian systems. If you’re using a different Linux distribution, you may need to adjust package management commands accordingly (e.g., yum for CentOS, brew for macOS).
1

Install Nginx

sudo apt update && sudo apt install nginx -y
sudo systemctl start nginx && sudo systemctl enable nginx
2

Install Certbot for SSL

sudo apt update && sudo apt install python3-certbot-nginx -y
3

Create the Webroot Directory

sudo mkdir -p /var/www/certbot/.well-known/acme-challenge && sudo chown -R www-data:www-data /var/www/certbot
4

Create a temporary Nginx config for creating SSL certificates

Make sure to change subdomain.domain.tld to your actual subdomain and ensure that it is pointed to your server’s IP address.
Edit the file /etc/nginx/sites-available/subdomain.domain.tld:
server {
    listen 80;
    listen [::]:80;  # IPv6 support
    server_name <subdomain.domain.tld>;  # CHANGE HERE

    location /.well-known/acme-challenge/ {
        root /var/www/certbot;
    }

    location / {
        return 301 https://$host$request_uri;
    }

    # Hide NGINX version for security reasons
    server_tokens off;
}
Then create the symlink:
sudo ln -s /etc/nginx/sites-available/<subdomain.domain.tld> /etc/nginx/sites-enabled/
5

Obtain SSL Certificates Using Webroot method

sudo certbot certonly --webroot -w /var/www/certbot -d <subdomain.domain.tld> --email your-email@example.com --agree-tos --no-eff-email
6

Edit the Nginx config

Edit the file /etc/nginx/sites-available/subdomain.domain.tld.
7

Generate DH Parameters

(Needed if you are using the “robust” nginx config option)
sudo openssl dhparam -out /etc/ssl/certs/dhparam.pem 2048
8

Test your Nginx configuration

sudo nginx -t
9

Automatic reload Nginx when SSL Certificates are renewed

echo -e '#!/bin/bash\nginx -t && systemctl reload nginx' | sudo tee /etc/letsencrypt/renewal-hooks/post/nginx-reload.sh && sudo chmod a+x /etc/letsencrypt/renewal-hooks/post/nginx-reload.sh
10

Reload Nginx

sudo systemctl reload nginx
11

Done!

Your Nginx reverse proxy is now running, securely serving your Postiz instance at your subdomain.domain.tld. 🎉