Skip to content
Back to blog

Magichosting

Setting up Nginx as a reverse proxy with Let's Encrypt

E
Elias Bieber
Estimated reading time: 3 min

Web app on port 3000, visitors on HTTPS: how to set up Nginx as a reverse proxy with a free Let's Encrypt certificate on Debian or Ubuntu – including WebSockets.

NginxReverse proxySSLLet's EncryptLinux

What this is about

A web application – such as a Node.js dashboard, a Docker app or a bot web interface – usually runs on an internal port like 3000. As a reverse proxy, Nginx accepts requests on ports 80 and 443, forwards them to the application and handles HTTPS with a free Let's Encrypt certificate. The application itself stays unreachable from outside.

You need a Linux server with root access, for example a KVM VPS running Debian or Ubuntu, and a domain whose A or AAAA record points to the server's IP. Our guide to DNS records explains how.

1. Install Nginx and Certbot

bash
sudo apt update
sudo apt install -y nginx certbot python3-certbot-nginx

If the UFW firewall is active, allow HTTP and HTTPS. The "Nginx Full" profile is installed along with Nginx:

bash
sudo ufw allow 'Nginx Full'

2. Let the application listen locally only

Start your application so it listens on 127.0.0.1 instead of all addresses – with Docker, for example, using -p 127.0.0.1:3000:3000. That way it's only reachable through Nginx and not directly via the public IP.

3. Create the server block

Create the file /etc/nginx/sites-available/app.example.de and replace domain and port with your values:

nginx
map $http_upgrade $connection_upgrade {
    default upgrade;
    ''      close;
}

server {
    listen 80;
    listen [::]:80;
    server_name app.example.de;

    location / {
        proxy_pass http://127.0.0.1:3000;
        proxy_http_version 1.1;
        proxy_set_header Host $host;
        proxy_set_header X-Real-IP $remote_addr;
        proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
        proxy_set_header X-Forwarded-Proto $scheme;
        proxy_set_header Upgrade $http_upgrade;
        proxy_set_header Connection $connection_upgrade;
    }
}

The map directive makes WebSockets work without affecting normal requests. Then enable the site and test the configuration:

bash
sudo ln -s /etc/nginx/sites-available/app.example.de /etc/nginx/sites-enabled/
sudo nginx -t && sudo systemctl reload nginx

4. HTTPS with Let's Encrypt

bash
sudo certbot --nginx -d app.example.de

Certbot asks for an email address for expiry warnings, obtains the certificate and adds HTTPS plus a redirect from HTTP to the server block. Renewal runs automatically via a systemd timer. Test it like this:

bash
sudo certbot renew --dry-run

Common errors

  • 502 Bad Gateway: The application isn't running or listens on a different port. Check with ss -ltnp which port is actually open.
  • Certbot fails: The DNS record doesn't point to the server yet, or port 80 is blocked. Let's Encrypt validates the domain over HTTP.
  • WebSocket disconnects immediately: The Upgrade and Connection headers are missing from the server block.
  • Wrong visitor IP in the app: The application must read the X-Forwarded-For header or trust the proxy.

More basics for running a server: set up a UFW firewall and the Docker Compose production checklist.

FAQ

Is Let's Encrypt really free?

Yes. Certificates are valid for 90 days and Certbot renews them automatically.

Do I need my own domain?

Yes. Let's Encrypt issues certificates for domains, not for bare IP addresses. You can register a domain under domains.

Does this work with Docker?

Yes. Publish the container port only on 127.0.0.1 and run Nginx on the host in front of it – or use a proxy container. The principle stays the same.

How does this compare to Caddy or Traefik?

Caddy and Traefik obtain certificates themselves and work well for container setups. Nginx is more widespread, very well documented and flexible – a solid choice for most VPS.

Was this post helpful?

MagicHosting – Hosting with real value

Hosting pays off with MagicHosting: fair pricing, current deals and operation in SkyLink RZ. Servers, domains and personal support – no hidden costs.

  • Current limited offers from our server exchange
  • Affordable domains with transparent pricing and great offers
  • Powerful DDoS protection for maximum availability
  • Personal support when you have questions or issues