Apache – Log Request Port Number in Apache 2.4

apache-2.4log-filesloggingport

I've got two virtual hosts to serve the same site configured in Apache/2.4.18 (Ubuntu), one for http port 80, and the other for https port 443.

Currently I have them generating two separate log files: access.http.log and access.ssl.log but it's quite annoying to have to flip back and forth between the two during audits. I originally had both hosts log to the same file access.log but I couldn't distinguish between the two when troubleshooting traffic requests.

I would like to log them to just one file again, but I can't find in the documentation how to log the port number of the request so I can tell the virtual hosts apart inline. Is this possible?

One sloppy way I noticed is that the protocol is logged in the http referrer (https://localhost/referringpage.php) but this isn't adequate enough since redirects and links could come from anywhere – I just want to log the port of the current request.

Best Answer

You will need to define a custom log pattern (http://httpd.apache.org/docs/current/mod/mod_log_config.html) which can include the value of an environment variable using the %{VARNAME}e notation. Using the variables that mod_ssl makes available (https://httpd.apache.org/docs/2.4/mod/mod_ssl.html) you could include at a minimum something like %{HTTPS}e which will show which requests were over SSL/TLS or not.

Personally I tend add a few of those variables so I can audit which ciphers, protocol version, etc. are used by clients to influence tuning decisions.

Once you have that, putting it into an ELK stack as suggested in the comments allows you to create some informative dashboards.

Related Topic