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"}';

Nginx Config

    location ~ .php$ {
            fastcgi_intercept_errors on;
            fastcgi_index index.php;
            fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
            include fastcgi_params;

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.

| improve this answer | |

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.