I'd like to add a CustomLog
directive to my apache configuration to log the full URL requested (or at least the host portion of the URL). I have several domains being handled by the same instance of apache, and would like to be able to distinguish the domains in the logs (as now all I see is "GET /"). I see in the documentation on LogFormat it lists %U
to print the path portion of the URL, but I'm looking for the host.
Asked
Active
Viewed 3.0k times
17
jrdioko
- 567
- 5
- 9
- 18
4 Answers
20
Keep reading the LogFormat
documentation and you'll find:
%...{Foobar}i: The contents of Foobar: header line(s) in the request
sent to the server.
Which means you could include in your configuration:
%{Host}i
The %v
and %V
directives may also get you what you want.
%v will always be the value of ServerName
(the "canonical name" of your virtual host). %V
may be the value of ServerName
, or it may be the value of the HTTP Host
header, depending on whether or not you have UseCanonicalName
enabled in your configuration (and whether or not the client supplied a Host
header).
larsks
- 41,276
- 13
- 117
- 170
-
Will `%{Host}i` work even for HTTP/1.0? `%V` looks right at first glance, thanks! – jrdioko Jun 23 '11 at 17:49
-
I believe that %{Host}i (or any %{...}i construct) will only produce a result if that header actually exists in the request. So for HTTP/1.0, I would not expect it to be useful. – larsks Jun 23 '11 at 17:51
-
One more clarification: So `%V` will be identical to `%{Host}i` if `UseCanonicalName` is disabled? – jrdioko Jun 23 '11 at 18:29
-
1According to the documentation, "With UseCanonicalName off Apache will form self-referential URLs using the hostname and port supplied by the client if any are supplied (otherwise it will use the canonical name, as defined above)." So `%V` will use `ServerName` if there is no `Host` header. – larsks Jun 23 '11 at 18:46
3
LogFormat "%h %l %u %t \"%r\" %>s %b \"%{Referer}i\" \"%{User-Agent}i\" %{Host}i%U%q" combined
%{Host}i%U%q
gives full url.
borayeris
- 213
- 1
- 9
2
'%v' is the ServerName might be what you want?
agy
- 206
- 1
- 1
-
It looks like `%v` always returns the same string in my situation: the ServerName value regardless of what domain appears in the URL. But `%V` looks like it does the right thing. – jrdioko Jun 23 '11 at 17:48
-
2
Add %v
to your log format.
Something like this:
LogFormat "%v - %h %l %u %t \"%r\" %>s %b \"%{Referer}i\" \"%{User-agent}i\"" combined-vhost
CustomLog /log/file/location combined-vhost
Shane Madden
- 112,982
- 12
- 174
- 248
-
1It looks like `%v` always returns the same string in my situation: the ServerName value regardless of what domain appears in the URL. But `%V` looks like it does the right thing. – jrdioko Jun 23 '11 at 17:47