NGINX is a popular web server used by millions of website around the world. It provides many server variables that can be readily used for NGINX configuration. In this article, we will look at the difference between $host and $http_host in NGINX and when to use them effectively.
Difference between $host and $http_host in NGINX
Here is the key difference between $host and $http_host.
$host is the value of Host in request header, without the port number. If that is not present in request header, then it assumes the value of server_name directive in NGINX configuration. If there are multiple values in server_name directive, then $host will use the first value and ignore the rest.
Also read : How to Test Multiple Variables Against a Value in Python
Also, $host variable is always lower case since NGINX 0.8.17. In other words, it can contain either of the following values in the following order of precedence
- Host name from request line
- Host name from Host request header
- Server name from NGINX configuration
Also read : How to Grep Multiple Strings or Patterns in Linux
$http_host is the value of HTTP_HOST request header. It will include port number if the HTTP_HOST value contains it.
Depending on your requirement, you can use either $host or $http_host server variables for processing. If you want to use only website domain name or IP address use $host. If you also want to use the port number then use $http_host.
Also read : How to Install Laravel in NGINX
Related posts:
Sreeram has more than 10 years of experience in web development, Python, Linux, SQL and database programming.
Error in last sentence:
“If you also want to use the port number then use $host”
Thank you so much for pointing it out. We have updated the post to mention $http_host instead of $host