How to fix the Nginx 404 not found error when WordPress is not installed in the root directory

Sometimes you need to install and access WordPress in a subdirectory instead of the root directory (where your main site is served with Nginx).

If you get the Nginx 404 not found error when accessing WordPress, add the following code in the virtual host file (eg: /etc/nginx/sites-available/mysite) to solve the problem.

NOTE: Replace yoursubdirectory with the name of the subdirectory where WordPress is installed.

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