Simply set the proxy_intercept_errors
option to on and create an nginx error page for it.
error_page 404 /404.html;
proxy_intercept_errors on;
To ensure that nginx will serve the static file from it’s document root you also have to specify the following:
location /404.html {
internal;
}
I'm assuming here that nginx is configured to talk with your app servers as proxy, because that is the usual way and your question does not mention any of this.