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

bdu*_*fin 5 apache json

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

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

这是 JSON 请求的接受标头:

Accept:application/json, text/javascript, */*; q=0.01
Run Code Online (Sandbox Code Playgroud)

以下是来自应用程序 (Tomcat) 的响应标头:

Content-Length:23
Content-Type:text/json;charset=UTF-8
Date:Fri, 02 Sep 2016 15:34:03 GMT
Server:Apache-Coyote/1.1
Run Code Online (Sandbox Code Playgroud)

但这里是 Apache 返回的响应标头:

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
Run Code Online (Sandbox Code Playgroud)

下面是我的配置,供参考:

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>
Run Code Online (Sandbox Code Playgroud)

这是 .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
Run Code Online (Sandbox Code Playgroud)

这是另一个 .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>
Run Code Online (Sandbox Code Playgroud)

小智 3

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

Accept:application/json, text/javascript, */*; q=0.01 
Run Code Online (Sandbox Code Playgroud)
<If "%{HTTP_ACCEPT} =~ /json/">
     ProxyErrorOverride Off
</If>
Run Code Online (Sandbox Code Playgroud)