How do I get the Wordpress JSON-API to work on Nginx server?

16.6k views Asked by At

For some reason "out-of-the-box" the Wordpress JSON API does not work on Nginx. I have tried several redirect schemes in the nginx conf. The only thing I have gotten to work is ?json. However, this does not work for authentication and registration.

As an FYI, I am developing a cordova application and attempting to use the WP JSON API for WP backend.

6

There are 6 answers

3
etrey On BEST ANSWER

I found the solution to the problem. Make sure that permalinks are working properly before you assume (like I did) that it is an issue with the plugin.

I was able to correct permalinks for a wordpress site in a subdirectory on an nginx site. This article will help you if you face the same issue here

0
Sanjaya Regmi On

Adding the following on my Nginx server worked like charm.

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

After adding check the Nginx configuration with

$ sudo nginx -t

Then if everything is okay, reload the server

$ sudo systemctl reload nginx.
0
MD Sadique Imam On

If you have installed Wordpress in subdirectory, below solution will work:

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

Now restart Nginx server

sudo systemctl restart nginx
0
Dylan B On

Assuming your Wordpress is installed on root domain, it should be like this:

#fix Woocommerce New Customer's activation link (embeded in welcome email)
rewrite ^/wp-json(.*)$ /index.php?rest_route=$1 permanent;

location / {
     #Forward all request to index.php
     try_files $uri $uri/ /index.php$is_args$args;
}

Note that the rewrite rule must be outside of the

location {}

and above the index redirection

6
northtree On

My Nginx conf for wp-json API.

location / {
    # http://v2.wp-api.org/guide/problems/#query-parameters-are-ignored
    try_files $uri $uri/ /index.php$is_args$args;
}

location ~ ^/wp-json/ {
    # if permalinks not enabled
    rewrite ^/wp-json/(.*?)$ /?rest_route=/$1 last;
}
1
zdd On

My case was to deploy wordpress on a sub directory under the root dir of my website, here is the step to make it works

  1. The website root dir is /www/wwwroot/www.abc.com/public/ - access via www.abc.com
  2. The wordpress dir is /www/wwwroot/www.abc.com/public/blog/ - access via www.abc.com/blog/
  3. Download wordpress and unzip it to you web root dir, then rename it to blog
  4. chmod -R www:www blog - run this command in your web root dir.
  5. Add the following lines to your nginx config file.
location /blog/ {
  index index.php;
  try_files $uri $uri/ /blog/index.php?$args;
}