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

前端之家收集整理的这篇文章主要介绍了linux – 带有JSON响应的Nginx错误页面前端之家小编觉得挺不错的,现在分享给大家,也给大家做个参考。
我正在尝试向客户提供维护页面,以便在维护时向我的应用程序发出请求.以下是我的Nginx配置.
  1. server {
  2. recursive_error_pages on;
  3. listen 80;
  4. ...
  5.  
  6. if (-f $document_root/maintenance.html) {
  7. return 503;
  8. }
  9.  
  10. error_page 404 /404.html;
  11. error_page 500 502 504 /500.html;
  12. error_page 503 @503;
  13.  
  14. location = /404.html {
  15. root $document_root;
  16. }
  17. location = /500.html {
  18. root $document_root;
  19. }
  20. location @503 {
  21. error_page 405 =/maintenance.html;
  22. if (-f $request_filename) {
  23. break;
  24. }
  25. rewrite ^(.*)$/maintenance.html break;
  26. }
  27. }

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

  1. $curl http://server.com/ -i -v -X GET -H "Accept: text/html"
  2. * Adding handle: conn: 0xf89420
  3. * Adding handle: send: 0
  4. * Adding handle: recv: 0
  5. * Curl_addHandleToPipeline: length: 1
  6. * - Conn 0 (0xf89420) send_pipe: 1,recv_pipe: 0
  7. * About to connect() to server.com port 80 (#0)
  8. * Trying xxx.xxx.xxx.xxx...
  9. * Connected to server.com (xxx.xxx.xxx.xxx) port 80 (#0)
  10. > GET / HTTP/1.1
  11. > User-Agent: curl/7.33.0
  12. > Host: server.com
  13. > Accept: text/html
  14. >
  15. < HTTP/1.1 503 Service Temporarily Unavailable
  16. HTTP/1.1 503 Service Temporarily Unavailable
  17. * Server Nginx/1.1.19 is not blacklisted
  18. < Server: Nginx/1.1.19
  19. Server: Nginx/1.1.19
  20. < Date: Thu,14 Nov 2013 11:16:16 GMT
  21. Date: Thu,14 Nov 2013 11:16:16 GMT
  22. < Content-Type: text/html
  23. Content-Type: text/html
  24. < Content-Length: 27
  25. Content-Length: 27
  26. < Connection: keep-alive
  27. Connection: keep-alive
  28.  
  29. <
  30. This is under maintenance.
  31. * Connection #0 to host server.com left intact

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

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

  1. $curl http://server.com/ -i -v -X GET -H "Accept: application/json"
  2. * Adding handle: conn: 0x190c430
  3. * Adding handle: send: 0
  4. * Adding handle: recv: 0
  5. * Curl_addHandleToPipeline: length: 1
  6. * - Conn 0 (0x190c430) send_pipe: 1,recv_pipe: 0
  7. * About to connect() to server.com port 80 (#0)
  8. * Trying xxx.xxx.xxx.xxx...
  9. * Connected to server.com (xxx.xxx.xxx.xxx) port 80 (#0)
  10. > GET / HTTP/1.1
  11. > User-Agent: curl/7.33.0
  12. > Host: server.com
  13. > Accept: application/json
  14. >
  15. < HTTP/1.1 503 Service Temporarily Unavailable
  16. HTTP/1.1 503 Service Temporarily Unavailable
  17. * Server Nginx/1.1.19 is not blacklisted
  18. < Server: Nginx/1.1.19
  19. Server: Nginx/1.1.19
  20. < Date: Thu,14 Nov 2013 11:15:50 GMT
  21. Date: Thu,14 Nov 2013 11:15:50 GMT
  22. < Content-Type: text/html
  23. Content-Type: text/html
  24. < Content-Length: 27
  25. Content-Length: 27
  26. < Connection: keep-alive
  27. Connection: keep-alive
  28.  
  29. <
  30. This is under maintenance.
  31. * Connection #0 to host server.com left intact

解决方法

尝试使用地图
  1. map $http_accept $maintenance_page {
  2. default /maintenance.html;
  3. ~application/json /maintenance.json;
  4. }

然后用$maintenance_page替换/maintenance.html

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

猜你在找的Linux相关文章