Redirect HTTP to HTTPS instead of answering an error on nginx
Why it matters
Visitors who type the bare domain or follow an old http:// link get an error page instead of the site. A permanent redirect sends every http request to the same page over https.
Staged change. Do this in stages and check each one before the next, so a mistake does not break your site or your mail.
The fix
- Stage 1: confirm the port 443 server serves every page correctly.
- Stage 2: replace the contents of the port 80 server (or the default server answering the error) with the redirect below, run sudo nginx -t and reload.
nginx
server { listen 80; listen [::]:80; server_name example.com; return 301 https://$host$request_uri; }
The examples use example.com. Replace it with your own domain.
Related
- Reported by the HTTP to HTTPS redirect check
- Serve the site over HTTPS
- Stop redirecting HTTPS to HTTP
- Redirect HTTPS straight to the other host's HTTPS page
- Redirect HTTP to HTTPS
- Check that browsers are redirected from HTTP to HTTPS
- Fix the broken HTTPS redirect chain
- Shorten the redirect chain
- Fix the HTTP redirect chain
- Redirect HTTP straight to HTTPS
- Answer HTTP with a redirect to HTTPS
- Redirect www and apex to one host
- Make the www or apex name work over HTTPS
Scan your domain to see whether this applies to you.