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

If you read the official nginx guide 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. Let’s say you have site on root domain and a wordpress on subdirectory. Eg:

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

Then you will need the following config to make the wordpress works as per Nginx official guide.

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. Check the below screenshot. Nginx using /blog location block where the requested URL is for root. This is plain 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, it’s gonna work as they way it should.

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

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

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