鍍金池/ 問答/Java  Linux/ Nginx和后端服務器建立連接超時后,想讓Nginx返回json數(shù)據(jù)給客戶端,該

Nginx和后端服務器建立連接超時后,想讓Nginx返回json數(shù)據(jù)給客戶端,該如何配置?

假設要求是這樣的:
Nginx監(jiān)聽的端口是80,后端服務器域名假設是:http://hello.com
Nginx作為代理接受來自客戶端的請求,然后Nginx把請求轉(zhuǎn)發(fā)給http://hello.com。如果Nginx和后端服務器建立連接超時,得向客戶端返回json格式的數(shù)據(jù),數(shù)據(jù)假設是這樣的:

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

并且希望客戶端收到的狀態(tài)碼是500。
請問前輩們實現(xiàn)這個功能,該如何配置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"}';
}
2018年9月7日 22:52