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

y53ybaqx  于 2023-08-07  发布在  Apache
关注(0)|答案(2)|浏览(112)

我正在使用一个同时提供HTML内容和JSON API的应用程序。我使用Apache为HTML内容创建了自定义错误页面。配置使用类型Map来基于浏览器语言指定错误页的不同版本。
问题是当JSON API返回错误时。该响应(包含一些描述错误的JSON元素)将被HTML错误页面替换。如何配置Apache以忽略JSON响应,并不加修改地返回它们?
下面是JSON请求的accept头:

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>

2hh7jdfx

2hh7jdfx1#

下面是我对具有以下头的请求使用的方法:

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

个字符

j9per5c4

j9per5c42#

另一种解决方案:
将所有返回json的脚本保存在单独的目录中,例如/ajax/api,然后像这样配置Apache:

ProxyErrorOverride On

<Directory "/path/to/ajax/or/api">
    ProxyErrorOverride Off
</Directory>

字符串
更新:如果可以的话,不要使用它。相反,使用您的后端语言(PHP、Python、Java)呈现错误页面。

相关问题