PHP: $_SERVER variables: $_SERVER['HTTP_HOST'] vs $_SERVER['SERVER_NAME']

PHP: $_SERVER variables: $_SERVER['HTTP_HOST'] vs $_SERVER['SERVER_NAME']

  1. Blog
  2. 15 November 2026
  3. 3 min read

Stefan Bogdanescu

Founder & Senior Architect

PHP: $_SERVER variables: $_SERVER['HTTP_HOST'] vs $_SERVER['SERVER_NAME']

When debugging a request or configuring a virtual host, you will frequently encounter two specific entries in the $_SERVER superglobal that appear identical at first glance: $_SERVER['HTTP_HOST'] and $_SERVER['SERVER_NAME']. While they often resolve to the same string, their origins are distinct, and understanding the difference is crucial for building robust applications, particularly when dealing with load balancers or reverse proxies.

The fundamental distinction lies in where the data comes from. $_SERVER['HTTP_HOST'] is populated from the HTTP request header sent by the client browser. Specifically, it parses the value of the Host header defined in RFC 7230. Conversely, $_SERVER['SERVER_NAME'] is a server-side variable set by the web server software itself (like Apache or Nginx) based on its internal configuration files.

This difference becomes critical when your application sits behind a reverse proxy, such as AWS Application Load Balancer (ALB), Cloudflare, or Traefik. In these scenarios, the client connects to the public DNS name of the load balancer, while the actual backend server has a different internal hostname. If you rely solely on $_SERVER['HTTP_HOST'], your application will see the public load balancer's domain. However, if you are inspecting $_SERVER['SERVER_NAME'] and the proxy strips or modifies headers, you might encounter discrepancies.

Consider a typical setup where a user accesses https://myapp.example.com. The browser sends a Host: myapp.example.com header.

  • If the server is running directly, HTTP_HOST will be myapp.example.com, and SERVER_NAME (configured in the Apache/Nginx virtual host block) will also match this value.
  • If the traffic passes through a proxy configured to forward requests as-is, both variables usually align.

However, complications arise with SSL termination or specific proxy configurations. Some proxies might strip the original Host header and replace it with their own internal hostname if not explicitly forwarded, or they might pass the upstream host in a different header (like X-Forwarded-Host). In these cases, HTTP_HOST reflects what the immediate preceding server received, which might differ from the intended application domain.

When configuring frameworks like Laravel, this distinction impacts how you construct redirects and base URLs. If you are implementing a middleware that forces HTTPS or rewrites paths, relying on $_SERVER['HTTP_HOST'] is generally safer because it represents the URL the user actually requested. Relying on $_SERVER['SERVER_NAME'] can lead to logic errors if the server configuration does not perfectly mirror the incoming request headers.

A common pitfall occurs during local development behind a tunnel like ngrok or localtunnel. These tools often inject their own domain into the Host header. If your code assumes SERVER_NAME is the canonical source of truth for the domain, you might inadvertently serve content from the tunnel's subdomain instead of your local host.

To handle this correctly, many modern stacks prefer checking HTTP_HOST first and falling back to environment variables or server configuration if needed. For example:

$host = $_SERVER['HTTP_HOST'] ?? null;

if ($host === null) {
    $host = getenv('SERVER_NAME');
}

This pattern ensures that the application respects the client's intent while providing a sensible default for direct server requests. Always verify your reverse proxy settings (such as proxy_set_header Host in Nginx or RequestHeader host pass in Apache) to ensure the Host header is preserved consistently across the infrastructure chain. Ignoring this nuance can result in broken redirects, incorrect canonical URLs, and security vulnerabilities where an attacker might exploit misconfigured header handling.

Our network