I am using Nginx in front of unicorn. When the application is under high load and all the unicorn workers are busy nginx will return a 502 response saying that the gateway is misconfigured. I'm ok with this behavior, but in this condition I want nginx to return a 503 response code (server busy, try again later) which is a much more appropriate response.
I saw this answer: Is it possible to change the HTTP status code returned when proxy_pass gateway is down in nginx?
which describes how to rewrite some status codes returned from the upstream proxy:
location / {
proxy_pass http://backend;
proxy_intercept_errors on;
error_page 502 503 504 =503 @proxyisdown; # always reply with 503
}
location @proxyisdown {
add_header Retry-After 500;
index my_pretty_error_page.html;
}
But there are 2 issues with this:
- The upstream application does sometimes return 502 response codes for valid situations that should be retained
- I'm not convinced that the upstream proxy is actually returning this erronous status code, more likely nginx can't place the item on the (tcp?) buffer and then nginx returns the misleading status code.
Is there any way I can control the status code returned by nginx in this situation so that it is a 503 instead of a 502?
proxy_intercept_errors on;
if you don't want to intercept errors from proxy? And why do you think that "nginx returns the misleading status code"? – VBart