How do I configure nginx for WordPress REST API in sub-folder?

admin

Administrator
Staff member
I am trying to set up multiple Wordpress sites in sub-folders under our domain (ie not multi-site), but I have difficulty configuring the REST API endpoints. For example, this endpoint works fine:

Code:
https://example.com/site1/?rest_route=/wp/v2/posts

But this endpoint gives a 404:

Code:
https://example.com/site1/wp-json/wp/v2/posts

I have tried to rewrite the failing url to the working url with these rules in my nginx configuration:

Code:
location /site1/wp-json {
    rewrite ^/site1/wp-json(.*)$ /site1/?rest_route=$1;
}

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

I can't see any special handling of wp-json in the <a href="https://wordpress.org/support/article/nginx/" rel="nofollow noreferrer">WordPress docs</a> or the <a href="https://www.nginx.com/resources/wiki/start/topics/recipes/wordpress/" rel="nofollow noreferrer">nginx wiki</a>. What am I missing here? The permalinks for the site is set to
Code:
Numeric
(<a href="https://example.com/site1/archives/123" rel="nofollow noreferrer">https://example.com/site1/archives/123</a>) if that might play a role.

<h2>Update</h2>

Gist of the redacted <a href="https://gist.github.com/JannieT/171c88171c01c6c8d17932a1027692d7" rel="nofollow noreferrer">full config file</a> and the config syntax lints okay:

Code:
nginx -c /etc/nginx/nginx.conf -t

<blockquote>
nginx: the configuration file /etc/nginx/nginx.conf syntax is ok<br>
nginx: configuration file /etc/nginx/nginx.conf test is successful
</blockquote>