How-to-Fix-WordPress-Pages-and-Permalinks-404-Not-Found-Error-on-Nginx-Server
| | |

How to Fix WordPress Pages and Permalinks 404 Not Found Error on Nginx Server

Tested with

  • Nginx 1.19.0
  • WordPress 5.4
  • PHP 7.2.24
  • Vesta CP

After switched to Nginx from Apache server WordPress started showing 404 or Page not found errors. Homepage loads but all permalinks are 404 when using Nginx & PHP-FPM

What causes the Nginx wordpress permalinks 404 error

WordPress recommends PHP-FPM as the PHP handler to take advantage of NGINX. Unfortunately, mod_security rules will not work with NGINX, and .htaccess rewrite rules and restrictions will no longer apply. Nginx and Apache rewrite rules have a totally different format. This means that WordPress permalink rewrites will no longer work after switching the webserver.

How do we fix an Nginx WordPress permalinks 404 error

Similarly, modifying the Nginx configuration also helps in solving the WordPress permalinks 404 error. Here, within the per domain Nginx configuration file for the domain. Edit both nginx.conf and nginx.ssl.conf files to make it work on http:// and https://

  • /home/admin/conf/web/domain-name.nginx.conf
  • /home/admin/conf/web/domain-name.nginx.ssl.conf

edit the following location block within the server block:

cd /home/admin/conf/web/
vim domain-name.nginx.ssl.conf
// vim domain-name.nginx.conf
// press i to enter vim insert mode

//add try_files $uri $uri/ /index.php?$args; under location / {} block
location / {
    # file ($uri) or directory ($uri/)? if not, redirect to /index.php + query string
    try_files $uri $uri/ /index.php?$args;
    index  index.html index.htm index.php;
}

// press ESC to exit from vim insert mode
:wq // save and quit configuration file

For more Vim editor command read this post Basic Vim Commands to start work with Vim Editor

Here is another solution, you can try this if the previous one is not working for you. I have tested both on my Linode server and both worked perfectly.

location / {
    rewrite ^/(.*)$ /index.php?$1;
    index  index.html index.htm index.php;
}

Further, we reload the Nginx configuration for the changes to take effect.

$ nginx -s reload

Similar Posts

Leave a Reply

Your email address will not be published. Required fields are marked *

4 Comments