带有 JSON 响应的 Nginx 错误页面

带有 JSON 响应的 Nginx 错误页面

我尝试在应用程序维护期间向向其发出请求的客户提供维护页面。以下是我为此目的所做的 nginx 配置。

server {
  recursive_error_pages on;
  listen 80;
   ...

  if (-f $document_root/maintenance.html) {
    return 503;
  }

  error_page 404 /404.html;
  error_page 500 502 504 /500.html;
  error_page 503 @503;

  location = /404.html {
    root $document_root;
  }
  location = /500.html {
    root $document_root;
  }
  location @503 {
    error_page 405 =/maintenance.html;
    if (-f $request_filename) {
      break;
    }
    rewrite ^(.*)$ /maintenance.html break;
  }
}

假设我已通过创建 启用了对我网站的维护$document_root/maintenance.html。当用户发出带有Accept标头的请求时,将正确地提供此文件text/html

$ curl http://server.com/ -i -v -X GET -H "Accept: text/html"
* Adding handle: conn: 0xf89420
* Adding handle: send: 0
* Adding handle: recv: 0
* Curl_addHandleToPipeline: length: 1
* - Conn 0 (0xf89420) send_pipe: 1, recv_pipe: 0
* About to connect() to server.com port 80 (#0)
*   Trying xxx.xxx.xxx.xxx...
* Connected to server.com (xxx.xxx.xxx.xxx) port 80 (#0)
> GET / HTTP/1.1
> User-Agent: curl/7.33.0
> Host: server.com
> Accept: text/html
> 
< HTTP/1.1 503 Service Temporarily Unavailable
HTTP/1.1 503 Service Temporarily Unavailable
* Server nginx/1.1.19 is not blacklisted
< Server: nginx/1.1.19
Server: nginx/1.1.19
< Date: Thu, 14 Nov 2013 11:16:16 GMT
Date: Thu, 14 Nov 2013 11:16:16 GMT
< Content-Type: text/html
Content-Type: text/html
< Content-Length: 27
Content-Length: 27
< Connection: keep-alive
Connection: keep-alive

< 
This is under maintenance.
* Connection #0 to host server.com left intact

现在有些客户端将Accept标头设置为application/json。我该如何向他们发送 JSON 响应而不是maintenance.html

Accept以下是我设置为 时得到的响应application/json

$ curl http://server.com/ -i -v -X GET -H "Accept: application/json"
* Adding handle: conn: 0x190c430
* Adding handle: send: 0
* Adding handle: recv: 0
* Curl_addHandleToPipeline: length: 1
* - Conn 0 (0x190c430) send_pipe: 1, recv_pipe: 0
* About to connect() to server.com port 80 (#0)
*   Trying xxx.xxx.xxx.xxx...
* Connected to server.com (xxx.xxx.xxx.xxx) port 80 (#0)
> GET / HTTP/1.1
> User-Agent: curl/7.33.0
> Host: server.com
> Accept: application/json
> 
< HTTP/1.1 503 Service Temporarily Unavailable
HTTP/1.1 503 Service Temporarily Unavailable
* Server nginx/1.1.19 is not blacklisted
< Server: nginx/1.1.19
Server: nginx/1.1.19
< Date: Thu, 14 Nov 2013 11:15:50 GMT
Date: Thu, 14 Nov 2013 11:15:50 GMT
< Content-Type: text/html
Content-Type: text/html
< Content-Length: 27
Content-Length: 27
< Connection: keep-alive
Connection: keep-alive

< 
This is under maintenance.
* Connection #0 to host server.com left intact

答案1

尝试使用地图

map $http_accept $maintenance_page {
    default /maintenance.html;
    ~application/json /maintenance.json;
}

然后将 /maintenance.html 替换为 $maintenance_page

更多详情http://nginx.org/en/docs/http/ngx_http_map_module.html

答案2

您可以尝试以下操作方法

  if (-f $document_root/system/maintenance.html) {
    return 503;
  }

  error_page 503 @maintenance;
  location @maintenance {
    internal;
    if ($http_accept ~ json) {
      return 503 "{}";
    }
    rewrite  ^(.*)$  /system/maintenance.html last;
    break;
  }

主要思想是检查请求的内容类型并相应地返回页面

相关内容