Real life usage of the X-Forwarded-Host header?

2019-03-08 22:17发布

I've found some interesting reading on the X-Forwarded-* headers, including the Reverse Proxy Request Headers section in the Apache documentation, as well as the Wikipedia article on X-Forwarded-For.

I understand that:

  • X-Forwarded-For gives the address of the client which connected to the proxy
  • X-Forwarded-Port gives the port the client connected to on the proxy (e.g. 80 or 443)
  • X-Forwarded-Proto gives the protocol the client used to connect to the proxy (http or https)
  • X-Forwarded-Host gives the content of the Host header the client sent to the proxy.

These all make sense.

However, I still can't figure out a real life use case of X-Forwarded-Host. I understand the need to repeat the connection on a different port or using a different scheme, but why would a proxy server ever change the Host header when repeating the request to the target server?

7条回答
太酷不给撩
2楼-- · 2019-03-08 23:15

I can tell you a real life issue, I had an issue using an IBM portal.

In my case the problem was that the IBM portal has a rest service which retrieves an url for a resource, something like: {"url":"http://internal.host.name/path"}

What happened? Simple, when you enter from intranet everything works fine because internalHostName exists but... when the user enter from internet then the proxy is not able to resolve the host name and the portal crashes.

The fix for the IBM portal was to read the X-FORWARDED-HOST header and then change the response to something like: {"url":"http://internet.host.name/path"}

See that I put internet and not internal in the second response.

查看更多
登录 后发表回答