Sunday, May 29, 2016

Custom Error Responses and Log Files in Apache Server

In this post, I decided to write some information about two important things we can configure in Apache server for a particular website. Those are custom error responses which we can send from the server to clients and log files which we can use to log different events occurring in the server for later investigations.

Custom error responses:

When a web client attempt to access a web page in our server which results in an error, the server is supposed to send an HTTP error response with the error code and some HTML content. In apache server, there are default HTTP error pages sent with an HTTP error responses. However, it may be interesting for a website to have custom error response pages with some nice and helpful messages to the web user. Let's see how we can add such a thing.

(1) An HTML page for the error response:

If we are going to use an HTML page for the user for an error response say for example for the error code 404. We have to put that HTML file in the web root of the website (eg: /var/www/smallco.com/) and then add an entry in the configuration file of the website as follows. It is important to note that a / is used to represent the web root of the website and then give the path to the HTML file inside the web root.

ErrorDocument 404 /meaningful-error.html

(2) Redirecting to a different URL:

If we want to redirect our web user to a different web URL upon when encountered with an HTTP error, we can specify the URL in the website configuration file and it is important to note that we should use http:// when writing the URL to properly redirect the user.

ErrorDocument 404 http://www.google.com

(3) A hard-coded string as an error response:

Instead of using the above two techniques, we can directly specify a text string which should be sent to the web user upon hitting an error.

ErrorDocument 404 "Sorry buddy, the page seems gone :("

To make these changes get effective, we should restart the apache server.

sudo service apache2 restart

Log files:

There are two important log files which can help us to troubleshoot and understand the activities going on in the apache web server. First thing is access log which is called TransferLog in apache terminology. We can use it to specify the file path where the server access details of different web clients should be written. The other log is ErrorLog which contains information about the errors faced by the apache server at different points.

In the apache configuration file for a particular website, we can specify where these log files should be saved. By default on an Ubuntu based system where we have installed apache2 server, /etc/apache2/envvars file contains the default path to these log files. In my system these files are pointed to /var/log/apache2/ directory.

(1) Let's change the location of these log files for our website. I will create a new directoy where I want to save these files.

sudo mkdir /var/log/apache2/smallco.com


Now, open the configuration file of our website and add the following entries to set the new paths for the configuration files.

ErrorLog /var/log/apache2/smallco.com/error.log
TransferLog /var/log/apache2/smallco.com/access.log


Just like the previous time, we should restart the apache server to make these log files become active.

sudo service apache2 restart

(2) In addition to those two default log files ErrorLog and TransferLog, we can define our own log files with our own logging formats. In order to do that, we can use LogFormat and CustomLog directives.

Add the following two lines to the configuration file of our website. Using the LogFormat directive, we specify a log format and give a name to this custom format. In this case we have given the name ourformat. Then using the CustomLog directive, we specify the path to that custom log file and the name of the log format.

LogFormat "%h %t \"%r\" %>s" ourformat
CustomLog /var/log/apache2/smallco.com/our-custom.log ourformat


In the log format, we have specified that we need to log the IP address of the client, access time, HTTP request from the client and the HTTP response code sent from the server. After adding these two files to the configuration file of the website, restart the apache server and access the website from a web browser.

sudo service apache2 restart
w3m smallco.com


Now we can check the content of the configuratin file we have defined.

cat /var/log/apache2/smallco.com/our-custom.log

In my log file, I could see a line like the following.

127.0.0.1 [29/May/2016:08:06:54 -0700] "GET / HTTP/1.0" 200

That's all folks!

No comments:

Post a Comment