Redirects & rewrites

Redirect all HTTP to HTTPS

2 min · updated June 16, 2026

The correct, fast way to force HTTPS is a tiny dedicated server block on port 80 that does nothing but return 301. Don’t use rewrite or if for this — return is unambiguous and cheaper.

# Plain HTTP: redirect everything to HTTPS.
server {
    listen 80;
    listen [::]:80;
    server_name example.com www.example.com;

    return 301 https://$host$request_uri;
}

# Your real site.
server {
    listen 443 ssl;
    listen [::]:443 ssl;
    http2 on;
    server_name example.com www.example.com;

    # ssl_certificate ...; (see the TLS recipe)

    location / {
        # ...
    }
}

Why this shape:

sudo nginx -t && sudo nginx -s reload

← All snippets