Nginx Redirect Analyzer

Analyze Nginx redirect chains, detect loops, validate rewrite rules and regex patterns

Enter Nginx configuration containing redirect rules (return, rewrite) to analyze

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
}

Frequently Asked Questions

What does the Nginx Redirect Analyzer check?

The Nginx Redirect Analyzer inspects your Nginx configuration for redirect-related issues including redirect chains (A → B → C), redirect loops (A → B → A), invalid rewrite regex patterns, missing termination flags, mixed protocol redirects (HTTPS downgrade to HTTP), overly broad regex patterns, and incorrect return codes. It calculates a quality score and provides specific recommendations for each issue found.

What is a redirect chain and why is it bad?

A redirect chain occurs when a URL redirects to another URL, which in turn redirects to yet another URL (e.g., /old → /middle → /new). Each hop adds latency (typically 100-300ms per redirect) and wastes crawl budget for SEO. Browsers have a maximum redirect limit (usually 20 hops) after which they stop following and show an error. The fix is to point all redirects directly to the final destination.

How do redirect loops happen in Nginx?

Redirect loops occur when two or more rules create a circular redirect path. For example, one rule redirects /page-a to /page-b, and another redirects /page-b back to /page-a. This can also happen with a single overly broad rewrite rule that matches its own target URL. Browsers display ERR_TOO_MANY_REDIRECTS when they detect a loop.

What rewrite flags should I use?

Nginx rewrite supports four flags: 'permanent' (301 redirect, cached by browsers), 'redirect' (302 temporary redirect), 'last' (restarts request processing with new URI, use in server context), and 'break' (stops processing further rewrites, use in location context). Missing a flag can cause the rewrite to continue processing subsequent rules unexpectedly.

When should I use 'return' vs 'rewrite' for redirects?

Use 'return' for simple redirects where you know the exact target URL — it is faster because Nginx does not need to evaluate a regex. Use 'rewrite' only when you need regex pattern matching to transform the URL dynamically (e.g., capturing path segments). For blanket domain or protocol redirects, 'return 301 https://$host$request_uri;' is preferred.

Why is redirecting to HTTP flagged as an issue?

Redirecting from HTTPS to HTTP (protocol downgrade) strips TLS encryption from the connection, exposing user data to interception. This violates HSTS policies if configured, and browsers increasingly warn users about insecure connections. Always redirect to HTTPS targets unless you have a specific reason for plain HTTP (e.g., local development).

Is my Nginx configuration sent to a server?

No. All analysis happens entirely in your browser using JavaScript. Your Nginx configuration — which may contain internal server names, IP addresses, and infrastructure details — is never transmitted to any server. No data is stored, logged, or shared.

What regex issues are detected in rewrite rules?

The analyzer detects: invalid regex syntax that would prevent Nginx from starting, unclosed character classes or capture groups, missing end anchors ($) that cause broader matching than intended, unescaped dots that match any character instead of literal periods, and overly broad patterns (like ^(.*)$) that catch all requests indiscriminately.

What is the difference between this tool and the Nginx Security Inspector?

The Nginx Redirect Analyzer focuses specifically on redirect/rewrite rules — chains, loops, regex validation, and redirect best practices. The Nginx Security Inspector analyzes broader security configuration including SSL/TLS, security headers, rate limiting, sensitive paths, and access controls. Use both for a comprehensive Nginx configuration audit.