The Redirect Block
server {
listen 80;
listen [::]:80;
server_name example.com www.example.com;
return 301 https://$host$request_uri;
}
$host is the host name the visitor asked for, and $request_uri is the path with the query string. The site itself is served by a separate block that listens on 443. A 301 is permanent: browsers and search engines remember it and move their links to the HTTPS address.
Use return, not rewrite and not an if on the scheme. return stops processing at once and needs no regular expression or condition, while an if inside a location changes which configuration the request uses and is easy to get wrong.
Make Sure HTTPS Works First
curl -I https://example.com/
Expect a 200 and no certificate error. If you redirect before the certificate is in place, every visitor lands on a browser warning. Current Certbot releases add a redirect of their own by default when you use the Nginx plugin; check that you do not end up with two. Getting the certificate is covered in Certbot for Nginx.
Keep the ACME Challenge Reachable
With Certbot's webroot method, Let's Encrypt fetches a file from /.well-known/acme-challenge/ over port 80. Serve that path directly and redirect everything else:
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;
}
}
Keep port 80 open in the firewall: renewals use it again.
Redirect www to the Bare Domain
server {
listen 443 ssl;
listen [::]:443 ssl;
server_name www.example.com;
ssl_certificate /etc/letsencrypt/live/example.com/fullchain.pem;
ssl_certificate_key /etc/letsencrypt/live/example.com/privkey.pem;
return 301 https://example.com$request_uri;
}
The certificate must cover www as well, or the browser warns before it ever sees the redirect. In the port 80 block, write https://example.com$request_uri instead of $host so that www visitors need one hop, not two. Swap the names if www is your main address.
Test and Reload
sudo nginx -t
sudo systemctl reload nginx
curl -I "http://example.com/page?id=1"
curl -IL http://www.example.com/
The third command should show a 301 and a Location header with the same path and query on https. -L follows every hop, so you can count them. Reload rather than restart: a failed test then leaves the running server as it was.
Redirect Loops Behind a CDN
If a CDN or load balancer connects to your server over plain HTTP while visitors use HTTPS, every request arrives on port 80, is redirected, and comes back on port 80. The browser reports too many redirects. Cloudflare's Flexible encryption mode works this way, and Cloudflare documents the loop for origins that redirect to HTTPS. The fix is to encrypt the connection to your server too, with Full (strict) mode and a valid certificate on the server, or to let the CDN or load balancer do the redirect instead. See ERR_TOO_MANY_REDIRECTS.
HSTS, with Care
add_header Strict-Transport-Security "max-age=300" always;
HSTS tells browsers to use HTTPS for your domain without asking port 80 first. Put the line in the 443 block, start with a short max-age such as five minutes, and raise it in steps once every page and subdomain works over HTTPS. A browser that has seen the header refuses plain HTTP until it expires; only a new header with max-age=0, received over HTTPS, clears it for that browser. A location with add_header lines of its own does not inherit this one, so repeat it there. Add includeSubDomains or preload only when you are sure.
Which Redirect Code?
| Code | Meaning |
|---|---|
| 301 | Permanent; clients may resend POST as GET |
| 302 | Temporary |
| 307 | Temporary; method kept |
| 308 | Permanent; method kept |
301 is the usual choice for a whole site. The other codes are in HTTP status codes explained.