Nginx: Redirect HTTP to HTTPS (and www to non-www) the Right Way

Key Takeaways
- ✓Use return 301 https://$host$request_uri; in the port-80 server block.
- ✓Send every variant to one canonical hostname in a single hop.
- ✓Add HSTS only after HTTPS works, starting with a short max-age.
- ✓Cloudflare Flexible SSL is the most common cause of redirect loops.
Table of Contents
Nginx: Redirect HTTP to HTTPS (and www to non-www) the Right Way
Once your site has a certificate, every visitor who arrives on http:// should be sent to https:// with a permanent 301 redirect, and every visitor should end up on one canonical hostname, either www.example.com or example.com. In Nginx that’s a few lines, but the details matter: a redirect that drops the path, sends people through two hops, or loops behind a CDN causes real problems for users and SEO. This guide shows the correct server blocks for each case.
Quick answer: add a server block that listens on port 80 and returns 301 https://$host$request_uri, then test with sudo nginx -t and reload.
The Basic HTTP to HTTPS Redirect
server {
listen 80;
listen [::]:80;
server_name example.com www.example.com;
return 301 https://$host$request_uri;
}return 301is a permanent redirect, which tells browsers and search engines the HTTPS URL is the real one.$hostkeeps the hostname the visitor used.$request_urikeeps the full path and query string, so/blog/post?x=1goes to the same page on HTTPS, not the home page.
Prefer return over rewrite for redirects: it’s simpler and faster, and Nginx’s own documentation recommends it for this job.
HTTPS Plus One Canonical Hostname
To make https://www.example.com the only address, with every other variant redirected in a single hop:
# 1. All HTTP traffic, both hostnames: one hop to the final URL
server {
listen 80;
listen [::]:80;
server_name example.com www.example.com;
return 301 https://www.example.com$request_uri;
}
# 2. HTTPS on the non-canonical name: redirect to www
server {
listen 443 ssl;
listen [::]:443 ssl;
server_name example.com;
ssl_certificate /etc/letsencrypt/live/example.com/fullchain.pem;
ssl_certificate_key /etc/letsencrypt/live/example.com/privkey.pem;
return 301 https://www.example.com$request_uri;
}
# 3. The real site
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;
root /var/www/example.com;
# ... the rest of your site config
}The certificate must cover both names (example.com and www.example.com), or visitors typing the non-canonical HTTPS address see a warning before the redirect. To prefer the bare domain instead, swap the names around.
Let Certbot Add the Redirect
If you use Certbot’s Nginx plugin, it can add the HTTP to HTTPS redirect for you when it installs the certificate:
sudo certbot --nginx -d example.com -d www.example.com --redirectCheck the result in your site config afterwards, and add the www/non-www rule yourself if you want a single hostname. The full certificate setup is in Nginx with Let’s Encrypt.
Add HSTS (Carefully)
HTTP Strict Transport Security tells browsers to use HTTPS for your domain automatically on future visits, even if someone types http://. Add it to the HTTPS server block once everything works over HTTPS:
add_header Strict-Transport-Security "max-age=31536000" always;Start with a short max-age (for example 300 seconds) while testing. Only add includeSubDomains if every subdomain supports HTTPS, because browsers will refuse plain HTTP for all of them.
Test and Reload
sudo nginx -t
sudo systemctl reload nginx
curl -I http://example.com/some/page
curl -I https://example.com/some/pageEach response should be 301 with a Location header pointing at https://www.example.com/some/page, and the final URL should return 200. More on safe reloads in testing and reloading Nginx config.
Fixing a Redirect Loop
ERR_TOO_MANY_REDIRECTS means something keeps sending the browser back and forth. Common causes:
Cloudflare in “Flexible” SSL mode: Cloudflare talks to your server over HTTP, your server redirects to HTTPS, and the cycle repeats. Use “Full (strict)” with a valid certificate on the server.
The redirect is in the HTTPS block too: make sure
return 301for HTTP only lives in thelisten 80block.The app also redirects: WordPress or your framework thinks it’s on HTTP behind a proxy. Pass
X-Forwarded-Protoand configure the app to trust it.Conflicting www rules between Nginx, the app and the CDN. Pick one canonical form everywhere.
Frequently Asked Questions
Should I use a 301 or 302 redirect for HTTPS?
A 301, because the move to HTTPS is permanent. Search engines transfer ranking signals to the HTTPS URLs.
Is return better than rewrite in Nginx?
For whole-site redirects, yes. return 301 is simpler and avoids regular expressions.
Do I need a certificate for the www and non-www names?
Yes, if visitors can reach both over HTTPS. One certificate can cover both names.
How do I redirect only one domain to HTTPS?
Set server_name in the port-80 block to just that domain, and keep other domains in their own server blocks.
Why does my redirect go to the home page?
The redirect is missing $request_uri. Use return 301 https://$host$request_uri; to keep the path.
Run Nginx on Your Own Server
RackUp’s Developer VPS gives you root access to configure Nginx exactly how you want: 8 vCPU, 12 GB RAM, 150 GB NVMe and 32 TB of monthly bandwidth for $35 a month. See plans.
Unsure Which Cloud Hosting Plan Fits Your Site?
Answer 4 quick diagnostic questions to estimate the server CPU, RAM and storage you need with transparent pricing.
Cloud Infrastructure Engineer.
Related Posts
Server StackHow to Install and Set Up Apache on Ubuntu (24.04 and 22.04)
Install and set up Apache on Ubuntu step by step: firewall, a virtual host for your domain, .htaccess support, free HTTPS with Certbot and everyday commands.
Server StackTraefik vs Caddy vs Nginx: Which Reverse Proxy Should You Use?
Traefik routes Docker containers automatically, Caddy does HTTPS with almost no config, and Nginx does everything. Which reverse proxy fits your setup.
Server StackNginx Monitoring: stub_status, Logs, Prometheus and Alerts
Set up Nginx monitoring with the built-in stub_status page and timed access logs, then add Prometheus, Grafana and the alerts that catch problems early.