Skip to content
This repository has been archived by the owner on Sep 24, 2018. It is now read-only.

WP-API and NGINX : query parameters not working #540

Closed
thomastraum opened this issue Oct 28, 2014 · 4 comments
Closed

WP-API and NGINX : query parameters not working #540

thomastraum opened this issue Oct 28, 2014 · 4 comments

Comments

@thomastraum
Copy link

Thanks for a great plugin. This is possibly not an issue with the plugin, but it might be an interesting issue others will run into. The plugin works perfectly on my local dev environment (apache) and on an apache server. But on my future production server with nginx it doesn't.

http://2013.thomastraum.com/wp-json/posts

the query above returns one test post, which is the correct behaviour, but the query below won't work, all query parameters are ignored and it returns the same results as the above.

http://2013.thomastraum.com/wp-json/posts?type[]=page

you can see the correct query result here:

http://stage.thomastraum.com/wp-json/posts?type[]=page

As I said the only difference is the webserver. here is my nginx config file. my nginx version is nginx/1.1.19

server {
    # .domain.com will match both domain.com and anything.domain.com
    server_name 2013.thomastraum.com;

    # It is best to place the root of the server block at the server level, and not the location level
    # any location block path will be relative to this root. 
    root /var/www/2013.thomastraum.com;

    # It's always good to set logs, note however you cannot turn off the error log
    # setting error_log off; will simply create a file called 'off'.
    access_log /var/log/nginx/2013.thomastraum.com.log;
    error_log /var/log/nginx/2013.thomastraum.com.error.log;

    # This can also go in the http { } level
    index index.html index.htm index.php;

    location / { 
        # if you're just using wordpress and don't want extra rewrites
        # then replace the word @rewrites with /index.php
        # try_files $uri $uri/ index.php;
        try_files $uri $uri/ /index.php?q=$request_uri;
    }

    location @rewrites {
        # Can put some of your own rewrite rules in here
        # for example rewrite ^/~(.*)/(.*)/? /users/$1/$2 last;
        # If nothing matches we'll just send it to /index.php
        rewrite ^ /index.php last;

    }

    # This block will catch static file requests, such as images, css, js
    # The ?: prefix is a 'non-capturing' mark, meaning we do not require
    # the pattern to be captured into $1 which should help improve performance
    location ~* \.(?:ico|css|js|gif|jpe?g|png)$ {
        # Some basic cache-control for static files to be sent to the browser
        expires max;
        add_header Pragma public;
        add_header Cache-Control "public, must-revalidate, proxy-revalidate";
    }

    # remove the robots line if you want to use wordpress' virtual robots.txt
    location = /robots.txt  { access_log off; log_not_found off; }
    location = /favicon.ico { access_log off; log_not_found off; }  

    # this prevents hidden files (beginning with a period) from being served
    location ~ /\.          { access_log off; log_not_found off; deny all; }

    location ~ \.php {
            fastcgi_param  QUERY_STRING       $query_string;
            fastcgi_param  REQUEST_METHOD     $request_method;
            fastcgi_param  CONTENT_TYPE       $content_type;
            fastcgi_param  CONTENT_LENGTH     $content_length;

            fastcgi_param  SCRIPT_NAME        $fastcgi_script_name;
            fastcgi_param  SCRIPT_FILENAME    $document_root$fastcgi_script_name;
            fastcgi_param  REQUEST_URI        $request_uri;
            fastcgi_param  DOCUMENT_URI       $document_uri;
            fastcgi_param  DOCUMENT_ROOT      $document_root;
            fastcgi_param  SERVER_PROTOCOL    $server_protocol;

            fastcgi_param  GATEWAY_INTERFACE  CGI/1.1;
            fastcgi_param  SERVER_SOFTWARE    nginx;

            fastcgi_param  REMOTE_ADDR        $remote_addr;
            fastcgi_param  REMOTE_PORT        $remote_port;
            fastcgi_param  SERVER_ADDR        $server_addr;
            fastcgi_param  SERVER_PORT        $server_port;
            fastcgi_param  SERVER_NAME        $server_name;

            fastcgi_pass 127.0.0.1:9000;
    }
}
@oyvind-lillegardsetre
Copy link

@thomastraum , Did you find a solution for this? Looks like I might have the same problem.

@thomastraum
Copy link
Author

no nothing. its a super weird issue and hard to debug, as I don't know where things go exactly wrong. I tried without permalinks as well which gave me loads of 502 depending on how many post_per_page I queried, which seems quite random.

@thomastraum
Copy link
Author

if anyone comes across this issue... years later the answer is either this #1926 or the buffer size in your nginx fast_cgi parameters which needs to be increased https://gist.github.com/magnetikonline/11312172#determine-fastcgi-response-sizes

@alvarotala
Copy link

I just added this to the config file:

location ~ /wp-json/ { try_files $uri $uri/ /index.php?$args; }

Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
None yet
Projects
None yet
Development

No branches or pull requests

4 participants