Fix WordPress 404 Errors on Nginx for All Pages but Homepage
In this article
If your WordPress site loads the homepage but every other page returns a 404 error, the problem is almost always a missing or misconfigured Nginx server block. This guide explains how to fix that by adding the correct WordPress rewrite rules to your Nginx configuration, so posts, pages, and custom post types all work again.
What You'll Need
- SSH access to your server (or a hosting control panel that lets you edit Nginx config files).
- Root or sudo privileges to modify Nginx configuration files.
- Your WordPress installation's document root path (e.g.,
/var/www/htmlor/usr/share/nginx/html). - A text editor on the server (like nano or vim) or a way to edit files via SFTP.
Step-by-Step
- Identify your Nginx configuration file. Common locations are
/etc/nginx/sites-available/your-siteor/etc/nginx/conf.d/default.conf. If you're using a control panel like Plesk or cPanel, find the config through its interface. - Open the configuration file with your preferred editor, for example:
sudo nano /etc/nginx/sites-available/your-site. - Locate the
serverblock that handles your domain. It should contain lines likelisten 80;andserver_name yourdomain.com;. - Find the
location /block inside that server block. It might look like:location / { try_files $uri $uri/ =404; } - Replace the
try_filesline with the standard WordPress rewrite rule:try_files $uri $uri/ /index.php?$args; - Add a PHP location block if it doesn't already exist. Place it inside the same server block, often right after the
location /block:location ~ \.php$ { include snippets/fastcgi-php.conf; fastcgi_pass unix:/var/run/php/php8.1-fpm.sock; }(Adjust the socket path to match your PHP version and setup.) - Check for any other location blocks that might interfere, like
location ~ \.php$that already exists but has a differenttry_filesorfastcgi_pass. Ensure they are correct. - Test the Nginx configuration before reloading:
sudo nginx -t. This will tell you if there are syntax errors. - Reload Nginx to apply changes:
sudo systemctl reload nginx(orsudo service nginx reloadon older systems). - Clear any caching plugins (like WP Super Cache or W3 Total Cache) and browser cache, then test a few pages (e.g., a post, a page, and a category archive) to confirm they now return 200 status.
Common Mistakes
- Using the wrong PHP socket path. If the socket path doesn't match your PHP-FPM setup, you'll get a 502 Bad Gateway instead of 404. Check with
php -i | grep 'listen'or look in/etc/php/*/fpm/pool.d/www.conf. - Forgetting to include
index index.php;in the server block. Without it, Nginx might not serve PHP files as the index. - Having a separate
locationfor static files that overrides the main one. Ensure yourlocation /block is not being shadowed by a more specific regex location. - Not testing Nginx config before reloading. A typo can take the whole site down.
- Editing the wrong config file. If you have multiple server blocks, you might be editing one that isn't being used.
Tips
- Always back up your Nginx config file before making changes:
sudo cp /etc/nginx/sites-available/your-site /etc/nginx/sites-available/your-site.bak. - Use
nginx -Tto dump the entire effective configuration and verify which server block is being applied for your domain. - If you're using a security plugin like Wordfence, temporarily disable it while testing, as it can sometimes interfere with rewrite rules.
- Keep your Nginx and PHP versions up to date to avoid compatibility issues.
- If you have a WordPress multisite, the rewrite rule is slightly different:
try_files $uri $uri/ /index.php?$args;still works, but you may need to addrewrite ^/wp-admin$ $scheme://$host$uri/ permanent;for subdirectory multisite.
Troubleshooting
If the fix doesn't work, try these steps:
- Check Nginx error logs:
sudo tail -f /var/log/nginx/error.logand access logs to see what URL is being requested and what status is returned. - Verify PHP-FPM is running:
sudo systemctl status php8.1-fpm(replace with your version). If it's not running, start it. - Test with a simple PHP file: Create a file
info.phpin your WordPress root with<?php phpinfo(); ?>and access it. If that works, the issue is specifically with WordPress rewrites. - Check WordPress permalink settings: Go to Settings > Permalinks in wp-admin and click "Save Changes" to flush rewrite rules. This can help if the .htaccess (though not used on Nginx) or the database rewrite cache is stale.
- Ensure the
fastcgi_param SCRIPT_FILENAMEis set correctly. In some configs, you needfastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;inside the PHP location block. - If you're using a reverse proxy or CDN, make sure it's not caching 404 responses. Purge the CDN cache and test directly with a curl command:
curl -I http://yourdomain.com/sample-page/.
Frequently Asked Questions
Why does only the homepage work?
Because the homepage is served by the location / block's default behavior (it finds the index.php), but other URLs need the rewrite rule to pass the request to WordPress's index.php for routing.
Do I need to change anything in WordPress itself?
Usually not, but you can go to Settings > Permalinks and click "Save Changes" to flush the rewrite cache. This is a good first step after fixing Nginx.
What if I'm using a hosting panel like cPanel or Plesk?
Most panels have a way to edit the Nginx config directly. In Plesk, go to Domains > your domain > Apache & nginx Settings. In cPanel, use the "MultiPHP Manager" or edit the vhost file via the "Include Editor". The same rewrite rule applies.
Can this break my site?
If you make a syntax error, Nginx will fail to reload, but you can revert to your backup. Always test with nginx -t first. The rewrite rule itself is standard and safe.
What if I get a 404 for the wp-admin area too?
That's less common but can happen if the PHP location block is misconfigured. Ensure the location ~ \.php$ block exists and has the correct fastcgi_pass. Also check that try_files in that block is not set to =404.
Is there a difference for WordPress multisite?
Yes, for subdirectory multisite you might need an extra rewrite rule for /wp-admin to redirect to the correct path. The main try_files rule remains the same. For subdomain multisite, the same rule works per site.