How to log the original status code in access_log when using Apache ErrorDocument that points to a remote URL
Environment
- Red Hat Enterprise Linux (RHEL)
- Red Hat JBoss Web Server
- Apache httpd
Issue
When I configured ErrorDocument that points to a remote URL like:
ErrorDocument 500 http://foo.example.com/cgi-bin/error500.cgi
The original status code 500 is not logged in access_log but 302 is logged instead.
Is this expected behavior? And is there any way to log the original status code?
Resolution
This is expected behavior as noted in the Apache httpd document:
Note that when you specify an ErrorDocument that points to a remote URL (ie. anything with a method such as http in front of it), Apache will send a redirect to the client to tell it where to find the document, even if the document ends up being on the same server. This has several implications, the most important being that the client will not receive the original error status code, but instead will receive a redirect status code.
We recommend you to specify local content (or error message string) to ErrorDocument.
If you want to redirect to the other remote server and log the original status code in access_log, you can specify custom CGI script that redirect to the remote server for ErrorDocument and the environment variable REDIRECT_STATUS to obtain the original status code.
For example:
ErrorDocument 500 /cgi-bin/errorpage.pl
ErrorDocument 502 /cgi-bin/errorpage.pl
LogFormat "%h %l %u %t \"%r\" %>s %b \"%{Referer}i\" \"%{User-Agent}i\" %{REDIRECT_STATUS}e" custom
CustomLog logs/access_log custom
with the custom error page cgi /var/www/cgi-bin/errorpage.pl:
#!/usr/bin/perl
print "Status: 302 Found\n";
printf "Location: http://foo.example.com/cgi-bin/error%s.cgi\n\n", $ENV{"REDIRECT_STATUS"};
See also to the following Apache documentations for reference:
This solution is part of Red Hat’s fast-track publication program, providing a huge library of solutions that Red Hat engineers have created while supporting our customers. To give you the knowledge you need the instant it becomes available, these articles may be presented in a raw and unedited form.
Welcome! Check out the Getting Started with Red Hat page for quick tours and guides for common tasks.
