Sphere
Log in Join

Nginx Reverse Proxy with Let's Encrypt: A Config You Can Copy

I set up Nginx for a small client site recently and hit the same two redirect mistakes I hit the last time. This is the config I ended up with, plus the certbot commands. Copy it, change the domain, and check the two things at the bottom.

Get a certificate first

Point your DNS A record at the server and make sure port 80 is open. Then run:

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

Certbot will ask whether to redirect HTTP to HTTPS. Say yes, then open the file it wrote in /etc/nginx/sites-available/ to see what it actually did. I have learned to read that file instead of trusting the prompt, because certbot adds its own location block and the ordering can surprise you.

Test renewal before you need it:

sudo certbot renew --dry-run

If this fails, your certificate will not renew, and you will find out three months later at the worst possible moment.

A server block that works

This is close to what I deployed. The app was a Node service on port 3000, so Nginx is just a reverse proxy:

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

server {
listen 443 ssl http2;
server_name example.com www.example.com;

ssl_certificate /etc/letsencrypt/live/example.com/fullchain.pem;
ssl_certificate_key /etc/letsencrypt/live/example.com/privkey.pem;

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;
}
}

Then:

sudo nginx -t
sudo systemctl reload nginx

Always run nginx -t before reload. A config error on reload leaves the old config running, which is confusing when you are sure you saved the file.

Mistake one: redirect loop

If both server blocks have a return 301 to HTTPS and certbot also added a rewrite rule, you get an infinite redirect. The browser says "too many redirects" and Nginx logs nothing useful. Check the file for two redirect mechanisms and keep one. In the config above, the HTTP block does the redirect and the HTTPS block does not.

Mistake two: the app thinks it is on HTTP

After the proxy was in place, our app generated http:// links in emails. It did not know the original request was HTTPS. The line that fixes it is:

proxy_set_header X-Forwarded-Proto $scheme;

Your framework also needs to trust that header. In Express that means app.set('trust proxy', 1). Without it the header arrives but the application ignores it. This is why the site looked fine in the browser while every email link was wrong.

Two small things worth adding

Set client max body size if you accept uploads; the default is 1 MB and the error looks like a failed upload in the browser. Set a longer keepalive timeout only if you need it; I left it alone.

If you serve static files directly, add caching headers for assets with hashed names, but do not cache index.html. I learned that one from a deploy where users kept seeing the old bundle for an hour.

Check the result with curl -I http://example.com and curl -I https://example.com, then open the site in a browser with a clean cache. That is enough to catch the redirect problems before anyone else does.