json - 防止 JSON 响应上的 Apache 自定义错误页面

标签 json apache

我正在使用一个提供 HTML 内容和 JSON API 的应用程序。使用 Apache,我为 HTML 内容创建了自定义错误页面。该配置使用类型映射根据浏览器语言指定不同版本的错误页面。

问题是当 JSON API 返回错误时。该响应(包含一些描述错误的 JSON 元素)正在被 HTML 错误页面替换。如何将 Apache 配置为忽略 JSON 响应,返回未修改的响应?

这是 JSON 请求的接受 header :

Accept:application/json, text/javascript, */*; q=0.01

以下是来自应用程序 (Tomcat) 的响应 header :
Content-Length:23
Content-Type:text/json;charset=UTF-8
Date:Fri, 02 Sep 2016 15:34:03 GMT
Server:Apache-Coyote/1.1

但这里是 Apache 返回的响应 header :
Accept-Ranges:bytes
Connection:close
Content-Language:en
Content-Length:7628
Content-Location:404.en.html
Content-Type:text/html; charset=UTF-8
Date:Fri, 02 Sep 2016 15:14:08 GMT
Server:Apache
TCN:choice
Vary:negotiate,accept-language

下面是我的配置,供引用:

httpd.conf :
Alias /errors/ "/var/www/errors/"

<IfModule mod_negotiation.c>
<IfModule mod_include.c>
    <Directory "/var/www/errors">
        AllowOverride All
        Options IncludesNoExec
        AddOutputFilter Includes html
        AddHandler type-map var
        Order allow,deny
        Allow from all
        LanguagePriority en es de fr pt
        ForceLanguagePriority Prefer Fallback
    </Directory>
    ErrorDocument 404 /errors/404.var
    ErrorDocument 403 /errors/403.var
    ErrorDocument 500 /errors/500.var
    ErrorDocument 502 /errors/500.var
    ErrorDocument 503 /errors/maintenance.var

</IfModule>
</IfModule>

这是 .var 文件之一的示例。 404.var :
URI: 404

URI: 404.en.html
Content-type: text/html
Content-language: en en-US

URI: 404.en-GB.html
Content-type: text/html
Content-language: en-GB

URI: 404.fr.html
Content-type: text/html
Content-language: fr

URI: 404.de.html
Content-type: text/html
Content-language: de

URI: 404.es.html
Content-type: text/html
Content-language: es

URI: 404.pt.html
Content-type: text/html
Content-language: pt

这是另一个 .conf 文件中的一些额外配置,以确保我包含所有相关内容:
ProxyRequests Off
ProxyPreserveHost On
ProxyErrorOverride On

<Proxy *>
    Order deny,allow
    Allow from all
</Proxy>

ProxyPass /server-info !
ProxyPass /server-status !
ProxyPass /errors !

ProxyPass / http://localhost:7002/
ProxyPassReverse / http://localhost:7002/

<Location />
    Order allow,deny
    Allow from all
</Location>

最佳答案

这是我用于具有以下 header 的请求的内容:

Accept:application/json, text/javascript, */*; q=0.01 
<If "%{HTTP_ACCEPT} =~ /json/">
     ProxyErrorOverride Off
</If>

关于json - 防止 JSON 响应上的 Apache 自定义错误页面,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/39300234/

相关文章:

apache - 在 Apache Tomcat 7 上启用 8443 时遇到问题

php - 从 JSON 文件中获取第一个对象

ruby-on-rails - 如何在 Rails 中使用 JSON 填充我的数据库?

linux - 服务器上每个域的邮件客户端配置

linux - svnadmin 创建忽略目录上的粘性组位

node.js - 在 apache2 服务器上的 express Node js 中提供静态文件时自动重定向而无需代理传递名称

html - 有没有办法结合这些重定向?

javascript - 如何定义可在所有 API 中使用的基本 url

c# - JayRock 间歇性缺失值

c# - 如何在 C# 中解析 json 对象?