nginx config_error ai_generated true

rewrite or internal redirection cycle while processing

ID: nginx/rewrite-or-internal-redirection-cycle

Also available as: JSON · Markdown · 中文
85%Fix Rate
84%Confidence
1Evidence
2023-03-22First Seen

Version Compatibility

VersionStatusIntroducedDeprecatedNotes
nginx 1.18.0 active
nginx 1.20.2 active
nginx 1.22.1 active
nginx 1.24.0 active
nginx 1.25.3 active

Root Cause

A rewrite rule or try_files directive causes an infinite loop because the rewritten URI matches the same location block repeatedly.

generic

中文

重写规则或try_files指令导致无限循环,因为重写后的URI反复匹配同一个location块。

Official Documentation

http://nginx.org/en/docs/http/ngx_http_rewrite_module.html#rewrite

Workarounds

  1. 85% success Add a break flag to the rewrite rule: rewrite ^/old/(.*)$ /new/$1 break;
    Add a break flag to the rewrite rule: rewrite ^/old/(.*)$ /new/$1 break;
  2. 90% success Use a separate location for the target URI to avoid recursion: location /new/ { ... }
    Use a separate location for the target URI to avoid recursion: location /new/ { ... }
  3. 80% success Replace rewrite with try_files and a named location: try_files $uri @fallback; location @fallback { ... }
    Replace rewrite with try_files and a named location: try_files $uri @fallback; location @fallback { ... }

中文步骤

  1. 在重写规则中添加break标志:rewrite ^/old/(.*)$ /new/$1 break;
  2. 为目标URI使用单独的location以避免递归:location /new/ { ... }
  3. 用try_files和命名location替换重写:try_files $uri @fallback; location @fallback { ... }

Dead Ends

Common approaches that don't work:

  1. 90% fail

    Logging doesn't break the cycle; it only helps debug.

  2. 70% fail

    This prevents external access but may still cycle internally if rewrite triggers same location.

  3. 95% fail

    Timeouts don't affect rewrite loops; they are unrelated.