FIXING WORDPRESS PERMALINK 404 ERRORS ON NGINX

If you use Nginx as your web server for WordPress, you may encounter frustrating 404 errors after changing your permalink settings. This is caused by Nginx not properly handling the rewritten URLs.

The solution is adding the right Nginx configuration for processing PHP and passing requests to WordPress’ index.php front controller file.

The Problem

WordPress uses rewrite rules to allow “pretty permalinks” instead of ugly query strings. For example, instead of example.com/?p=123 you could have example.com/sample-post/.

However, Nginx requires explicit rules for rerouting these rewritten URLs to PHP-FPM and index.php. Otherwise, it will fail with 404 errors as it tries to find files matching the pretty permalinks.

The Solution

To fix the 404 errors, you need to add a try_files directive and location block for PHP:

location / {
  try_files $uri $uri/ /index.php$is_args$args; 
}

location ~ \.php$ {
  try_files $uri =404;
  fastcgi_pass unix:/var/run/php-fpm/php-fpm.sock;
  fastcgi_index index.php;
  fastcgi_buffers 16 16k; 
  fastcgi_buffer_size 32k;
  fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
  # Other fastcgi params
}

This tells Nginx to first try finding real files matching the request, then fallback to passing it to index.php for WordPress to handle the rewrite rules.

The location block passes all .php requests to PHP-FPM for processing.

With these simple additions, your pretty permalinks will work perfectly!

Additional Tips

  • Flush permalinks and test after adding rules
  • Increase php-fpm child processes if high traffic
  • Make sure mod_rewrite is enabled
  • Try a default theme and plugins to isolate issues