X-Original-For header: 它的目的是什么?

X-Original-For header: what's its purpose?

在记录我的 Web 应用程序(在负载均衡器 + 防火墙后面)收到的 HTTP headers 时,我注意到我收到了 X-Original-For 和 X-Original-Protoheaders(除了传统的X-Forwared-XXXheaders)。

他们的目的是什么?

简短回答:X-Original-* 表示 原始 header 值 HttpContext.Connection 和 [=15= 中收到 ]

长版:当使用Nginx/IIS/Apache设置反向代理时,HttpContext.ConnnectionHttpContext.Request将改为X-Forwarded-*header、X-Original-*、header中的left-most值用来保存原来的HttpContext.ConnectionHttpContext.Request值:

  1. 将原来的HttpContext.Request.Scheme保存为headerX-Original-Proto: ...,然后HttpContext.Request.Scheme改为left-most中的方案header 共 X-Forwarded-Proto: o1, o2, ...
  2. 原来的HttpContext.Request.Host会保存为headerX-Original-Host: <original-host>,然后HttpContext.Request.Host会改成left-most主机在header 共 X-Forwarded-Host: o1, o2, ...
  3. 将原来的HttpContext.Connection.RemoteIpAddressHttpContext.Connection.RemotePort保存为headerOriginalForHeaderName: <original-endpoint>,然后将此值改为left-most中的IP和端口header 共 X-Forwarded-For: o1, o2, ...

source code of saving X-Original-For:

requestHeaders[_options.OriginalForHeaderName] = new IPEndPoint(connection.RemoteIpAddress, connection.RemotePort).ToString();

参见source code of saving X-Original-Proto

requestHeaders[_options.OriginalProtoHeaderName] = request.Scheme;

参见source code of saving X-Original-Host

requestHeaders[_options.OriginalHostHeaderName] = request.Host.ToString();