Nginx make sites faster. It definitely has an upper edge in serving static content. That’s why, many WordPress users switch to Nginx.
Often, this switch cause Nginx WordPress permalinks 404 errors. This happens as Apache’s rewrite rules do not work with NGINX.
Therefore, fixing WordPress websites is one of the common task that we do as part of our Server Administration Services.
Today, we’ll see how Bobcares’ Engineers fixed Nginx WordPress permalinks 404 error for one of our customer.
Details of WordPress permalinks 404 error
We’ll begin with seeing the details of the error reported by the customer. The Help-desk request said:
In my domain, only the home page is working, where as every other page returns a 404 not found error. I
think this is either due to .htaccess file, permalink settings, or Nginx Configuration? Please fix!
The customer had website hosted on a Plesk server. He recently switched to Nginx from Apache. That’s when he started getting 404 or Page not found errors.
What causes permalinks 404 error in Nginx?
WordPress come with the option to create permanent links to blog posts on the website. And, the links can be created based on post name, post number, date, etc.
Now, its time to see what causes the Nginx wordpress permalinks 404 error.
Basically, customer choose PHP-FPM as the PHP handler, as it is the handler that WordPress recommends. And, to take advantage of NGINX, we recommend customers to enable PHP-FPM on all domains that serve PHP content.
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 totally different format. The directives in Apache like Rewritecond, RewriteRule, etc. take its equivalents in Nginx configuration.
This means that WordPress permalink rewrites will no longer work after switching the web server.
Steps for fixing Nginx wordpress permalinks 404 error
Luckily, there are several methods to fix the WordPress permalinks on Nginx. Let’s take a look at each of them.
1. Converting rewrite rules to Nginx format
A quicker method to fix the permalink error is to convert the Apache rewrite rules to Nginx format. Either we can manually rewrite the rules or use various plugins for automatic conversion.
For example, in servers with Plesk control panel, there comes an extension like “htaccess to nginx“. By using this extension, it would rewrite the custom Apache directives according to the rules of nginx syntax. The plugin details are as shown below.
We just had to add the rules in the Additional Nginx directives section inside the domain settings as shown below.
That fixed the permalinks and the website started working again.
2. Modifying Nginx configuration
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, our Support Engineers edit the following location block within the server block:
location / {
try_files $uri $uri/ /index.php?$args;
}
Further, we reload the Nginx configuration for the changes to take effect.
3. Tweaking Proxy mode
Similarly in Plesk servers having Apache with Nginx as reverse proxy, tweaking domain’s setting also solve the permalink errors. The .htaccess
is ignored if Apache is turned off for the domain. And, it results in Permalink 404 errors.
Therefore, in some cases, to solve the error, we turn ON the option Proxy mode from Plesk > Domains > example.com > Apache & nginx Settings.
[Finding Nginx WordPress permalinks 404 error trouble? We can fix your website.]
Conclusion
To be precise, Nginx wordpress permalinks 404 error occurs when the rewrite rules do not follow the Nginx format. The fix involves converting the rules, modifying Nginx configuration and many more. Today, we saw how our Support Engineers solved the error for our customer and made permalinks working again.
Thanks it helped me, Actually my file location was
location / {
try_files $uri $uri/ 404;
}
Glad to know that our post was helpful for you 🙂
I had the same problem and this helped me out, thanks!
Hello,
We’re glad you were able to solve the issue by following the instructions in this article.
MAAAAAN, thanks for your pot, it’s 2022 and this post keep helping people
Hello Thiago,
We’re glad you were able to solve the issue by following the instructions in this article.
just replace this from nginx’s default line.
try_files $uri $uri/ /index.php$is_args$args;