webzod
account

Redirect HTTP straight to HTTPS on nginx

headers.redirect.insecure_first_hop · effort: minutes

Why it matters

The first redirect from http:// goes to another http:// address, so the visitor's request travels unencrypted twice before reaching https. The name the visitor typed also never gets an HSTS header, which is only honoured over https.

The fix

  1. Use one port 80 server for both names that redirects to https on the host that was requested; the https server then sends the non-canonical name to the canonical one. Run sudo nginx -t and reload.
nginx
server {
    listen 80;
    listen [::]:80;
    server_name example.com www.example.com;
    return 301 https://$host$request_uri;
}

The examples use example.com. Replace it with your own domain.

Read the documentation (opens in a new tab)

Scan your domain to see whether this applies to you.