欢迎各位兄弟 发布技术文章

这里的技术是共享的

You are here

Apache CustomLog to log full URL 域名 主机名 domain 有大用

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.


4 Answers 正确答案

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).

shareimprove this answeredited Jun 23 '11 at 17:53answered Jun 23 '11 at 17:32larsks32.7k377134

  • 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
  • 1
    According 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 

'%v' is the ServerName might be what you want?

shareimprove this answeranswered Jun 23 '11 at 17:32agy1861

  • 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 

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
shareimprove this answeranswered Jun 23 '11 at 17:33Shane Madden♦99.1k8125203

  • 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:47 

LogFormat "%h %l %u %t \"%r\" %>s %b \"%{Referer}i\" \"%{User-Agent}i\" %{Host}i%U%q" combined

%{Host}i%U%qgives full url.

shareimprove this answeranswered Sep 16 '15 at 22:41borayeris1918

Your Answer

来自  https://serverfault.com/questions/283513/apache-customlog-to-log-full-url

普通分类: