2016-09-02 40 views
3

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

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

这里的JSON请求的接受头:

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

这里是响应头从应用未来(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 

但这里有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 

这里是我的配置,仅供参考:

的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> 

回答

0

以下是我用来与要求下面标头:

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