If you opt to set up your own WordPress server and happen to choose Nginx as your web server, you will bump into this classic problem the moment you change the WordPress link to anything other than plain.

Luckily the solution is very simple, and you need only one line of change in your website’s Nginx configuration file.

1. Solution

Use your favorite editor to open your website’s Nginx server. The path is very likely to be the following if you use yum/opt-get to install it by default. If you do not have access to this file, you’ll need to contact your Linux server administrator to make a change for you.

 vim /etc/nginx/sites-available/yourdomain.conf

Find the following line. This is the line that gave you the HTTP 404 error.

location / {
         try_files $uri $uri/ =404;
}

Change this line to the following.

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

In order to enable the new configuration, you’ll need to restart the Nginx server.

sudo systemctl restart nginx

2. Reasons behind a few Whys

If you have opted to deploy WordPress along with Nginx, you might be interested in the reasons behind it.

If you take a close look at the difference between a plain link and other structures, the plain link comes with a ‘?’ symbol. The question mark ‘?’ tells the webserver whatever comes after it is a parameter. The Nginx can understand it, and treat parameter p with a value of 123, and redirect the request to index.php for further processing.

The Permalink Structure in WordPress backend

For other permalink types, their structure looks like a directory. Nginx knows it’s a directory, and the location block for / (web root directory) tries the first two directories ($URI and $URI/) without any success. So it comes to the 404 redirection and outputs the 404 page.

Unlike Nginx, Apache servers support .htaccess files (or “distributed configuration files“). It provides a way to make configuration changes on a per-directory basis. When we installed WordPress, the WordPress software detected that your web server was Apache and generated a.htaccess file under its own directory. So the WordPress software has full authority of .htaccess, and writes the necessary configuration inside it to let Apache knows what comes after is not a directory, but parameters for index.php.

The Nginx server chooses not to support such distributed configuration for various reasons. It limits the configuration to Nginx itself, not the web application. As a result, the WordPress software won’t be able to do anything when non-plain permalinks are configured. Thus, you or your Linux administrator need to manually change it.

2.3 What did we just change?

What we added in the Nginx configuration file, effectively tells Nginx that you may try the directories first, but if they do not work, continue to try to make $URI a parameter and send it over to index.php. Voilà! Everything starts work.

3. Further readings

If you would like to learn more, I recommend you continue to read Create Nginx Rewrite Rule on Nginx official blog and official WordPress Nginx document.

If you have more questions, welcome to leave me a comment below.

Leave a Reply

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