Certainly! This is a common issue when switching to PHP-FPM with NGINX, especially when using a custom PHP router script (like for a simple PHP app or a micro-framework).
Problem Explanation:
- By default, NGINX does not route all requests to your
index.php(or router script). Instead, it tries to serve static files directly, and if the file does not exist, it returns a 404. - On Apache,
.htaccesshandles this for you. On NGINX, you must configure this in your server block.
Solution:
You need to update your NGINX server block configuration to pass all requests to your PHP router script (usually index.php) if the requested file does not exist.
Example NGINX Configuration:
Open your site’s NGINX config file (commonly found in /etc/nginx/sites-available/your-site.com).
Look for the location / block and update it as follows:
location / {
try_files $uri $uri/ /index.php?$query_string;
}
This tells NGINX:
- Try to serve the file as is (
$uri) - If not found, try as a directory (
$uri/) - If still not found, pass the request to
index.php(your router script), preserving the query string.
Make sure you also have a PHP block like this:
location ~ \.php$ {
include snippets/fastcgi-php.conf;
fastcgi_pass unix:/var/run/php/php8.3-fpm.sock;
}
After editing:
- Save the file.
- Test your NGINX config:
sudo nginx -t - Reload NGINX:
sudo systemctl reload nginx
Summary:
- The 404 is because NGINX is not passing non-existent files to your router script.
- The
try_filesdirective fixes this.
Let me know if you need help with the exact file path or further details!