Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Prevent Apache custom error pages on JSON responses

Tags:

json

apache

I'm working with an application that provides both HTML content and JSON APIs. Using Apache, I created custom error pages for the HTML content. The configuration uses type-maps to specify different versions of the error page based on the browser language.

The problem is when there is an error returned by the JSON API. That response (containing some JSON elements describing the error) is being replaced with an HTML error page. How can I configure Apache to ignore JSON responses, returning them unmodified?

Here's the accept header of the JSON request:

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

Here are the response headers coming from the application (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

But here are the response headers that Apache is returning instead:

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

Here are my configurations, for reference:

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>

Here's an example of one of the .var files. 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

And here are some additional configurations in another .conf file, to make sure I'm including everything relevant:

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>
like image 775
bduffin Avatar asked Nov 09 '22 09:11

bduffin


1 Answers

Here is what I use for requests with the following header:

Accept:application/json, text/javascript, */*; q=0.01 
<If "%{HTTP_ACCEPT} =~ /json/">
     ProxyErrorOverride Off
</If>
like image 70
Maxime Avatar answered Nov 15 '22 06:11

Maxime