3

I have Nginx setup to reverse proxy all files ending in .php. In some scripts, I check for parameters and send "406 Not Acceptable" as a generic error which needs to be picked up on jQuery/from Ajax. I can change the error but I'd like to be able to send it to the client side. If I send 406, Nginx changes it into 502 Bad Gateway and injects it's own error message.

It works for 404 and 403 though, but those errors aren't really "applicable" in this circumstance; it's an API so forbidden isn't good RESTful practice.

I tried using fastcgi_intercept_errors but that seems to not make a difference.

I'm using Nginx 0.7.

Part of the PHP:

header($_SERVER['SERVER_PROTOCOL'] . '406 Not Acceptable');
echo '{"error":"Missing Params"}';
exit;

Nginx Config

    location ~ .php$ {
            fastcgi_intercept_errors on;
            fastcgi_pass 127.0.0.1:9000;
            fastcgi_index index.php;
            fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
            include fastcgi_params;
    }

1 Answer 1

4

You are missing a space after the protocol.

header($_SERVER['SERVER_PROTOCOL'] . ' 406 Not Acceptable'); //This works
header($_SERVER['SERVER_PROTOCOL'] . '406 Not Acceptable'); //This doesn't work.

I got the same result as you were seeing. With the space missing, Nginx intercepted the response and gave a 502 error page. It passes the 406 through when the space is there.

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .