Don’t follow official Nginx guidelines to serve WordPress in a sub-directory

If you see the official Nginx guidelines to serve WordPress as a sub directory then you will see that they will tell you to use the following location block:

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

DON’T use it….!!! 🤢 Let me tell you why.

For example, let’s say you have site on root domain and a wordpress setup on subdirectory. Eg:

https://example.com              (static html)
https://example.com/blog/    (wordpress setup)

Then you will need the following config to make the wordpress setup works as per Nginx official guidelines:

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

Now let’s say you have page on root domain named:

https://example.com/blog-foo-page/

As you can see, the page is located on root domain, NOT in the WordPress sub directory. But our good Nginx will process the request via /blog, NOT via root and you will probably gonna end up with 404 or other errors.

You can see it using the Nginx location match tester too. Check the below screenshot Nginx using /blog location block where the requested URL is for root. This is wrong.


Now, if I use this config that fixes the issue.

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

Notice that I changed /blog to /blog/, Now if we test it gonna work as they way it should.

So, you should use /blog/ instead of /blog.

Note: I’m NOT Nginx guru but a very noob 😐 If anyone can explain why this happening they’re welcome to comment.

Also this is not limited to WordPress but for Laravel or any other PHP apps which uses index.php as their handler.

Share your thoughts by leaving a comment below.