RackUp IT - View Cloud Hosting Plans

Get started now or speak directly with our engineering team for specialized assistance.

Logo
DevelopmentWEB & APPCalculatorTOOLServicesProductBlogOur TeamCareers
View Plans
Home>Blog>Nginx: Redirect HTTP to HTTPS (and www to non-www) the Right Way
Server Stack

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

Alex Vance
Alex Vance
Published Nov 20, 2026•✓ Verified Expert Reviewed•3 min read
Nginx redirect HTTP to HTTPS cover

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
  • •The Basic HTTP to HTTPS Redirect
  • •HTTPS Plus One Canonical Hostname
  • •Let Certbot Add the Redirect
  • •Add HSTS (Carefully)
  • •Test and Reload
  • •Fixing a Redirect Loop
  • •Frequently Asked Questions
  • •Run Nginx on Your Own Server

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 301 is a permanent redirect, which tells browsers and search engines the HTTPS URL is the real one.

  • $host keeps the hostname the visitor used.

  • $request_uri keeps the full path and query string, so /blog/post?x=1 goes 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 --redirect

Check 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/page

Each 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 301 for HTTP only lives in the listen 80 block.

  • The app also redirects: WordPress or your framework thinks it’s on HTTP behind a proxy. Pass X-Forwarded-Proto and 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.

← Previous ArticleDNS_PROBE_FINISHED_NXDOMAIN: What It Means and How to Fix ItNext Article →What Is a VPS? Virtual Private Servers Explained
Interactive 60-Second Tool

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.

Free Migration 99.9% Uptime Target
Calculate My Hosting Build
Alex Vance
Written ByAlex Vance

Cloud Infrastructure Engineer.

99.9% Uptime Target

RackUp IT - View Cloud Hosting Plans

View Hosting Plans

View Hosting Plans

CONTENTS

  • Nginx: Redirect HTTP to HTTPS (and www to non-www) the Right Way
  • The Basic HTTP to HTTPS Redirect
  • HTTPS Plus One Canonical Hostname
  • Let Certbot Add the Redirect
  • Add HSTS (Carefully)
  • Test and Reload
  • Fixing a Redirect Loop
  • Frequently Asked Questions
  • Run Nginx on Your Own Server

Categories

  • VPS Hosting
  • DevOps & Tutorials
  • WordPress Hosting
  • Cloud Hosting
  • Server Stack
  • Networking & Security
  • Web Hosting / Managed Cloud
  • E-Commerce
  • Hosting & SEO
  • Hosting Benchmarks
  • Cloud Backup & DevOps
  • Managed VPS

Recent Posts

  • Vultr Alternatives in 2026: VPS Hosting for Developers Compared
  • Docker Port Mapping Explained: -p, EXPOSE and Compose ports
  • Bluehost Alternatives (2026): WordPress Hosting Without the Renewal Price Jump

Tags

NginxHTTPSRedirectsSSL

Related Posts

Install Apache on Ubuntu coverServer Stack
Nov 22, 2026

How 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.

Read Article →
Traefik vs Caddy vs Nginx coverServer Stack
Nov 4, 2026

Traefik 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.

Read Article →
Nginx monitoring coverServer Stack
Nov 2, 2026

Nginx 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.

Read Article →
Logo

Expert Web & App Development, UI/UX Design, Branding, and Consulting to scale your business.

Company

  • About Us
  • Blog
  • Contact
  • Our Team
  • Careers

Services

  • Web Development
  • App Development
  • UI/UX Design
  • Consulting
  • Managed WordPress VPS
  • Website Speed Test

Subscribe to our newsletter

We accept
  • bKash
  • Nagad
  • Rocket
  • Upay
  • CellFin
  • Meghna Pay
  • OK Wallet
  • Tap
  • mCash
  • DBBL Nexus
  • Visa
  • Mastercard
  • American Express
  • Crypto
Pay with Visa, Mastercard, American Express, DBBL Nexus, CellFin, Rocket, bKash, Tap, Meghna Pay, Nagad, mCash, Upay and OK Wallet. Verified by EPS

© 2026 Rackup IT Solution. All rights reserved.

Privacy PolicyTerms of Service