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.

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.

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