13

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.

Dawson Loudon
  • 6,029
  • 2
  • 27
  • 31
etrey
  • 447
  • 3
  • 6
  • 15

6 Answers6

42

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;
}
northtree
  • 8,569
  • 11
  • 61
  • 80
  • Strangely, this solution seems to be required in certain environments even when permalinks are properly configured. I'm not sure, perhaps due to issues with plugin or theme code, or resource behavior in the browser itself... https://wordpress.org/support/topic/contact-form-7-wp-json-404-error/#post-11427175 – Jesse Nickles Jan 13 '20 at 17:46
  • 1
    I needed to add this in my nginx.conf for my wordpress website running in Amazon Linux 2 (AMI) – Melvin Sy May 31 '20 at 13:36
  • Still needed in at least my Nginx configuration. Now the search function for adding a link in Gutenberg is working again, which had brought up only the latest posts no matter what I typed in. Thanks for this. – physalis Oct 28 '20 at 12:05
  • Note, it's important that this wp-json rewrite rule is **not** present if you have permalinks configured correctly. Otherwise it will break with 405 errors. – CR. Feb 28 '22 at 06:02
  • Thanks you are a life saver, spend two days looking at the .conf file copied your code to my file and it worked right away. – OCP30pt1c1l1l43-X1z17 Jul 20 '22 at 11:45
5

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

etrey
  • 447
  • 3
  • 6
  • 15
  • 2
    You never suspect the simplest thing, thanks; saved me a lot of time! – Sammaye Dec 31 '15 at 12:23
  • For those wondering how to fix it: Go to settings => Permalinks => Select an option (I used post name) => Save Changes => Verify that your /wp-json route works – Rafael Mejía Jul 13 '19 at 22:49
  • https://web.archive.org/web/20170324103810/http://lawsonry.github.io/2013/11/nginx-settings-wordpress-subdirectory-permalink-fix/ – etrey Jan 14 '21 at 07:31
2

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;
}
zdd
  • 8,258
  • 8
  • 46
  • 75
0

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

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

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

Dylan B
  • 796
  • 8
  • 16