11

nginx has the $scheme variable usable in its log_format lines.

%H is the request protocol (e.g. "HTTP/1.1").

How can I do the same with Apache?

5 Answers 5

7

This works for me with Apache 2.4.23:

LogFormat "%{REQUEST_SCHEME}x ..." my_log_format

%{varname}x is only available when mod_ssl is loaded, see: https://httpd.apache.org/docs/2.4/mod/mod_ssl.html#logformats

3
3

One way to do it is to have two conditional CustomLog directives, controlled by whether the HTTPS variable is set.

CustomLog logs/access.log "https://..." env=HTTPS
CustomLog logs/access.log "http://..." env=!HTTPS

I have also tried using SetEnvIf in the following manner, but it doesn't work (it logs -):

SetEnv URL_SCHEME=http
SetEnvIf HTTPS on URL_SCHEME=https
CustomLog logs/access.log "%{URL_SCHEME}e://..."
4
  • 1
    your mod_env endeavors may have failed because of syntactical errors. SetEnv takes its variable and value without an equal sign: SetEnv URL_SCHEME http.
    – glasz
    Apr 5, 2014 at 23:30
  • 1
    also, the SetEnvIf line does not work on apache < 2.4.1 because mod_setenvif apparently has no access to the ssl environment. almost drove me crazy on 2.2.22.
    – glasz
    Apr 6, 2014 at 0:55
  • This environment variable seems specific to mod_ssl. If you use another module, such as mod_gnutls, you do not have it.
    – bortzmeyer
    Aug 22, 2014 at 14:09
  • Also, mod_ssl only sets the environment variable if you explicitly tell it to do so via SSLOptions. Aug 25, 2016 at 12:04
3

For some reason I could not get the examples above to work, so found another way: you can add 2 rewrite rules into your configuration as follows:

RewriteCond %{HTTPS} !=on
RewriteRule ^(.*)$ - [E=SCHEME:HTTP]

RewriteCond %{HTTPS} =on
RewriteRule ^(.*)$ - [E=SCHEME:HTTPS]

Then add this into your LogFormat definition.

scheme=\"%{SCHEME}e\"
3

As the url scheme is not directly available in apache log format, you may log the canonical port (e.g. 80/443) of the server serving the request by using %p as an alternative:

CustomLog access.log "%h %l %u %t \"%r\" %>s %b \"%{Referer}i\" \"%{User-agent}i\" port:%p"
3
  • How did this get two upvotes so quickly? Anyway, this is not useful if you want to effectively log requests' URLs, for which you need the protocol URI scheme. Feb 7, 2015 at 9:56
  • The port indicates the protocol.
    – Wick
    Apr 6, 2016 at 5:22
  • well ports can be changed so it's only viable if you use default ports though.
    – olivierg
    May 3, 2023 at 19:21
-2

Define your log output and add %H to it. This is the same for Apache.

So, you build a LogFormat like this and afaik there are a couple defined in the default apache configuration.

LogFormat "%h %l %u %t \"%r\" %>s %b" common

and add the name of the LogFormat (in this case "common") to the end of your logfile call

CustomLog logs/access_log common

Just look here for further informations about the log and here about the different format strings.

1
  • 1
    Um... I've already looked there. I stated in my question that %H does not do what I need. Feb 13, 2012 at 8:23

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .