Smart Systems for Home

Plain how-to guides for smart home, security and home Wi-Fi

Fix WordPress 404 Errors on Nginx for All Pages but Homepage

· 5 min read · Home Tech

In this article
  1. What You'll Need
  2. Step-by-Step
  3. Common Mistakes
  4. Tips
  5. Troubleshooting
  6. Frequently Asked Questions

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/html or /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

  1. Identify your Nginx configuration file. Common locations are /etc/nginx/sites-available/your-site or /etc/nginx/conf.d/default.conf. If you're using a control panel like Plesk or cPanel, find the config through its interface.
  2. Open the configuration file with your preferred editor, for example: sudo nano /etc/nginx/sites-available/your-site.
  3. Locate the server block that handles your domain. It should contain lines like listen 80; and server_name yourdomain.com;.
  4. Find the location / block inside that server block. It might look like:
    location / { try_files $uri $uri/ =404; }
  5. Replace the try_files line with the standard WordPress rewrite rule:
    try_files $uri $uri/ /index.php?$args;
  6. 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.)
  7. Check for any other location blocks that might interfere, like location ~ \.php$ that already exists but has a different try_files or fastcgi_pass. Ensure they are correct.
  8. Test the Nginx configuration before reloading: sudo nginx -t. This will tell you if there are syntax errors.
  9. Reload Nginx to apply changes: sudo systemctl reload nginx (or sudo service nginx reload on older systems).
  10. 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 location for static files that overrides the main one. Ensure your location / 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 -T to 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 add rewrite ^/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.log and 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.php in 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_FILENAME is set correctly. In some configs, you need fastcgi_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.