The real difference between $host and $http_host

Maxim Dounin mdounin at
Mon Aug 15 10:19:38 UTC 2011


On Mon, Aug 15, 2011 at 03:30:20AM -0400, speedfirst wrote:

> The wiki page of "$host" says $host and $host_addr are different only
> when there is no "Host" header or the "Host" header is empty. But I
> found when "Host" contains port number, $host never contains the port
> number while $http_host is equal to the value of "Host" header.
> That is, if "Host: foo:8080", then
> $http_host = foo:8080
> $host = foo
> So in this case, they are different.
> Tested in nginx 1.0.3
> I want the official confirmation of this difference.

Yes, $host variable represents host part of a Host header[1].  
Moreover, in 0.8.17+ $host will be additionally lowercased.  That 
is, for

    Host: FOO:8080

$host will be "foo" (while $http_host will be "FOO:8080" as it 
just reflects the whole header).

Additionally, $host will be set to one from request uri, if 
request request used absolute uri (as per RFC2616, section 5.2, 
see [2]).

> And if it's
> confirmed, could you correct the illustration in wiki page?

Probably wiki as well as docs should say something like "$host 
represents http host requested".  The full description above is 
clearly a bit too many for just a variable description.

Anyway, feel free to update wiki if you think you know what to 
write there.


Maxim Dounin

More information about the nginx mailing list