加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 运营中心 > Nginx > 正文

linux – 带有JSON响应的Nginx错误页面

发布时间:2020-12-13 21:39:33 所属栏目: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

我正在尝试向客户提供维护页面,以便在维护时向我的应用程序发出请求.以下是我的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来启用我的网站维护.当用户使用带有text / html的Accept标头发出请求时,会正确地提供此文件.

$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
最佳答案
尝试使用地图

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

然后用$maintenance_page替换/maintenance.html

关于http://nginx.org/en/docs/http/ngx_http_map_module.html的更多细节

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读