What's the Difference of $Host and $Http_host in Nginx
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
$host
variable represents the value of theHost
header from the client request, or, if theHost
header is not present, it will default to the server name specified in the Nginx configuration.
Behavior:
- If the
Host
header is present:$host
will contain the value of this header. - If the
Host
header is not present:$host
will fall back to the server'sserver_name
orlisten
directive’s IP address or domain.
Use Case:
$host
is 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 theHost
header, 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_host
variable specifically represents the value of theHost
header as it was received from the client request. It directly corresponds to theHost
header value in the HTTP request.
Behavior:
- Always contains the raw
Host
header value from the client request. - No fallback: Unlike
$host
,$http_host
does not have a fallback mechanism. It is strictly the header value sent by the client.
Use Case:
$http_host
is useful when you need to work with the exactHost
header 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 theHost
header is not present.$http_host
: Directly reflects the value of theHost
header; no fallback.
- Use Cases:
$host
: General usage when you want to handle requests based on theHost
header or default server name.$http_host
: When you need to log or use the exact value of theHost
header sent by the client.
Practical Example
Consider a request where the Host
header is set to example.com
:
- With
$host
:- If the
Host
header isexample.com
,$host
will beexample.com
. - If the
Host
header is missing,$host
will be the server'sserver_name
(e.g.,localhost
ordefault_server
).
- If the
- With
$http_host
:- If the
Host
header isexample.com
,$http_host
will beexample.com
. - If the
Host
header is missing or empty,$http_host
will 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-Host
will beexample.com
or the fallback server name.X-Http-Host
will beexample.com
or empty if theHost
header is missing.
Summary
$host
: Represents theHost
header value or server name if the header is missing. Useful for general server configuration and fallback scenarios.$http_host
: Represents the exactHost
header 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.
Make your mark
Join the writer's program
Are you a developer and love writing and sharing your knowledge with the world? Join our guest writing program and get paid for writing amazing technical guides. We'll get them to the right readers that will appreciate them.
Write for usBuild on top of Better Stack
Write a script, app or project on top of Better Stack and share it with the world. Make a public repository and share it with us at our email.
community@betterstack.comor submit a pull request and help us build better products for everyone.
See the full list of amazing projects on github