Nginx+php-fpm下PHP Slim应用重定向后部分请求404问题
Hey there! Let’s break down why you’re seeing those frustrating 404s even though your rewrite logs look good. I’ve tackled similar issues with Slim + Nginx setups before, so here’s what to check step by step:
1. Fix Your Rewrite Rule Flags
First, make sure you’re using the right flag for internal rewrites (not external redirects). If you’re using redirect or permanent, the browser is making a new request to the rewritten URL—but if you want Nginx to handle the rewrite internally without telling the browser, you need last instead.
Here’s the correct rule to use:
rewrite ^/apis/master/(.*)$ /apis/FreeTierSmall/master/$1 last;
The last flag tells Nginx to stop processing current rules and start matching the rewritten URI against your location blocks again. Using redirect here can lead to unexpected issues with Slim’s routing if your base path isn’t set up to handle the external jump.
2. Set the Correct Base Path in Each Slim App
Slim relies on knowing its base path to match routes correctly. If your rewritten request hits a Slim app that doesn’t have the right base path configured, it’ll return a 404 even if the file exists.
For your example app at apis/FreeTierSmall/master/exampleApp/v1, add this to your Slim bootstrap code:
$app = Slim\Factory\AppFactory::create(); // Match the full path to your app's root $app->setBasePath('/apis/FreeTierSmall/master/exampleApp/v1');
Every Slim instance needs its own setBasePath call matching its deployment path—this is a common gotcha when running multiple apps on the same server.
3. Verify Nginx Location Block Priority
Nginx matches location blocks in a specific order, and if your rewrite isn’t falling into the right block, it can cause 404s. Make sure your /apis/ location has priority over other PHP-related blocks, and that it forwards requests correctly to Slim’s index.php.
Here’s a solid setup to use:
# Match /apis/ first to handle rewrites location ^~ /apis/ { # Your rewrite rule goes here rewrite ^/apis/master/(.*)$ /apis/FreeTierSmall/master/$1 last; # Forward non-file requests to Slim's index.php try_files $uri $uri/ /apis/index.php$is_args$args; } # Handle PHP execution location ~ \.php$ { fastcgi_pass unix:/run/php/php8.2-fpm.sock; # Update to your FPM socket/path fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name; fastcgi_param REQUEST_URI $request_uri; # Critical for Slim routing include fastcgi_params; }
The ^~ flag ensures the /apis/ location takes precedence over regex locations like the .php block, so your rewrite runs before PHP processing.
4. Check Filesystem Path Alignment
Double-check that the rewritten URI maps to an actual file on your server. For example, if you’re accessing apis/master/exampleApp/v1, the rewrite should point to /var/www/html/apis/FreeTierSmall/master/exampleApp/v1/index.php (adjust your document root as needed). If that file doesn’t exist, Nginx will return a 404 regardless of rewrites.
5. Dig Into PHP-FPM Logs
Sometimes Nginx shows a 404, but the real issue is in PHP-FPM. Check your FPM error logs (usually at /var/log/php-fpm/ or /var/log/apache2/ depending on your setup) for messages like "No such file or directory" or Slim-specific routing errors. These logs will tell you if the request is reaching PHP but failing to find the right script or route.
内容的提问来源于stack exchange,提问作者Blane Townsend

