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

HttpProxyHttp Headers

Http Problem Overview


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?

Http Solutions


Solution 1 - Http

If you use a front-end service like Apigee as the front-end to your APIs, you will need something like X-FORWARDED-HOST to understand what hostname was used to connect to the API, because Apigee gets configured with whatever your backend DNS is, nginx and your app stack only see the Host header as your backend DNS name, not the hostname that was called in the first place.

Solution 2 - Http

This is the scenario I worked on today: Users access certain application server using "https://neaturl.company.com" URL which is pointing to Reverse Proxy. Proxy then terminates SSL and redirects users' requests to the actual application server which has URL of "http://192.168.1.1:5555";. The problem is - when application server needed to redirect user to other page on the same server using absolute path, it was using latter URL and users don't have access to this. Using X-Forwarded-Host (+ X-Forwarded-Proto and X-Forwarded-Port) allowed our proxy to tell application server which URL user used originally and thus server started to generate correct absolute path in its responses.

In this case there was no option to stop application server to generate absolute URLs nor configure it for "public url" manually.

Solution 3 - Http

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.

Solution 4 - Http

For the need for 'x-forwarded-host', I can think of a virtual hosting scenario where there are several internal hosts (internal network) and a reverse proxy sitting in between those hosts and the internet. If the requested host is part of the internal network, the requested host resolves to the reverse proxy IP and the web browser sends the request to the reverse proxy. This reverse proxy finds the appropriate internal host and forwards the request sent by the client to this host. In doing so, the reverse proxy changes the host field to match the internal host and sets the x-forward-host to the actual host requested by the client. More details on reverse proxy can be found in this wikipedia page http://en.wikipedia.org/wiki/Reverse_proxy.

Check this post for details on x-forwarded-for header and a simple demo python script that shows how a web-server can detect the use of a proxy server: [x-forwarded-for explained][1]

[1]: http://remotalks.blogspot.com/2013/12/x-forwarded-for-proxy-server-detection.html "x-forwarded-for"

Solution 5 - Http

One example could be a proxy that blocks certain hosts and redirects them to an external block page. In fact, I’m almost certain my school filter does this…

(And the reason they might not just pass on the original Host as Host is because some servers [Nginx?] reject any traffic to the wrong Host.)

Solution 6 - Http

X-Forwarded-Host just saved my life. CDNs (or reverse proxy if you'd like to go down to "trees") determine which origin to use by Host header a user comes to them with. Thus, a CDN can't use the same Host header to contact the origin - otherwise, the CDN would go to itself in a loop rather than going to the origin. Thus, the CDN uses either IP address or some dummy FQDN as the Host header fetching content from the origin. Now, the origin may wish to know what was the Host header (aka website name) the content is asked for. In my case, one origin served 2 websites.

Solution 7 - Http

Another scenario, you license your app to a host URL then you want to load balance across n > 1 servers.

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
QuestionBenMorelView Question on Stackoverflow
Solution 1 - HttpAstraView Answer on Stackoverflow
Solution 2 - HttpXantrulView Answer on Stackoverflow
Solution 3 - HttpCarlos VerdesView Answer on Stackoverflow
Solution 4 - HttpguestView Answer on Stackoverflow
Solution 5 - HttpRy-View Answer on Stackoverflow
Solution 6 - HttpwickView Answer on Stackoverflow
Solution 7 - HttpColWhiView Answer on Stackoverflow