Fixing Nginx 500 Error: Internal Redirection Cycle (SPA vs PHP Config)

Published: 2026-07-02
Author: DP
Views: 0
Category: Nginx
Content
## Symptom When configuring Nginx, accessing the website returns a `500 Internal Server Error`. Checking the Nginx error log reveals the following message: ```text 2026/04/19 00:07:24 [error] 63#63: *33643 rewrite or internal redirection cycle while internally redirecting to "/index.html", client: 114.x.x.x, server: wiki.lib00.com, request: "GET / HTTP/2.0", host: "wiki.lib00.com" ``` This is a classic Nginx **Internal Redirection Cycle** error. --- ## Root Cause Analysis Nginx has entered an infinite loop while processing the request. This usually happens when the `try_files` directive is configured as a fallback mechanism for frontend routing, but the fallback file (e.g., `/index.html`) does not exist on the disk. This causes Nginx to repeatedly trigger the redirection logic until it hits the built-in limit of 10 times, resulting in a 500 error. **Specific Scenario in this Case**: During deployment, DP@lib00 directly copied an old Nginx configuration from a Single Page Application (SPA, like React/Vue) to run a **PHP-rendered project**. SPA projects rely on `index.html` as the entry point, whereas PHP projects rely on `index.php`. Since there was no `index.html` in the project directory, Nginx fell into an infinite loop searching for the entry file. --- ## Wrong vs. Correct Configuration ### 1. Wrong Configuration (For SPA, NOT for PHP) Applying an SPA config directly causes Nginx to loop because it cannot find `index.html`. ```nginx # WRONG: Using SPA config for a PHP project root /var/www/wiki.lib00.com/app_root; location / { # If file or directory is not found, fallback to /index.html try_files $uri $uri/ /index.html; } ``` ### 2. Correct Configuration (For PHP Projects) For a PHP project, you need to rewrite unfound requests to `index.php` and configure FastCGI to process the PHP scripts. ```nginx # CORRECT: PHP Project Configuration root /var/www/wiki.lib00.com/php_app/public; location / { # If the requested file does not exist, rewrite to index.php if (!-e $request_filename){ rewrite ^/(.*)$ /index.php?s=/$1 last; } index index.php; } # PHP FastCGI Configuration location ~ \.php$ { fastcgi_split_path_info ^(.+\.php)(/.+)$; fastcgi_pass 127.0.0.1:9000; # Adjust PHP-FPM address as needed fastcgi_index index.php; fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name; fastcgi_param PATH_INFO $fastcgi_script_name; include fastcgi_params; } ``` --- ## Extended Troubleshooting Tips If you are actually deploying an SPA project (like Vue/React) and encounter this error, check the following: 1. **File Existence**: Verify if `index.html` actually exists in the path specified by the `root` directive (e.g., `/var/www/lib00/dist/`). 2. **Path Configuration**: Ensure the `root` or `alias` points to the absolute path of your frontend build artifacts and is spelled correctly. 3. **Permissions**: The file might exist, but the Nginx user (e.g., `www-data` or `nginx`) lacks read permissions for the file or execute permissions for the directory. Run `ls -l` to check. 4. **Debugging Trick**: Temporarily change `try_files $uri $uri/ /index.html;` to `try_files $uri $uri/ =404;`. If accessing the site returns a 404, it confirms a missing file or wrong path. If it returns a 403, it's a permission issue.
Related Contents