After the connection between Nginx and the back-end server timed out, how to configure it if you want Nginx to return json data to the client?

suppose the requirement is as follows:
Nginx listens on port 80, the domain name of the backend server assumes http://hello.com,
Nginx accepts requests from the client as a proxy, and then Nginx forwards the request to http://hello.com. If the connection between Nginx and the back-end server times out, you have to return the data in json format to the client. The data is assumed to be like this:

{
    "code": -1
    "message":"failed to connect remote error"
} 

and the status code you want the client to receive is 500.
I would like to ask the seniors to achieve this function, how to configure Nginx?


proxy_intercept_errors on;
error_page 504 = @500;
location @500 {
    default_type application/json;
    return 500 '{"code": -1,"message":"failed to connect remote error"}';
}
Menu