By glarrain

2013-03-14 16:25:54 8 Comments

In Nginx, what's the difference between variables $host and $http_host.


@glarrain 2013-03-14 16:25:54

$host is a variable of the Core module.


This variable is equal to line Host in the header of request or name of the server processing the request if the Host header is not available.

This variable may have a different value from $http_host in such cases: 1) when the Host input header is absent or has an empty value, $host equals to the value of server_name directive; 2)when the value of Host contains port number, $host doesn't include that port number. $host's value is always lowercase since 0.8.17.

$http_host is also a variable of the same module but you won't find it with that name because it is defined generically as $http_HEADER (ref).


The value of the HTTP request header HEADER when converted to lowercase and with 'dashes' converted to 'underscores', e.g. $http_user_agent, $http_referer...;


  • $http_host equals always the HTTP_HOST request header.
  • $host equals $http_host, lowercase and without the port number (if present), except when HTTP_HOST is absent or is an empty value. In that case, $host equals the value of the server_name directive of the server which processed the request.

@Jonathan Vanasco 2013-03-14 16:36:38

$host is specifically the first server_name that is defined in the current server block. if you have multiple server_names, only the first one will appear.

@glarrain 2013-03-14 16:41:20

True. In fact, it is quite typical to define: server_name;

@Jonathan Vanasco 2013-03-14 17:09:35

you can have multiple server_name directives too. if you happen to have a regex in the first one, that becomes the $host , and all sorts of ugly stuff can happen during rewrite rules.

@CMCDragonkai 2014-03-13 20:03:06

Does the $server_name variable equal the server_name directive's value or the actual server name that was selected if there were multiple server_name directives?

@ATLief 2019-04-02 14:23:59

@CMCDragonkai $server_name is always equal to the first value specified with the server_name directive. For example, with server_name;, $server_name will always be "", regardless of which host the user has specified. In fact, if you don't have a default_server, the host might be something completely different (like

Related Questions

Sponsored Content

15 Answered Questions

9 Answered Questions

[SOLVED] What's the difference between Cache-Control: max-age=0 and no-cache?

18 Answered Questions

7 Answered Questions

[SOLVED] Nginx -- static file serving confusion with root & alias

  • 2012-05-17 08:03:01
  • treecoder
  • 203165 View
  • 396 Score
  • 7 Answer
  • Tags:   nginx

11 Answered Questions

[SOLVED] Node.js + Nginx - What now?

  • 2011-02-15 20:49:02
  • Van Coding
  • 331703 View
  • 941 Score
  • 11 Answer
  • Tags:   node.js nginx concept

15 Answered Questions

4 Answered Questions

[SOLVED] Why does Unicorn need to be deployed together with Nginx?

1 Answered Questions

[SOLVED] Practical difference of $http_host and $host

  • 2016-10-11 06:41:55
  • Markus
  • 589 View
  • 0 Score
  • 1 Answer
  • Tags:   nginx proxy

3 Answered Questions

[SOLVED] Proper MIME media type for PDF files

1 Answered Questions

[SOLVED] Nginx Different Domains on Same IP

  • 2012-08-02 08:18:32
  • berkayk
  • 97081 View
  • 117 Score
  • 1 Answer
  • Tags:   nginx dns

Sponsored Content