Home / Configs / Servers / nginx HTTPS server block

nginx HTTPS config: SSL server block with redirect

Serve your site over HTTPS with nginx: certificate paths, HTTP to HTTPS redirect, HSTS and a proxy or static site.

For Linux servers running nginx 1.18 or newer with a Let’s Encrypt certificate.

This nginx server block turns on HTTPS with your certificate, sends visitors from HTTP to HTTPS, and either passes requests to your app or serves a folder of files. Get a certificate first (the steps are below), then fill in the form.

Create your config file

Type your own values. The file below updates as you type, and nothing you type leaves your browser.

Certbot saves it in /etc/letsencrypt/live

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

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

    ssl_certificate     /etc/letsencrypt/live/example.com/fullchain.pem;
    ssl_certificate_key /etc/letsencrypt/live/example.com/privkey.pem;
    ssl_protocols TLSv1.2 TLSv1.3;

    location / {
        proxy_pass http://127.0.0.1:3000;
        proxy_set_header Host $host;
        proxy_set_header X-Real-IP $remote_addr;
        proxy_set_header X-Forwarded-Proto $scheme;
    }
}

How this setup works

Diagram: Browser, then nginx, then Your app or files
Browser (https://your domain) → nginx (port 443, certificate) → Your app or files (behind nginx)

Step-by-step setup

  1. Get a certificate first: sudo certbot certonly --nginx -d example.com. Your domain must already point to this server.
  2. Save the config as /etc/nginx/conf.d/example.com.conf.
  3. Test it: sudo nginx -t.
  4. Apply it: sudo systemctl reload nginx.
  5. Open https://example.com and check the padlock.
  6. Test automatic renewal: sudo certbot renew --dry-run.

What the important lines mean

Common errors and how to fix them

cannot load certificate: No such file or directory
The path is wrong, or no certificate has been issued yet. Run certbot first and copy the exact paths it prints.
ERR_TOO_MANY_REDIRECTS
Your app, or a service such as Cloudflare in flexible mode, also redirects to HTTPS and loops. Make sure only one place does the redirect.
nginx: [warn] the “http2” directive is deprecated
Your nginx is 1.25.1 or newer. Tick the option for newer nginx above.
unknown directive “http2”
Your nginx is older than 1.25.1. Untick the option for newer nginx so the http2 flag goes on the listen line.
bind() to 0.0.0.0:443 failed
Another program uses port 443. Find it with the port checker.
key values mismatch (SSL_CTX_use_PrivateKey_file failed)
Either the key does not belong to the certificate, or the chain file is in the wrong order. The server certificate must come first: cat site.crt bundle.crt > chained.crt. nginx otherwise pairs the key with the first certificate in the bundle.
Works in my browser but fails in curl, apps or older devices
Browsers may cache intermediate certificates, so a missing chain can go unnoticed. Check what the server sends with openssl s_client -connect example.com:443; with no bundle only certificate #0 is shown.
Wrong certificate shown for another domain on the same IP
The TLS handshake happens before the HTTP request, so nginx picks the certificate by SNI. Give each site its own server block with its own server_name and certificate, or use one multi-name certificate.

Frequently asked questions

How do I get a free SSL certificate for nginx?
Use Let’s Encrypt with certbot. Run certbot certonly --nginx -d your-domain, then point the config at the files it creates.
How do I redirect HTTP to HTTPS in nginx?
Add a server block on port 80 that returns 301 to https://$host$request_uri. The generator does this when the redirect option is ticked.
Should I turn on HSTS?
Only after HTTPS works for the whole site. HSTS cannot easily be undone for visitors who already received it.
Does *.example.com cover example.com?
No. A wildcard covers one subdomain level only: it matches www.example.com but not example.com or www.sub.example.com. List both names on the certificate.
Do I need to set ssl_protocols and ssl_ciphers?
Since nginx 1.27.3 the default protocols are TLSv1.2 and TLSv1.3, and the ciphers default to HIGH:!aNULL:!MD5, so explicit settings are generally not needed.
Can the certificate and key be in one file?
Yes, point both ssl_certificate and ssl_certificate_key at it and restrict its permissions. Only the certificate part is sent to visitors.
How do I reduce the CPU cost of HTTPS?
Handshakes are the expensive part. Use keepalive_timeout and a shared session cache such as ssl_session_cache shared:SSL:10m; 1 MB holds about 4000 sessions.

Written for: nginx 1.26, Ubuntu 24.04, certbot 2 · Updated 2026-10-03

Free tools that help

Fix a problem with this setup

More server configs

See all 41 config generators