How to setup Custom Apache Log Format with visitors IP details? Print

  • 2

Apache by default stores only log of host, link, user, time, referrer, browser agent. Being a server owner & administrator, this isn't enough. You may wish to see the visitors IP and other details, so you can sort them to find traffic, pattern, abusers so on. To setup custom Apache log format, you will need to locate httpd.conf file, which is apache configuration file.
To do so, run command: whereis httpd.conf , you will find directories where httpd configs are located as well httpd executable. On Centos / cPanel setup, it will be located at: /usr/local/apache/conf/httpd.conf. All you have to do is vi the file and replace new log format.
Before we go ahead, first let us take a backup of httpd.conf to make sure we do not break anything. Follow the steps give below & see the result yourself:
1. cp /usr/local/apache/conf/httpd.conf /usr/local/apache/conf/httpd.conf.bkp
2. vi /usr/local/apache/conf/httpd.conf 
Look for the lines:
LogFormat "%h %l %u %t \"%r\" %>s %b \"%{Referer}i\" \"%{User-Agent}i\"" combined
LogFormat "%h %l %u %t \"%r\" %>s %b" common
CustomLog "logs/access_log" combined

remove these above lines & replace with the following lines:

LogFormat "%h %l %u %t \"%r\" %>s %b \"%{Referer}i\" \"%{User-Agent}i\"" combined
LogFormat "%{X-Forwarded-For}i %l %u %t \"%r\" %>s %b \"%{Referer}i\" \"%{User-Agent}i\"" proxy
SetEnvIf X-Forwarded-For "^.*\..*\..*\..*" forwarded
CustomLog "logs/access_log" combined env=!forwarded
CustomLog "logs/access_log" proxy env=forwarded

Save file & restart apache. Check your access_log for new logs, access your website pages to see if its working. 

Was this answer helpful?

« Back

Powered by WHMCompleteSolution