Analisador de Redirects Nginx

Analise cadeias de redirect Nginx e detecte loops

Analyzing Nginx Redirect Rules for Loops and Chains

Nginx redirect configuration controls how URLs are rewritten and how clients are redirected between locations. Misconfigured rewrite rules create redirect chains (A→B→C) that slow page loads and waste crawl budget, redirect loops (A→B→A) that make pages completely inaccessible, and overly broad regex patterns that capture URLs unintentionally. The Redirect Analyzer inspects your Nginx configuration to detect these issues before they impact users and search engine crawling.

The analyzer parses rewrite directives, return statements, and location block redirects to build a complete redirect graph. It traces each redirect path to detect chains longer than one hop, loops that create infinite redirect cycles, invalid regex patterns that will cause Nginx configuration errors, and missing termination flags that allow request processing to continue unexpectedly. All analysis happens entirely in your browser.

Redirect Chains and Performance Impact

A redirect chain occurs when a URL redirects to another URL that itself redirects to a third URL. Each hop adds latency for users and wastes search engine crawl budget:

  • Two-hop chains (A→B→C): Add 100-300ms per redirect hop, doubling time-to-first-byte
  • Protocol + path chains: http://example.com/oldhttps://example.com/oldhttps://example.com/new — a common pattern when HTTPS redirect and path redirect are configured separately
  • www normalization chains: http://www.example.comhttps://www.example.comhttps://example.com

The analyzer traces all redirect paths and reports chains longer than one hop, recommending consolidated redirects that go directly from origin to final destination in a single hop.

Redirect Loops and Infinite Cycles

Redirect loops make pages completely inaccessible — browsers display "ERR_TOO_MANY_REDIRECTS" after detecting the cycle. Common causes include:

  • Conflicting server blocks: A non-www block redirects to www, while the www block redirects back to non-www
  • Proxy redirect conflicts: Nginx redirects to HTTPS, but the upstream proxy also issues a redirect based on the X-Forwarded-Proto header
  • Rewrite rule ordering: Multiple rewrite rules that match the same URL and redirect to URLs that match each other
  • Location block conflicts: A location block catches the redirect target and redirects back to the original

The analyzer builds a directed graph of all redirect relationships and uses cycle detection to identify loops before they reach production.

Regex Pattern Validation

Nginx rewrite rules use PCRE regex patterns for URL matching. The analyzer validates regex patterns for correctness and safety:

  • Invalid regex syntax: Unescaped special characters, unclosed groups, or invalid quantifiers that cause Nginx configuration test failures
  • Overly broad patterns: Patterns like ^(.*)$ that match every URL when a more specific pattern was intended
  • Missing anchors: Patterns without ^ or $ that may match unexpected URLs
  • Catastrophic backtracking: Nested quantifiers that can cause regex engine hangs on certain inputs

Each pattern issue includes the specific regex, what it actually matches versus what was likely intended, and a corrected version.

Code Examples

Detecting and Fixing a Redirect Chain

# BEFORE: Three-hop redirect chain
server {
    listen 80;
    server_name www.example.com example.com;
    return 301 https://$host$request_uri;  # Hop 1: HTTP → HTTPS
}

server {
    listen 443 ssl;
    server_name www.example.com;
    return 301 https://example.com$request_uri;  # Hop 2: www → non-www
}

# Result: http://www.example.com/page → https://www.example.com/page → https://example.com/page

# AFTER: Single-hop redirect
server {
    listen 80;
    listen 443 ssl;
    server_name www.example.com;
    return 301 https://example.com$request_uri;  # Direct to final destination
}

server {
    listen 80;
    server_name example.com;
    return 301 https://example.com$request_uri;  # HTTP → HTTPS only
}