# 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 the `Host` header from the client request, or, if the `Host` 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's `server_name` or `listen` 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 the `Host` 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 the `Host` header as it was received from the client request. It directly corresponds to the `Host` 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 exact `Host` 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 the `Host` header is not present.
    - `$http_host`: Directly reflects the value of the `Host` header; no fallback.
- **Use Cases:**
    - `$host`: General usage when you want to handle requests based on the `Host` header or default server name.
    - `$http_host`: When you need to log or use the exact value of the `Host` 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 is `example.com`, `$host` will be `example.com`.
    - If the `Host` header is missing, `$host` will be the server's `server_name` (e.g., `localhost` or `default_server`).
- **With `$http_host`:**
    - If the `Host` header is `example.com`, `$http_host` will be `example.com`.
    - If the `Host` header is missing or empty, `$http_host` will be empty.

**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 be `example.com` or the fallback server name.
- `X-Http-Host` will be `example.com` or empty if the `Host` header is missing.

### **Summary**

- **`$host`**: Represents the `Host` header value or server name if the header is missing. Useful for general server configuration and fallback scenarios.
- **`$http_host`**: Represents the exact `Host` 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.