Australian cloud for business software — built and run in Sydney since 2002

NetLeverage
NetLeverage

nginx 500: rewrite or internal redirection cycle while internally redirecting to "/index.php"

Technical article
Hosting

About this article: This article is reviewed against the cited sources. This is source research, not lab validation. Check the stated versions and your own requirements before making changes.

The visitor gets a 500 Internal Server Error and the nginx error log has a line that contains rewrite or internal redirection cycle while internally redirecting to "/index.php". nginx follows a request through up to 10 internal redirects, and this request kept being sent back to a location that sent it on again. Our own reading of nginx's try_files text quoted below: a try_files whose last parameter, /index.php, does not exist under the location's root sends the request back to the same location, and that repeats.

What the error means

nginx's documentation for the internal directive says: "There is a limit of 10 internal redirects per request to prevent request processing cycles that can occur in incorrect configurations. If this limit is reached, the error 500 (Internal Server Error) is returned. In such cases, the “rewrite or internal redirection cycle” message can be seen in the error log."

In nginx 1.26.2 the limit is the constant NGX_HTTP_MAX_URI_CHANGES, set to 10 in ngx_http_request.h. Each URI change counts down from a starting value, and when it reaches zero nginx logs at the error level and finalises the request with a 500. The text ends differently depending on how the request got there. The source has three forms:

  • while processing "...", when a rewrite directive keeps changing the URI;
  • while internally redirecting to "...", when a try_files or index or error_page sends the request to another URI, as with /index.php here;
  • while redirect to named location "...", when the target is a named location such as @wordpress.

nginx adds the client address, the server name and the request after the message. The text between the quotation marks is the URI the request was being sent to when the limit was hit.

Three directives of the documentation matter here. The page for try_files begins "Checks the existence of files in the specified order and uses the first found file for request processing" and continues: "If none of the files were found, an internal redirect to the uri specified in the last parameter is made." The redirect starts a new search for a matching location. If that search lands in the same location and the file is still missing, the same redirect happens again. The rewrite flag last "starts a search for a new location matching the changed URI". And index has the same effect: "using an index file causes an internal redirect, and the request can be processed in a different location."

Likely causes

Cause What you see How to confirm
The last parameter of try_files, such as /index.php, is not found under the location's root and no other location catches it ... while internally redirecting to "/index.php" The file named by root plus the URI does not exist
No location for .php files, so /index.php lands in the location / that sent it there The same, even though PHP is installed The server block has no location ~ \.php$ or its include is not loaded
A rewrite ... last inside a location that matches its own result ... while processing "..." nginx's page says to use break in a location, "or otherwise nginx will make 10 cycles and return the 500 error"
A named location whose own try_files points back at the first one ... while redirect to named location "@..." Read the two locations together
Wrong root, for example after a deploy that moved the site Worked before the move; fails for every URL that does not exist as a file Compare the root path with where the site files are

Prove the cause before changing anything

All of these steps only read.

1. Find the lines. The error_log directive names the file. Use the file your configuration names; the path below is the one used in the example. The GNU manual says "grep prints lines that contain a match for one or more patterns."

grep "internal redirection cycle" /var/log/nginx-error.log

A good result is one or more lines with one of the three endings above, at the times of the failing requests. Note the URI in quotation marks.

2. Dump the configuration nginx is really using. nginx's page says -T is "same as -t, but additionally dump configuration files to standard output (1.9.2)", so it prints included files too.

nginx -T

In the output, find the server block for the host and read its root, index, every location, and every try_files. Look for a location that handles \.php$.

3. Check the file the redirect points at. nginx's page says "A path to the file is constructed by merely adding a URI to the value of the root directive", so the file to look for is the root value followed by the URI. The ls(1) page describes -l as "use a long listing format".

ls -l /var/www/example.com/public/index.php

A bad result is No such file or directory. A good result lists the file, in which case the cycle comes from the location search and not the missing file; read the locations again.

4. Optional: see each change. With rewrite_log on, nginx logs rewrite results "into the error_log at the notice level", and a debugging log (built with --with-debug and set with error_log ... debug) records each URI change; the 1.26.2 source writes a debug line uri changes: %d.

Fix

The cycle is broken at the place found above. There are three shapes of fix.

  • Where the file is missing, either the root does not hold the site, which the edit below corrects, or index.php was never deployed, which is for the site's developer to put in place.
  • Where the redirect lands in the wrong location, nginx's documented WordPress and Joomla example gives .php files their own location. It has location / { try_files $uri $uri/ @wordpress; }, a separate location ~ \.php$ that begins with try_files $uri @wordpress;, and a named location @wordpress that passes to the PHP handler with /index.php as the script. That is a description of nginx's example for the site's developer, with the PHP handler address and script path of the site's own setup; it is not a step to paste.
  • Where a rewrite ... last is the cause, nginx's page says the last flag "should be replaced by break" inside a location. That edit is made with the step below: change last to break on that one line and leave the rest of the line as it is.

A root fix and a last to break fix are edits to a live server configuration, and the step below is how either is made.

First save the current state. The GNU manual says "cp copies files (or, optionally, directories)." Copy the file that holds the server block to /root/, which no include line reads, so nginx does not read the saved copy as configuration:

cp -p /etc/nginx/conf.d/example.com.conf /root/example.com.conf.2026-12-04

Risk: the next edit changes how a live site finds its files or routes its requests. A wrong root can break every page of the site, and a syntax error can stop the reload that follows. Edit the file when traffic is low, and do not reload until the syntax check passes.

Edit the file. For the case where root is wrong, the root line is changed to the directory that holds index.php:

root /var/www/example.com/current/public;

Check the syntax and file access. nginx's page describes -t as "test the configuration file: nginx checks the configuration for correct syntax, and then tries to open files referred in the configuration."

nginx -t

Disruptive step: the next command reloads nginx. nginx's guide says the master process checks the new file and, if it works, starts new worker processes and asks the old ones to shut down; if the check fails it "rolls back the changes and continues to work with the old configuration". Requests in flight are finished by the old workers.

nginx -s reload

Only if you need to back out: copy /root/example.com.conf.2026-12-04 back over example.com.conf.

Risk: the back-out command overwrites the live server file with the saved copy. It discards your edit and any other change made to the file since, so the cycle returns if the cause is still there.

cp -p /root/example.com.conf.2026-12-04 /etc/nginx/conf.d/example.com.conf

Check the restored file:

nginx -t

Disruptive step: the next command reloads nginx so that it loads the restored file. As above, the old workers finish requests in flight.

nginx -s reload

Example (illustrative): on web01, www.example.com has root /var/www/example.com/public;, index index.php;, and one location / { try_files $uri $uri/ /index.php?$args; } with no .php location. A deploy has moved the site to /var/www/example.com/current/public. A request for /hello finds no file, try_files redirects to /index.php, ls -l /var/www/example.com/public/index.php says the file does not exist, and the request comes back to location / ten times. The log shows while internally redirecting to "/index.php". The administrator saves the file, changes root to the new path, runs nginx -t, then nginx -s reload.

Confirm the fix

  1. Request the URL that failed, with curl -I https://www.example.com/hello (curl's manual says -I, --head will "Fetch the headers only."), and expect the status the application gives, not 500.
  2. Look at the end of the error log: no new internal redirection cycle line should appear after the reload.
  3. Request /index.php and a static file that exists, such as an image, and check that each returns what it did before.

What to watch afterwards

  • After every deploy that moves a site, run nginx -T and compare the root with the real path before reloading.
  • Count internal redirection cycle lines per day in the error log; a rise after a configuration change points straight at it.
  • Our own advice: whenever try_files ends in a URI, read where that URI will land next and make sure a different location takes it.

Risks and limits

Sources