Home > database >  How to override status code received from proxy_pass in error fallback
How to override status code received from proxy_pass in error fallback

Time:11-19

I have this setup on my nginx config:

location @img_fallback {
    try_files $uri =404;
}

location ~ \.(png|jpe?g)$ {
    proxy_intercept_errors on;
    error_page 500 502 503 504 @img_fallback;
    proxy_pass https://someinternalservice.com:3000;
}

This works very well, it firsts tries the proxy, and then the try_files if it gets any error, but the problem is, if the proxy returns any errors like 502, and the try_files finds the image, we still get a 502 status code, even when the image itself loads.

How do we make sure that the status sent from the proxy is not used in the response in case of an error?

CodePudding user response:

When the specified error handler will be processed by another upstream (for example when you have something like error_page 502 /error_handler.php which will be processed by PHP-FPM backend) or when error handler is a named location, it is possible to tell nginx that the handler could change HTTP response code using the = sign:

error_page 500 502 503 504 = @img_fallback;
  • Related