Common scenarios where Nginx misconfiguration leads to ERR_TOO_MANY_REDIRECTS

expense or outlay Nginx When acting as a Web server, theERR_TOO_MANY_REDIRECTS error is one of the common configuration problems. This error is usually due to the presence of the Nginx configurationIncorrect redirection rulesmaybeProxy Settingsthat puts the request in an infinite redirection loop. This article focuses on some of the common causes of ERR_TOO_MANY_REDIRECTS Incorrect Nginx configuration issues and their solutions.

Image [1] - Resolving ERR_TOO_MANY_REDIRECTS error in Nginx configuration

1. HTTP to HTTPS redirection loop

One of the most common causes ofIncorrect HTTP to HTTPS redirection configuredOften, sites want to force all traffic to use HTTPS. Often, sites want to force all traffic to use HTTPS, and if Nginx is not configured correctly, this can result in the HTTP to HTTPSredirectscirculateThe

Wrong Practices:

1.Configuring the wrong server redirection rules: The server's configuration may have accidentally introduced a redirection loop that caused requests to keep bouncing between HTTP and HTTPS.

2.Browser Cache Issues: Browsers may cache redirect information, especially if HTTP to HTTPS redirects are not handled correctly, causing loops.

3.Load balancer or proxy configuration issues: If a load balancer or proxy server is used, there may be a misconfiguration that causes requests to redirect endlessly between HTTP and HTTPS.

Image [2] - Resolving ERR_TOO_MANY_REDIRECTS error in Nginx configuration

Solution:
assure HTTP and HTTPS configurations are handled separately and do not redirect each other. For example, a proper HTTP to HTTPS redirection configuration should look like this:

# HTTP -> HTTPS redirection
server {
    listen 80; server_name example.com; server_name
    server_name example.com; return 301 ;
    return 301 https://$host$request_uri;
}

# HTTPS configuration
server {
    listen 443 ssl; server_name example.com; }
    server_name example.com.

    ssl_certificate /path/to/certificate; ssl_certificate_key /path/to/private_key; ssl_certificate_key /path/to/certificate
    ssl_certificate_key /path/to/private_key; ssl_certificate_key /path/to/private_key; ssl_certification_key

    # Other HTTPS configurations
}

2. X-Forwarded-Proto Misdirection of the head

If Nginx is working as a reverse proxy, the back-end server may be able to use Nginx based on the X-Forwarded-Proto The header determines the protocol type of the request. If misconfigured, this may cause the back-end server to incorrectly assume the request is HTTP or HTTPS, triggering a redirect.

Wrong Practices:

  • In the Nginx configuration, theX-Forwarded-Proto The headers were passed incorrectly, causing the back-end application to incorrectly decide that it needed to redirect to HTTPS or HTTP.

Solution:
Ensure that you pass the correct X-Forwarded-Proto Head:

X-Forwarded-Proto header passed correctly

3. Conflict of agency agreements

If a request is proxied to a backend server via Nginx, inconsistencies in the protocol (HTTP or HTTPS) can also cause redirect loops. In particular, when an HTTP request is proxied to an HTTPS backend server, if the backend server incorrectly assumes the request is HTTP, it may send an HTTPS redirect, causing a loop.

Wrong Practices:

  • Nginx receives the HTTP request and proxies it to an HTTPS backend, but the backend redirects back to HTTP.

Solution:
Make sure that Nginx is using the same protocol as the backend server and that it is configured correctly proxy_set_header Head.

Correct usage of proxy_set_header header

4. Caching issues

Browsers or proxy servers may cache redirect responses, which can lead to infinite when revisiting theredirectsThe

Sample Scenario:

  • As the browser caches the wrong redirection response, it causes subsequent requests to enter the redirection loop again.

Solution:
Clear Browser Cache

Image [5] - Resolving ERR_TOO_MANY_REDIRECTS error in Nginx configuration

ERR_TOO_MANY_REDIRECTS incorrectIt is usually caused by problems with redirection, proxies, and protocol handling in the Nginx configuration. This error can be avoided by double-checking and tweaking the Nginx configuration to ensure proper redirection rules, proxy settings, and header passing.


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: liuyan
THE END
If you like it, support it.
kudos155 share (joys, benefits, privileges etc) with others
commentaries sofa-buying

Please log in to post a comment

    No comments