起因

最近因为某个站点的流量异常,需要统计一下服务器的来源IP,本来开一下IIS日志就能搞定的事儿,但不幸的是生产服务器使用F5做了负载均衡,IIS日志无法记录到真实IP,真实的IP在“x-forwarded-for”中,baidu了一堆,没几个靠谱的,还好有个bing能用,很快找到了下这篇文章,文章写的条理清晰,浅显易懂,立马转了过来,省的什么时候又给墙了,大家看原文吧,我懒得翻译了。

原文:http://www.loadbalancer.org/blog/iis-and-x-forwarded-for-header/

What's the issue?

When you load balance your website at layer 7, it will become non-transparent. This means that the actual client source IP address is replaced by the load balancer's own IP address - and only this address will be recorded in the IIS logs.
One way around this is to insert XFF headers on the load balancer to track the actual client source IP address. IIS can then be reconfigured to make this data available in the logs.
NB. The other options to gain source IP transparency are to configure the load balancer in layer 4 NAT mode, layer 4 DSR mode or Layer 7 with TPROXY enabled.

The solution - how to modify logging behaviour

The steps required depend on your version of IIS.

IIS 8.5 & later

A custom logging field can be added to record XFF headers.
To enable this feature:

  1. Start IIS Manager, then on the Connections pane on the left, click the appropriate website where you want to enable XFF logging. The Home page is then displayed in the main panel.
  2. From the Home page, double-click Logging.
  3. From the Log File section, click Select Fields.
  4. From the bottom left corner, click Add Field.
  5. In the Add Custom Field window, complete the following:

    • in Field Name, type X-Forwarded-For
    • in Source, type X-Forwarded-For
    • leave Source Type set to ‘Request Header’
    • click OK on the Add Custom Field window
    • click OK on the W3C Logging Fields window
  6. From the Actions pane on the right, click Apply to implement the change.
  7. The log files are located by default in the directory %SystemDrive%\inetpub\logs\LogFiles. IIS creates new log files and appends “_x” to the log file names to indicate that they contain custom fields.

UIIS 7 & IIS 7.5

For IIS 7 & 7.5 the Advanced Logging add-on must be installed. This can be downloaded here. Once installed on the IIS server, you'll see an extra option called 'Advanced Logging' in IIS.
To configure and enable Advanced Logging:

  1. Start IIS Manager, then on the Connections pane on the left, click the server, website or directory where you want to configure Advanced Logging. The related Home page is displayed in the main panel.
  2. From the Home page, under IIS, double-click Advanced Logging.
  3. From the Actions pane on the right, click Enable Advanced Logging.
  4. From the Actions pane on the right, click Edit Logging Fields.
  5. On the Edit Logging Fields window click Add Field, and then complete the following:

    • in Field ID, type ClientSourceIP
    • in Category, type Default
    • in Source type, select Request Header
    • in Source name, type X-Forwarded-For
    • click OK on the Add Logging Field form
    • click OK on the Edit Logging Fields form
  6. From the Actions pane on the right, click Add Log Definition.
  7. Enter Client Source IP in the Base file name field.
  8. Click Select Fields.

    • Check (enable) the ClientSourceIP field created earlier
  9. Click OK on the Select Logging Fields form, then click Apply in the actions pane.
  10. Click Return To Advanced Logging in the actions pane.
  11. Run iisreset /restart to apply the new settings.
  12. The advanced log must be viewed rather than the default log. The advanced log is located in %SystemDrive%inetpublogsAdvancedLogs. To view the logs, right-click the Client Source IP log definition and click View Log Files.

IIS 6

Unfortunately, the Microsoft solution mentioned above is not available for IIS 6. Luckily there are a number of other solutions - some costing money and others that have been released as open source. One excellent example that we've tested with our products is F5's X-Forwarded-For ISAPI filter. It's available in both in 32- and 64-bit versions.

  1. Download the zipped archive from here and extract to an appropriate folder.
  2. Navigate to the relevant version (32- or 64-bit).
  3. Copy F5XForwardedFor.dll to a suitable location on your server, e.g. C:ISAPIfilters.
  4. Make sure you have ISAPI Filters enabled on your IIS server.
  5. Open IIS Manager, right-click the site and select Properties.
  6. Select the ISAPI Filters tab.
  7. Click add, then in the popup enter a suitable name and select the DLL file stored in step 3.
  8. Restart your website.

后记

关于IIS日志分析,这里再推荐个工具HttpLogBrowser(传送门),基本上大部分的统计需求都可以满足,实在是居家旅行之必备。

补充

其实还有个方法,可以直接在IIS日志里输出,就是使用F5XForwardedFor,注册完成后重启IIS,就会在日志里直接输出(传送门),我试了也是可行的,但不知道是不是官方方案,生产没敢搞。

IIS日志如何记录X-Forwarded-For的更多相关文章

  1. IIS日志-网站运维的好帮手

    对于一个需要长期维护的网站来说,如何让网站长久稳定运行是件很有意义的事情. 有些在开发阶段没有暴露的问题很有可能就在运维阶段出现了,这也是很正常的. 还有些时候,我们希望不断地优化网站,让网站更快速的 ...

  2. 网站运维工具使用iis日志分析工具分析iis日志(iis日志的配置)

    我们只能通过各种系统日志来分析网站的运行状况,对于部署在IIS上的网站来说,IIS日志提供了最有价值的信息,我们可以通过它来分析网站的响应情况,来判断网站是否有性能问题,或者存在哪些需要改进的地方 对 ...

  3. 【转】IIS日志-网站运维的好帮手

    对于一个需要长期维护的网站来说,如何让网站长久稳定运行是件很有意义的事情. 有些在开发阶段没有暴露的问题很有可能就在运维阶段出现了,这也是很正常的. 还有些时候,我们希望不断地优化网站,让网站更快速的 ...

  4. X-Forwarded-For (IIS日志记录用户真实IP)

    参考:http://www.jbxue.com/article/7521.html 当IIS放在反向代理后面时,日志中的客户端ip是反向代理服务器的ip,不是用户的真实IP地址. 本文为大家介绍使用X ...

  5. [转]IIS 日志记录时间和实际时间 不一样

    今天偶然发现 2003 系统IIS 日志记录时间和实际时间总是差了8个小时,也就是慢了8个小时.苦苦找了半天才发现如下办法能解决 ,特发来分享下 解决1:如果 IIS日志记录默认使用的是W3C扩展日志 ...

  6. Apache/Nginx/IIS日志记录的各个字段内容与含义

    一.Apache 1.1 Apache日志文件名称及路径介绍 当我们安装并启动Apache后,Apache会自动生成两个日志文件,这两个日志文件分别是访问日志access_log(在Windows上是 ...

  7. iis日志查看

    IIS日志是每个服务器管理者都必须学会查看的,服务器的一些状况和访问IP的来源都会记录在IIS日志中,所以IIS日志对每个服务器管理者非常的重要,seoer也不例外,这点同时也可方便网站管理人员查看网 ...

  8. 云计算之路-阿里云上:结合IIS日志分析“黑色30秒”问题

    在昨天针对“黑色30秒”问题的分析中,我们猜测Requests Queued上升是由于正在处理的请求出不去(到达不了客户端).今天我们结合IIS日志验证这个猜测. IIS日志中有一个重要的指标——ti ...

  9. 【一步一图】:详解IIS日志配置

    打开网站配置:右键点击属性 弹出设置界面     如上图,日志可选4种格式: [Microsoft IIS 日志文件格式]   存放地址如上图 以in开头 年份后两位+月份+日 命名: 示例: //, ...

随机推荐

  1. XScreenSaver强大的锁屏工具

    source install:  https://www.jwz.org/xscreensaver/ XScreenSaver     Related articles DPMS Xresources ...

  2. 终于好了 ipython 里执行dos命令 显示结果却显示在kernel界面里 搞定了

    import os cmd = r'type c:\foo.txt' os.system(cmd) import os cmd = r'type c:\foo.txt' os.system(cmd) ...

  3. Jmeter断言中判断请求失败的响应代码问题

    很多http请求会返回400.404.500.502等错误,我们在断言中一般会直接去勾选响应代码,然后匹配得到的响应代码,但是总是失败.百思不得其解! 后来查询Jmeter官方帮助时,在里面找到了答案 ...

  4. zabbix真的很简单 (安装篇)

    系统环境: Centos 6.4 一直觉得 zabbix 很简单,但是还是有好多人看了好多文档都搞不明白怎么用,我从2013年使用到现在也小有心得,如果时间允许,很高兴与大家一起分享我在使用过程中的一 ...

  5. Python笔记(五)_内置函数BIF

    查看所有的内置函数:dir(__builtins__) abs()   获取绝对值 max()   返回给定元素中的最大值 min()   返回给定元素中的最小值 sum()   求和 reverse ...

  6. 更新python的依赖包,亲测!

    输入pip install --upgrade pandas 无需卸载,让它自己更新就好 pandas可以改成别的包

  7. spring, spring mvc, mybatis整合文件配置详解

    转自:http://www.cnblogs.com/wxisme/p/4924561.html 使用SSM框架做了几个小项目了,感觉还不错是时候总结一下了.先总结一下SSM整合的文件配置.其实具体的用 ...

  8. 【目录】mysql 架构篇系列

    随笔分类 - mysql 架构篇系列 mysql 架构篇系列 4 复制架构一主一从搭建(半同步复制) 摘要: 一.概述 在mysql 5.5之前,mysql 的复制是异步操作,主库和从库的数据之间存在 ...

  9. 字符串格式的Url的截取

    一,我们先在看在页面上获取的URL的处理,如下方法: //获取全部URL string Url = Request.Url.ToString(); Url += "</br>&q ...

  10. MVC的实体模型写在类库,为什么被其他类库调用时,用不了模型的表?

    一,很简单,由于第一次添加实体模型时,VS会自动帮你添加引用System.Data.Entity到当前类库,如下图示: 二,而手动添加的类库并不存在这个引用,则及时你引用了当前的实体模型的类库,却使用 ...