How to get original Host header when proxying request

Hi @adrm!

You should not rely on that information. We don’t AUTOMATICALLY provide it in any reliable way even if you happen to find it working today in X-Forwarded-For or Referer. The Referer header is I think from the visitor’s browser anyway (and is also the location of the page that linked to the proxy’d URL, AFAIK - so it might change based on where people link to your site from!), and is not from Netlify’s servers.

You’d need to set some specific header that shows the name which your API can examine and use. This would be set at BUILD time for use later, by using the headers configuration attribute in your netlify.toml specified proxy rules. At a guess you could use this with your setup by creating several different /api redirects: /api-host1/*, /api-host2/*`, for which you set different headers. More docs on the topic:

You’d want something like this:

[[redirects]]
  from = "/api-host1/*"
  to = "https://remotehost/api/:splat"
  status = 200
  headers = {X-Netlify-Hostname: "host1.com"}
[[redirects]]
  from = "/api-host2/*"
  to = "https://remotehost/api/:splat"
  status = 200
  headers = {X-Netlify-Hostname: "host2.com"}
2 Likes