NGINX is a powerful web server used by millions of website. It provides many useful configuration features that support simple to complex websites. $host, $http_host and $server_name are 3 such NGINX variables that are used in almost every NGINX server configuration. While they appear to be similar, there are some key differences that you must know about them. Let us take a look at the difference between NGINX $host vs $server_name.
NGINX $host vs $server_name
Here’s a quick difference between NGINX $host vs $server_name. According to NGINX documentation,
- $host contains either host name from the request, or host name from the ‘HOST’ request header field, or the server_name that processes the request. Its value is different from $http_host if the latter contains a port number, or if it is absent or has an empty value. It can also be different if $host uses $server_name value.
- $http_host contains the value HTTP HOST header field, if it is present in the request
- $server_name is the server_name of the virtual host that processed the request. If your server configuration contains multiple values for server_name only the 1st value will be present.
Sometimes, the user agent may send host name in the request line, instead of request header. So, as you can see, if you want the host name at any cost, then always use $host variable since it will contain some value or the other, no matter how the user agent behaves.
If you are getting requests from old HTTP 1.0/ requests or legacy software, then the user agent may not even send the hostname in the request. Sometimes, you may be running multiple websites on a single server. In all such cases, send these requests to a catch all virtual host, with a proper server name and then use it for your purpose. Here are the steps to create a catch all virtual host in NGINX.