Nginx和后端服务器建立连接超时后,想让Nginx返回json数据给客户端,该如何配置?

假设要求是这样的:
Nginx监听的端口是80,后端服务器域名假设是:http://hello.com
Nginx作为代理接受来自客户端的请求,然后Nginx把请求转发给http://hello.com。如果Nginx和后端服务器建立连接超时,得向客户端返回json格式的数据,数据假设是这样的:

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

并且希望客户端收到的状态码是500。
请问前辈们实现这个功能,该如何配置Nginx呢?

阅读 4k
1 个回答
proxy_intercept_errors on;
error_page 504 = @500;
location @500 {
    default_type application/json;
    return 500 '{"code": -1,"message":"failed to connect remote error"}';
}
撰写回答
你尚未登录,登录后可以
  • 和开发者交流问题的细节
  • 关注并接收问题和回答的更新提醒
  • 参与内容的编辑和改进,让解决方法与时俱进
推荐问题