Serve a site with Nginx and HTTPS

Nginx plus a free Let's Encrypt certificate from Certbot gives you a fast HTTPS site in about ten minutes on any CheapVPS plan. This guide writes a proper server block per domain, adds the certificate, sets up renewal and shows the reverse proxy setup for Node.js, Python or Go apps.

What do you need before you start?

A domain whose A and AAAA records already point at the server (see pointing a domain), ports 80 and 443 open in the firewall, and a sudo user. Port 80 must stay open even for an HTTPS-only site, because Certbot proves you control the domain over plain HTTP and the redirect to HTTPS also runs there.

Nginx is light. A static site or a small app proxy runs comfortably on P1 with 1 vCPU, 1 GB RAM, 20 GB NVMe; WordPress or several sites with PHP fit well on S4 in Europe or P4 in any of the 15 cities.

How do you install Nginx?

On Ubuntu and Debian:

apt update
apt install -y nginx
systemctl enable --now nginx
ufw allow "Nginx Full"

On Rocky Linux and AlmaLinux, Nginx comes from the AppStream repository:

dnf install -y nginx
systemctl enable --now nginx
firewall-cmd --permanent --add-service=http --add-service=https
firewall-cmd --reload

Open http://203.0.113.10 in a browser and you should see the Nginx welcome page. From the server itself, curl -I http://localhost should answer HTTP/1.1 200 OK with a Server: nginx header.

How do you write a server block for your domain?

Give each site its own file and its own web root. On Ubuntu and Debian the files go in /etc/nginx/sites-available/ and are switched on with a link in sites-enabled; on Rocky and Alma, put the same block in /etc/nginx/conf.d/example.com.conf. Create the folder and the file:

mkdir -p /var/www/example.com/public
echo "hello from example.com" > /var/www/example.com/public/index.html

Then /etc/nginx/sites-available/example.com:

server {
    listen 80;
    listen [::]:80;
    server_name example.com www.example.com;
    root /var/www/example.com/public;
    index index.html index.php;

    access_log /var/log/nginx/example.com.access.log;
    error_log  /var/log/nginx/example.com.error.log;

    location / {
        try_files $uri $uri/ =404;
    }
}

Enable it, remove the default site, test and reload:

ln -s /etc/nginx/sites-available/example.com /etc/nginx/sites-enabled/
rm -f /etc/nginx/sites-enabled/default
nginx -t && systemctl reload nginx

nginx -t checks every file and names the line of any error; only reload when it reports "syntax is ok". On Rocky and Alma, if files under /var/www return 403, restore their SELinux labels with restorecon -Rv /var/www.

How do you get a free certificate with Certbot?

Install Certbot with its Nginx plugin and let it edit the server block for you. On Ubuntu and Debian:

apt install -y certbot python3-certbot-nginx

On Rocky and Alma, Certbot is in EPEL:

dnf install -y epel-release
dnf install -y certbot python3-certbot-nginx

Then request one certificate for both names:

certbot --nginx -d example.com -d www.example.com --redirect -m you@example.com --agree-tos

Certbot reads server_name, places a temporary file to prove control of the domain, saves the certificate under /etc/letsencrypt/live/example.com/, adds listen 443 ssl lines to your block and, because of --redirect, adds a block that sends HTTP visitors to HTTPS. The email address receives expiry warnings only if renewal ever fails.

Check it from outside with curl -I https://example.com; the answer should be HTTP/2 200 once HTTP/2 is on, which the next section covers.

How do you make sure the certificate renews?

Certificates last 90 days and Certbot renews them automatically from a systemd timer or a cron job installed with the package. Confirm the timer exists and test a renewal without changing anything:

systemctl list-timers | grep certbot
certbot renew --dry-run

A dry run that ends with "all simulated renewals succeeded" means renewal will work. The usual reasons it later fails are a firewall change that closed port 80, a DNS record moved elsewhere, or a server block edited so the domain no longer answers on HTTP. certbot certificates lists every certificate on the server with its expiry date.

Which HTTPS settings are worth adding?

A few lines inside the HTTPS server block improve speed and safety. Turn on HTTP/2, send an HSTS header once you are sure the site will stay on HTTPS, and enable compression for text:

listen 443 ssl;
listen [::]:443 ssl;
http2 on;
add_header Strict-Transport-Security "max-age=31536000" always;
gzip on;
gzip_types text/css application/javascript application/json image/svg+xml;

The http2 on; directive needs Nginx 1.25.1 or newer, which Ubuntu 24.04 and Debian 13 ship; on older releases write listen 443 ssl http2; instead. Certbot already includes sensible protocol and cipher settings from /etc/letsencrypt/options-ssl-nginx.conf, so leave those alone. For static files, add caching:

location ~* \.(css|js|png|jpg|webp|svg|woff2)$ {
    expires 30d;
    access_log off;
}

How do you run an app behind Nginx as a reverse proxy?

Let the app listen on localhost and have Nginx handle TLS and the public ports. For a Node.js, Python or Go app on port 3000, replace the location / block with:

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

The two last headers let WebSocket connections through. The forwarded headers tell the app the real visitor IP and that the request came over HTTPS, which many frameworks need to build correct links. On Rocky and Alma, SELinux blocks Nginx from opening connections by default; allow it once with setsebool -P httpd_can_network_connect 1.

Check that the app port itself is not open to the world: ss -tlnp | grep 3000 should show 127.0.0.1:3000, not 0.0.0.0:3000.

How do you troubleshoot a site that does not load?

Work from the inside out. curl -I http://127.0.0.1 -H "Host: example.com" on the server tests Nginx without DNS or the firewall. If that works, test from your laptop with curl -I --resolve example.com:80:203.0.113.10 http://example.com, which skips DNS but crosses the firewall. Then plain curl -I https://example.com adds DNS and TLS.

The site log usually names the cause: tail -n 50 /var/log/nginx/example.com.error.log. A 502 Bad Gateway means the app behind the proxy is not running or listens on another port; check it with systemctl status for its service. A 403 usually means file permissions or, on Rocky and Alma, SELinux labels. A certificate warning in the browser often means the name you typed is not in the certificate; certbot certificates shows the names covered.

Questions about this guide

Will one Nginx install serve many domains?

Yes. Give each domain its own server block and web root, then run Certbot with that domain's names. Nginx picks the right block by the host name of each request.

Is the certificate really free?

Yes. Let's Encrypt certificates cost nothing and Certbot renews them on its own. The server and its traffic are the only costs.

Can I get a wildcard certificate?

Yes, with a DNS challenge instead of HTTP: certbot certonly --manual --preferred-challenges dns -d "*.example.com". Automatic renewal then needs a Certbot DNS plugin for your DNS provider.

Should I use Apache instead?

Either works. Nginx uses less memory per connection and is the usual choice for reverse proxies, while Apache with .htaccess suits some older PHP apps. Certbot has a plugin for both.

Do I need a WordPress setup from scratch?

Not necessarily. The WordPress image comes with Nginx, PHP and MariaDB already set up; you still add your domain and run Certbot as above.

Why does Certbot say the challenge failed?

Most often the domain does not yet point at this server, or port 80 is closed. Check with dig +short example.com and an outside curl -I http://example.com, then retry.