# 处理时发生重写或内部重定向循环

- **ID:** `nginx/rewrite-or-internal-redirection-cycle`
- **领域:** nginx
- **类别:** config_error
- **验证级别:** ai_generated
- **修复率:** 85%

## 根因

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

## 版本兼容性

| 版本 | 状态 | 引入 | 弃用 |
|------|------|------|------|
| 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 | — | — |

## 解决方案

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

## 无效尝试

- **** — Logging doesn't break the cycle; it only helps debug. (90% 失败率)
- **** — This prevents external access but may still cycle internally if rewrite triggers same location. (70% 失败率)
- **** — Timeouts don't affect rewrite loops; they are unrelated. (95% 失败率)
