Nginx: try next upstream on http 200 response with custom header set

nginx

I have an nginx setup which works very well.
I am using an upstream block to load balance between two servers.
These servers give HTTP 200 response even if they unable to serve the request, but they set custom http header like this:

X-Response-Status: Failed

When the response is OK, they give:

X-Response-Status: OK

Is it possible to configure nginx to try next upstream server while the first one gives "X-Response-Status: Failed"?

I mean like following in nginx config:

proxy_next_upstream error timeout http_* ($sent_http_x_response_status="Failed")

Thanks!

UPDATE1:

I have to keep upstream servers giving HTTP 200 even on errors because there is an error message in http body in binary form.

I have to pass this binary error to client if none of upstream server are able to serve the request.

Best Answer

There's no option for proxy_next_upstream to implement the behavior you describe.

Your application should not return an HTTP 200 if it couldn't actually process the request. Have the application return a more appropriate error, such as 500 or 503.