Apache :

LogFormat "%{X-Forwarded-For}i %l %u %t \"%r\" %>s %b \"%{Referer}i\" \"%{User-Agent}i\"" varnishcombined
 
ServerName www.example.com
  # [...]
  CustomLog /var/log/apache2/www.example.com/access.log varnishcombined
  # [...]
 

这样在apache的日志文件中就可以看到客户端真实的IP地址了。如果无法显示,则需要修改Varnish的配置文件

sub vcl_recv {
# Add a unique header containing the client address
remove req.http.X-Forwarded-For;
set req.http.X-Forwarded-For = client.ip;
# [...]
}
 

我这里测试的情况是如果我添加上面的两行,apache的日志就会输出两个客户端的ip地址。因此我没添加这两行。

Nginx:

log_format  access  '$remote_addr - $remote_user [$time_local] "$request" '
 '$status $body_bytes_sent "$http_referer" '
 '"$http_user_agent" $http_x_forwarded_for';
 access_log      /usr/local/nginx/logs/access.log  access;