Troubleshooting Nginx Configuration Error Causing TOO_MANY_REDIRECTS

TOO_MANY_REDIRECTS(Too many redirects) is a common error on websites. This problem affects user access and SEO results. In this article, we will explain the troubleshooting ideas and solutions for TOO_MANY_REDIRECTS caused by Nginx misconfiguration.

Image [1]-Troubleshooting TOO_MANY_REDIRECTS due to Nginx misconfiguration

1. TOO_MANY_REDIRECTS error principle

browser (software)When accessing a website, if the server has misconfigured the redirection rules, causing requests to be sent in multiple URL The browser will eventually return a TOO_MANY_REDIRECTS error in order to prevent a dead loop.

Common Performance:

  • Page load fails with ERR_TOO_MANY_REDIRECTS
  • F12 Developer Tools Network panel shows duplicate 301 or 302 redirects

2. Pre-examination preparations

Before troubleshooting, confirm the following information:

  • Domain name used for the site (is there a mix of www and non-www)
  • Whether SSL (HTTPS) is configured or not
  • Whether or not you are using the CDN or a reverse proxy (such as Cloudflare)

3. Common Causes of Redirection Errors with Nginx Configuration

3.1 Conflicting HTTP and HTTPS Configurations

If you also configure HTTP to automatically jump to HTTPS, and the HTTPS configuration jumps back to HTTP, you will create an infinite loop.

Image [2]-Troubleshooting TOO_MANY_REDIRECTS due to Nginx misconfiguration

Typical error example:

server {
    listen 80; server_name example.com; server_name
    server_name example.com; server_name
    return 301 http://example.com$request_uri; # error: jump to http
}

server {
    listen 443 ssl; server_name example.com; return 301 ; # error
    server_name example.com;
    return 301 https://example.com$request_uri; } server { listen 443 ssl; server_name example.com; return 301 https://example.com$request_uri; }
}

This configuration redirects HTTP requests to HTTP (itself) and HTTPS requests to HTTPS (itself) and does not complete the expected HTTP -> HTTPS hop.

3.2 Misconfiguration of www and non-www redirects

TOO_MANY_REDIRECTS are also generated when www and non-ww jump to each other.

Example of a misconfiguration:

server {
    server_name example.com.
    return 301 http://www.example.com$request_uri;
}

server {
    server_name www.example.com; return 301 www.example.com; } server { server_name www.example.com; }
    return 301 http://example.com$request_uri; }
}

This configuration causes:

  • Visit example.com Jump to www.example.com
  • Going to www.example.com brings you back to example.com
  • infinite loop

3.3 Reverse Proxy and HTTPS Configuration Conflicts

Cloudflare was used,pagodaAfter the reverse proxy or CDN, if you don't have the right X-Forwarded-Proto With this parameter, Nginx may misrecognize the URL protocol (treating HTTPS as HTTP), resulting in a page jump error.

Solution:

Add it in the configuration file:

The configuration file is usually in one of the following locations:

  • nginx / open nginx.conf (the main configuration file, it is not recommended to edit the server block directly)
Image [3]-Troubleshooting TOO_MANY_REDIRECTS due to Nginx misconfiguration
server {
    listen 80; server_name example.com ;
    server_name example.com www.example.com;

    set $https_off ""; if ($http_x_forwarded_proto !
    if ($http_x_forwarded_proto ! = "https") {
        set $https_off "redirect";
    }
    if ($https_off = "redirect") {
        return 301 https://$host$request_uri;
    }
}

Or use the standard approach:

server {
    listen 80; server_name example.com ;
    server_name example.com www.example.com; return 301 www.example.com;
    return 301 https://$host$request_uri;
}

Add it to the HTTPS configuration:

server {
    listen 443 ssl; server_name example.com ;
    server_name example.com www.example.com;
    # Configuring SSL Certificate...
}

Also in the Nginx global configuration fastcgi_params file or server segment:

fastcgi_param HTTPS on.

Ensure that the reverse proxy passes HTTPS information.

4. Systematic troubleshooting process

The following are the recommended steps for troubleshooting TOO_MANY_REDIRECTS:

  • Check your browser's Network panel
    Look at the jump link and verify that it is a loop between HTTP and HTTPS or www and non-www.
  • Viewing the Nginx Configuration File
    • Is there duplicate configuration of multiple server_names?
    • Is return 301 pointing to the same domain name causing a loop
  • Confirming the reverse proxy configuration If you are using Cloudflare, make sure "SSL/TLS Mode" is selected. Full (strict)To avoid loops caused by the Flexible mode.
  • ferret out WordPress or other CMS settings If the website URL is set to HTTP, but the server is configured to force HTTPS, this may also result in duplicate hops.
Image [4]-Troubleshooting TOO_MANY_REDIRECTS due to Nginx misconfiguration
  • Clearing Browser and CDN Caches Sometimes the configuration has been changed but the cache has not been updated and still shows the old error.

summarize

TOO_MANY_REDIRECTS Yes Nginx The key to common problems in configuration is Avoiding redirection loopsThe following is an example of an error that can be resolved by checking HTTP vs. Checking HTTP vs. HTTPS, www vs. non-www configurations, in conjunction with web server, CDN, and application settings troubleshooting can resolve these types of errors.


Contact Us
Can't read the tutorial? Contact us for a free answer! Free help for personal, small business sites!
Customer Service
Customer Service
Tel: 020-2206-9892
QQ咨詢:1025174874
(iii) E-mail: info@361sale.com
Working hours: Monday to Friday, 9:30-18:30, holidays off
? Reprint statement
Author: linxiulian
THE END
If you like it, support it.
kudos110 share (joys, benefits, privileges etc) with others
commentaries sofa-buying

Please log in to post a comment

    No comments