What's the Difference of $Host and $Http_host in Nginx
Better Stack Team
Updated on October 7, 2024
In Nginx, $host and $http_host are variables used to represent different aspects of the HTTP request, specifically related to the Host header. While they may seem similar, they serve slightly different purposes and can be used in different contexts.
1. $host Variable
Definition:
- The
$hostvariable represents the value of theHostheader from the client request, or, if theHostheader is not present, it will default to the server name specified in the Nginx configuration.
Behavior:
- If the
Hostheader is present:$hostwill contain the value of this header. - If the
Hostheader is not present:$hostwill fall back to the server'sserver_nameorlistendirective’s IP address or domain.
Use Case:
$hostis often used to construct URLs or to handle requests based on the requested host name. It's useful in situations where you want to ensure that your server handles requests based on theHostheader, including cases where the header might be missing or malformed.
Example:
server {
listen 80;
server_name example.com;
location / {
# Logs the value of the Host header or the default server_name
access_log /var/log/nginx/access.log '$host';
}
}
2. $http_host Variable
Definition:
- The
$http_hostvariable specifically represents the value of theHostheader as it was received from the client request. It directly corresponds to theHostheader value in the HTTP request.
Behavior:
- Always contains the raw
Hostheader value from the client request. - No fallback: Unlike
$host,$http_hostdoes not have a fallback mechanism. It is strictly the header value sent by the client.
Use Case:
$http_hostis useful when you need to work with the exactHostheader value as sent by the client, without any modification or fallback. This is particularly relevant when dealing with multiple virtual hosts or when debugging issues related to header values.
Example:
server {
listen 80;
location / {
# Logs the exact value of the Host header sent by the client
access_log /var/log/nginx/access.log '$http_host';
}
}
Key Differences
- Fallback Mechanism:
$host: Will fall back to the server name or IP if theHostheader is not present.$http_host: Directly reflects the value of theHostheader; no fallback.
- Use Cases:
$host: General usage when you want to handle requests based on theHostheader or default server name.$http_host: When you need to log or use the exact value of theHostheader sent by the client.
Practical Example
Consider a request where the Host header is set to example.com:
- With
$host:- If the
Hostheader isexample.com,$hostwill beexample.com. - If the
Hostheader is missing,$hostwill be the server'sserver_name(e.g.,localhostordefault_server).
- If the
- With
$http_host:- If the
Hostheader isexample.com,$http_hostwill beexample.com. - If the
Hostheader is missing or empty,$http_hostwill be empty.
- If the
Configuration Example:
server {
listen 80;
server_name example.com;
location / {
# Example of using $host and $http_host
add_header X-Host '$host';
add_header X-Http-Host '$http_host';
}
}
In this configuration:
X-Hostwill beexample.comor the fallback server name.X-Http-Hostwill beexample.comor empty if theHostheader is missing.
Summary
$host: Represents theHostheader value or server name if the header is missing. Useful for general server configuration and fallback scenarios.$http_host: Represents the exactHostheader value received from the client. Useful for precise logging or when you need the raw header value.
By understanding the differences between $host and $http_host, you can more accurately configure and debug Nginx to handle HTTP requests as per your requirements.
Got an article suggestion?
Let us know