Skip to content

How to Fix Nginx Rewrite Rule Error

DodaTech Updated 2026-06-24 2 min read

In this tutorial, you'll learn about How to Fix Nginx Rewrite Rule Error. We cover key concepts, practical examples, and best practices.

Nginx rewrite rules cause infinite redirect loops or do not match as expected — the rewrite regex captures the wrong parts or the flag (last, break, redirect, permanent) is inappropriate for the use case.

The Problem

# Wrong: Causes redirect loop
rewrite ^/old-page$ /new-page permanent;

Without checking if the request is already for /new-page, this creates an infinite loop when subsequent rules process the new URI.

Step-by-Step Fix

Step 1: Use the correct rewrite syntax

# Simple redirect with redirect flag
rewrite ^/old-page$ /new-page redirect;

# Permanent redirect
rewrite ^/old-page$ /new-page permanent;

# Internal rewrite (no redirect to client)
rewrite ^/old-page$ /new-page last;

Step 2: Add condition to prevent loops

if ($request_uri !~ ^/new-page) {
    rewrite ^/old-page$ /new-page permanent;
}

Step 3: Use return instead of rewrite for simple cases

# Better: Use return for simple redirects
location = /old-page {
    return 301 /new-page;
}

# Regex-based redirect with return
location ~ ^/products/(.*)$ {
    return 301 /shop/$1;
}

Step 4: Test rewrite rules

curl -I http://localhost/old-page
# Check Location header in response

Expected:

HTTP/1.1 301 Moved Permanently
Location: http://localhost/new-page

Step 5: Debug with logs

rewrite_log on;
error_log /var/log/nginx/rewrite.log notice;

Prevention Tips

  • Prefer return over rewrite for simple redirects
  • Use break flag to stop further rewrite processing
  • Always test rewrites with curl -I to check headers
  • Enable rewrite_log on during development

Common Mistakes with rewrite rule

  1. Placing the wildcard pattern first in case expressions, making all subsequent patterns unreachable
  2. Using head and tail instead of pattern matching, causing runtime errors on empty lists
  3. Forgetting that lazy evaluation defers computation until the value is forced, causing space leaks with unevaluated thunks

These mistakes appear frequently in real-world NGINX code. DodaTech's contributors have identified these patterns through analysis of open-source projects and production systems.

Practice Exercise

Write a pure function that safely divides two integers using Maybe, then test it with edge cases like division by zero and negative numbers.

This exercise reinforces the concepts covered in this guide. Try implementing it before checking online solutions.

FAQ

### What is the difference between the last and break flags in nginx rewrite?

last stops processing the current location block and starts a new location search with the rewritten URI. break stops processing in the current location block but does not start a new location search. Use last for rewrites that should trigger a new location match.

Why does my nginx rewrite create an infinite redirect loop?

The rewrite is being applied to the new URI as well. For example, rewriting /old to /new and then /new also matches the rewrite rule. Add a condition to check the original URI or use the break flag to prevent reprocessing.

Should I use rewrite or return for redirects?

Use return 301/302 for simple path-to-path redirects. Use rewrite only when you need regex capture groups to transform URLs. return is faster, simpler, and less error-prone than rewrite.

Built by the developers of DodaTech

Doda Browser, DodaZIP & Durga Antivirus Pro