X-Forwarded-Host

Robotecture » HTTP » HTTP Headers » X-Forwarded-Host

X-Forwarded-Host HTTP Header: What You Need to Know

The X-Forwarded-Host HTTP header is a request header used to identify the original host as requested by the client. It is commonly used in debugging, statistics, and generating location-dependent content. This header exposes privacy-sensitive information such as the IP address of the client, so the user’s privacy must be kept in mind when deploying this header.

The X-Forwarded-Host header is often used in conjunction with other headers such as X-Forwarded-For and X-Forwarded-Proto to provide additional information about the client’s request. Improper use of these headers can be a security risk, so it is important to follow best practices when using them. The HTTP Forwarded header is a standardized version of these headers and should be used instead when possible.

What Is the X-Forwarded-Host HTTP Header?

The X-Forwarded-Host (XFH) header is a request-type header that is used to identify the original host requested by the client in the Host HTTP request header. It is part of the HTTP forwarded header mechanism that is used by proxies to forward requests to the destination server. The X-Forwarded-Host header is a de-facto standard header that is widely used in the industry.

The syntax of the X-Forwarded-Host header is simple. It consists of the header name “X-Forwarded-Host” followed by a colon and a space, and then the value of the header, which is the original host requested by the client. For example, “X-Forwarded-Host: example.com”.

The X-Forwarded-Host header is a type of HTTP header, which is a part of the HTTP protocol. HTTP headers are used to provide additional information about a request or a response. They are sent along with the request or the response and are used by the client and the server to exchange information.

The X-Forwarded-Host header is a type of request header, which means that it is sent by the client as part of the request. It is used by the client to inform the proxy or the load balancer about the original host requested by the client. The proxy or the load balancer can then use this information to forward the request to the correct destination server.

Why Is the X-Forwarded-Host HTTP Header Important?

The X-Forwarded-Host HTTP header is an essential tool for identifying the original host in an HTTP request. This header is particularly important when intermediaries such as reverse proxies, CDNs, or load balancers exist between the client and the server. In this section, we will discuss the importance of the X-Forwarded-Host HTTP header, including privacy concerns, trusted reverse proxy, and examples.

Privacy Concerns

The X-Forwarded-Host HTTP header exposes privacy-sensitive information, such as the IP address of the client. Therefore, it is crucial to keep the user’s privacy in mind when deploying this header. Improper use of this header can be a security risk, so it is essential to use it carefully.

Trusted Reverse Proxy

In situations where the client connects indirectly to the server through a trusted reverse proxy, the X-Forwarded-Host HTTP header is essential. This header helps the server identify the original host from which the request was made, even if the request was made through a reverse proxy. The reverse proxy can modify this header to indicate the original host, and the server can use this information to respond appropriately.

Examples

Here are a few examples of when the X-Forwarded-Host HTTP header is useful:

  • When a website is hosted behind a reverse proxy, the X-Forwarded-Host HTTP header can help identify the original host from which the request was made.
  • When a website is hosted on multiple servers, the X-Forwarded-Host HTTP header can help identify the server that should handle the request.
  • When a website is hosted on a CDN or load balancer, the X-Forwarded-Host HTTP header can help identify the original host and ensure that the request is routed to the correct server.

How to Set X-Forwarded-Host HTTP Header

X-Forwarded-Host is an HTTP header used for debugging, statistics, and generating location-dependent content. It allows a trusted reverse proxy to identify the original host requested by an HTTP request. This header is essential when deploying CDNs, load balancers, and reverse proxies.

Parsing X-Forwarded-Host Header

The syntax for the X-Forwarded-Host header is as follows:

X-Forwarded-Host: <host>

The <host> value must be a valid domain name or IP address. If multiple hosts are present, the first one is considered the original host.

It is important to note that X-Forwarded-Host is a non-standard header and should not be confused with the HTTP Forwarded header.

Deploying X-Forwarded-Host Header

To deploy X-Forwarded-Host header, a trusted reverse proxy must be configured to add this header to incoming requests. The header should contain the original hostname requested by the client.

When deploying X-Forwarded-Host header, user’s privacy must be kept in mind. The header exposes privacy-sensitive information, such as the IP address of the client. Therefore, it is crucial to restrict access to this header within the internal network.

Debugging X-Forwarded-Host Header

Debugging X-Forwarded-Host header can be challenging, especially when dealing with multiple reverse proxies and load balancers. It is important to ensure that the header is correctly parsed and forwarded by all intermediate proxies.

If the X-Forwarded-Host header is not correctly parsed, it can result in Forbidden Header Name error or Forwarded Server header being used instead. To avoid this, ensure that the header is added by a trusted reverse proxy and that the IP ranges of the proxy servers are whitelisted.

Examples of X-Forwarded-Host header directives include:

X-Forwarded-Host: www.example.com
X-Forwarded-Host: 192.168.1.1

Other Proxy HTTP Headers

Forwarded

X-Forwarded-For

X-Forwarded-Proto

Via