簡體   English   中英

使用mod_remoteip獲取Apache 2.4訪問日志以使用Varnish顯示客戶端IP而不是127.0.0.1

[英]Getting Apache 2.4 access logs to show client IP instead of 127.0.0.1 with Varnish using mod_remoteip

對於我的生活,我無法獲得mod_remoteip來獲取我的Apache訪問日志中的客戶端IP。 我正在使用安裝在Apache 2.4.7前面的Varnish 4的Virtualmin設置。 你是如何讓它運作的?

我終於在日志中獲得了客戶端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; } } 
  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

我只是將%a替換為%h,這就是它的樣子:

LogFormat "%a %l %u %t \"%r\" %>s %O \"%{Referer}i\" \"%{User-Agent}i\"" combined

最后,這是我的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

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM