如何在Nginx中设置服务器错误和超时的JSON响应

时间:2018-12-04 10:01:33

标签: nginx

我想为超时或内部服务器错误设置json响应。

server {
    server_name 192.168.1.16;
    location / {
        proxy_pass http://localhost:5000;
        proxy_set_header Host $host;
        proxy_set_header X-Real-IP $remote_addr;
        proxy_read_timeout 5s;
        proxy_connect_timeout 5s;
        proxy_send_timeout 6s;
        send_timeout 5s;
    }
    location /static {
        alias /home/oem/PycharmProjects/indiawyn-backend/static;
    }
    location  ^~ /json_files {
        root /home/www/;
    }
}

我不知道在哪里配置。 请为此共享任何文档或答案。

1 个答案:

答案 0 :(得分:0)

例如:

server {
    ...
    error_page 500 /json_files/500.json;
    error_page 504 /json_files/504.json;
    ...
}

/home/www/json_files/500.json/home/www/json_files/504.json文件中放置类似

{
  "code": 500,
  "error": "internal_server_error",
  "error_description": "Internal server error"
}

{
  "code": 504,
  "error": "gateway_timeout",
  "error_description": "Gateway timeout"
}

如果您的mime类型设置正确,nginx将自动添加Content-Type: application/json标头。