Skip to content

Redirecting HTTP to HTTPS in Nginx — the right way and the wrong way

How to do the 80→443 redirect in Nginx without leaking redirect loops, HSTS surprises, or ACME validation failures. The minimal block, plus what to add for HSTS preload.

Forcing every visitor onto HTTPS is the smallest, most important Nginx change a site does. The minimal block is three lines; the trap is the one that catches certbot’s renewal because the redirect swallowed the ACME challenge. This article shows the redirect block we keep in every server config and what to do if you want HSTS preload on top.

How to verify

Check the current behaviour from the outside:

curl -sI http://example.com | head -5
curl -sIL http://example.com | grep -iE 'HTTP/|location'
curl -sI https://example.com | grep -i strict-transport-security
curl -sI http://example.com/.well-known/acme-challenge/test 2>&1 | head -5

A clean setup returns 301 Moved Permanently with Location: https://example.com/ on the first request, then a 200 (or other valid response) on the HTTPS hop. The ACME challenge path should return 200 or 404 — not a 301.

What’s happening

Nginx terminates TLS on port 443 and listens on port 80 for plain HTTP. To force HTTPS, you keep a small port-80 block whose only job is to redirect everything to the TLS counterpart. Return codes matter: 301 (permanent) is what you want once the redirect is stable and you want browsers and search engines to cache it; 302 (temporary) is the right choice during a rollout when you might roll back.

The HSTS header complements the redirect. The browser still does one HTTP request on first visit, sees the 301 to HTTPS, hits HTTPS, sees Strict-Transport-Security: max-age=…, and from then on goes straight to HTTPS without asking. With HSTS preload submitted to the browsers’ built-in lists, even the first visit goes to HTTPS.

The procedure

  1. The minimal redirect block — kept separate from the TLS block:
    server {
        listen 80;
        listen [::]:80;
        server_name example.com www.example.com;
    
        location /.well-known/acme-challenge/ {
            root /var/www/letsencrypt;
        }
    
        location / {
            return 301 https://$host$request_uri;
        }
    }
    The /.well-known/acme-challenge/ location is the crucial detail — certbot drops its challenge files there, and the redirect must not touch them.
  2. The matching TLS block:
    server {
        listen 443 ssl http2;
        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;
    
        add_header Strict-Transport-Security "max-age=31536000; includeSubDomains" always;
        # ... the rest of the site config ...
    }
    The always argument on add_header makes Nginx emit it on error responses too.
  3. Use $host not $server_name in the redirect. $host is the requested name; $server_name is the first one in the block. If a visitor types a name not in your block list, $host lets them land on themselves rather than your canonical name.
  4. Reload: sudo nginx -t && sudo systemctl reload nginx.
  5. Validate from outside: curl -sIL http://example.com/foo?bar=1 should follow exactly one redirect to https://example.com/foo?bar=1 and then return your normal response.
  6. Decide on HSTS preload. Once you are confident every subdomain serves TLS and you will not need to roll back, extend the HSTS header and submit to https://hstspreload.org/:
    add_header Strict-Transport-Security "max-age=63072000; includeSubDomains; preload" always;
    Once preloaded, the entry is hard to remove — browsers refresh their built-in list slowly.

Common pitfalls

  • A catch-all redirect that includes /.well-known/acme-challenge/. Certbot renews fail with “Invalid response from … 301”. The challenge location must come before the catch-all.
  • Redirecting to https://www.example.com from http://example.com (or vice versa) — that is two redirects for a single visitor, which slows the first hit. Pick the canonical name and redirect everything to it directly.
  • Using rewrite … redirect; instead of return 301. Both work, but return is faster and unambiguous; rewrite is for URL transformation, not protocol redirection.
  • HSTS on a host that mixes HTTP and HTTPS endpoints (legacy admin, mail interface). The header on the public site causes the HSTS browser cache to block the legacy ones.
  • Forgetting always on the HSTS add_header — add_header only fires on 2xx, 3xx (some), and 1xx by default; a 503 from a maintenance page would not carry HSTS.

In the engagements we run, the redirect block is templated and never edited by hand; it is generated by the same Ansible role that manages the TLS material. See Nginx security headers for the wider header posture we apply. Stack Harbor operates this end-to-end as part of managed operations.