Fix "Too Many Redirects" in Nginx: Prevent Infinite HTTP to HTTPS Loops
Fix Nginx redirect loops caused by HTTP to HTTPS misconfigurations. Learn how to stop 'Too Many Redirects' errors with correct server block setup for fast, secure site access.

Encountering endless redirects between HTTP and HTTPS on your Nginx website? This common issue, often called an "infinite redirect loop," can be frustrating. Stack Overflow This guide provides a step-by-step solution to configure Nginx for smooth and secure redirection.
What Are Nginx Infinite Redirect Loops and Why They Happen
The goal of HTTP to HTTPS redirection is simple: automatically route http://yourdomain.com requests to https://yourdomain.com. However, misconfiguration can create a cycle where Nginx repeatedly redirects between HTTP and HTTPS, preventing users from accessing the page and resulting in the dreaded "Too Many Redirects" browser error. sitechecker
How to Fix Infinite Redirect Loops in Nginx (Step-by-Step)
This guide provides a solution to configure Nginx correctly, eliminating frustrating redirect loops. checkout infinite redirect for next js
Step 1: Redirect HTTP to HTTPS Properly in Nginx
The primary cause of infinite redirect loops is often improper HTTP to HTTPS redirection. Configure your Nginx configuration file as follows:
server {
listen 80;
server_name yourdomain.com;
# Redirect all HTTP traffic to HTTPS
return 301 https://$host$request_uri;
}
listen 80;: Nginx listens on port 80 for HTTP traffic.server_name yourdomain.com;: Specifies the domain to redirect.return 301 https://$host$request_uri;: Performs a permanent (301) redirect to the HTTPS version, preserving the original URL path. community.cloudflare
Step 2: Setup a Secure HTTPS Server Block in Nginx
This server block handles HTTPS requests on port 443 and utilizes your SSL certificates:
server {
listen 443 ssl;
server_name yourdomain.com;
ssl_certificate /etc/nginx/ssl/yourdomain.crt;
ssl_certificate_key /etc/nginx/ssl/yourdomain.key;
location / {
try_files $uri $uri/ =404;
}
}
listen 443 ssl;: Nginx listens on port 443 for HTTPS traffic.ssl_certificateandssl_certificate_key: Paths to your SSL certificate and private key. Ensure these paths are correct.location /: This block handles incoming HTTPS requests.try_filesattempts to serve the requested file or returns a 404 error if not found.
Step 3: Avoid Nginx Redirect Loops with Clean Separation
The key to preventing loops is to ensure that redirection occurs only from HTTP to HTTPS. Avoid configuring redirects within the HTTPS server block. Keep the HTTP block solely for redirection and the HTTPS block for serving secure content. nife.io - Web development reference
Step 4: Validate and Reload Nginx Configuration
Before restarting Nginx, test your configuration:
sudo nginx -t
If no errors are reported, reload Nginx:
sudo systemctl reload nginx
Step 5: Clear Browser Cache to Fix Persistent Redirects
If the "Too Many Redirects" error persists, clearing your browser's cache and cookies might resolve the issue.
Final Thoughts: Best Practices to Prevent Redirect Loops
Setting up an HTTP to HTTPS redirect in Nginx is pretty simple, but getting it right is key to avoiding endless redirect loops. The best way to do it is by setting up two separate server blocks—one to catch HTTP traffic and send it to HTTPS, and another to handle secure HTTPS connections.
This way, your users get a seamless and secure browsing experience without unnecessary redirects slowing things down. Frontend Deployment with Nife
From the team at Nife
Deploy your next app in minutes, not days.
Nife is a multi-cloud edge platform that lets you deploy, scale, and manage applications globally without the infrastructure headache.