How to configure logging in Apache Web server

In this guide let us see how to log any required particular logs in Apache web server.

To read about Apache related topic click on below links.

    1. How to install Apache Web Server in Red Hat Enterprise Linux 7 and Variants
    2. Understanding Apache Web Server logs




  • Is it possible to log a client IP Address which coming through the proxy?

No, we cant log clients IP which coming through Proxy.

  • Is it possible to log a client MAC Address in Apache logs?

In Apache, this is not possible.

  • Is it possible to logging cookies?

Yes, its possible by defining below directive in httpd configuration.

To log clients, cookies define directive as follows.

CustomLog logs/cookies_in.log "%{UNIQUE_ID}e %{Cookie}i"
CustomLog logs/cookies2_in.log "%{UNIQUE_ID}e %{Cookie2}i"

To log server to the client cookies

CustomLog logs/cookies_out.log "%{UNIQUE_ID}e %{Set-Cookie}o"
CustomLog logs/cookies2_out.log "%{UNIQUE_ID}e %{Set-Cookie2}o"
  • Is it possible to rotate logfiles in Apache Web Server?




Yes, its possible by defining customLog directive as follows.

Just we need to pipe the logs through rotate logs. By default, rotate logs will be available once we have done with installing httpd package.

CustomLog "| /usr/sbin/rotatelogs /var/log/httpd/access_log.%Y-%m-%d 86400" combined

86400 = 1 day

  • Log Hostnames Instead of IP Addresses

While we install Apache web server using httpd package we may notice httpd-tools will installed as its dependencies, it contains the tool logresolve we can locate it under /usr/bin/logresolve.

Logresolve will help to resolve the hostname from IP address. By defining below directive we can log and resolve the hostname from IP address under httpd configuration.

HostnameLookups On
CustomLog "| /usr/bin/logresolve -c >> /var/log/httpd/access_log.resolved" combined
  • To log the name of browser used by end user.

If we required knowing which browser is used by end users we need to append with below string in Custom log directive.

CustomLog /var/log/httpd/agent_log "%{User-agent}i"

In this chapter, we have seen about logging particular required things in Apache. In next chapter let us see how to define a virtual host in Apache Web server in various methods.