使用mod_remoteip获取Apache 2.4访问日志以使用Varnish显示客户端IP而不是127.0.0.1

cur*_*ser 5 apache logging reverse-proxy varnish

对于我的生活,我无法获得mod_remoteip来获取我的Apache访问日志中的客户端IP.我正在使用安装在Apache 2.4.7前面的Varnish 4的Virtualmin设置.你是如何让它运作的?

cur*_*ser 12

我终于在日志中获得了客户端IP,我在这里找到了最后一步:

以下是使其工作的步骤:

  1. 获取Varnish以使用客户端IP将标头传递给Apache.你可以通过在vcl_recv的最开头包含这段代码(在这个答案中找到)来做到这一点:

    if (req.restarts == 0) {
      if (req.http.X-Forwarded-For) {
        set req.http.X-Forwarded-For = req.http.X-Forwarded-For + ", " + client.ip;
      } else {
        set req.http.X-Forwarded-For = client.ip;
      }
    }
    
    Run Code Online (Sandbox Code Playgroud)
  2. 现在在Apache中启用mod_remoteip.

  3. 编辑Apache配置以告诉mod_remoteip哪个头包含客户端IP(来自Apache文档).我正在使用X-Forwarded-For,但我想这可能是任何东西,只要它匹配你配置的Varnish传递:

    RemoteIPHeader X-Forwarded-For

  4. 如果你现在重新启动Apache和Varnish,我敢打赌Apache现在会引用客户端IP而不是127.0.0.1.除了访问日志,我一直在检查.要获取访问日志以显示客户端IP,我们需要修改它使用的Apache日志格式.就我而言,这是"组合"格式.这是我的突破,我发现它在这里链接到我们的目的这个优秀的信息.

这就是我的组合日志格式:

LogFormat "%h %l %u %t \"%r\" %>s %O \"%{Referer}i\" \"%{User-Agent}i\"" combined
Run Code Online (Sandbox Code Playgroud)

我只是将%a替换为%h,这就是它的样子:

LogFormat "%a %l %u %t \"%r\" %>s %O \"%{Referer}i\" \"%{User-Agent}i\"" combined
Run Code Online (Sandbox Code Playgroud)

最后,这是我的Apache配置文件的一个块(在它之前加载mod_remoteip):

# Note that the use of %{X-Forwarded-For}i instead of %h is not recommended.
# Use mod_remoteip instead.
RemoteIPHeader X-Forwarded-For

LogFormat "%a %l %u %t \"%r\" %>s %O \"%{Referer}i\" \"%{User-Agent}i\"" combined
Run Code Online (Sandbox Code Playgroud)