Published on

NGINX $upstream_http_name

In Nginx, the variable:

$upstream_http_<name>

is used to access a response header returned by the upstream server (backend).

So specifically:

$upstream_http_name

means:

👉 “The value of the Name HTTP response header sent by the upstream server.”


🔹 How It Works

When Nginx proxies a request (using proxy_pass, fastcgi_pass, etc.), the backend returns response headers like:

HTTP/1.1 200 OK
Content-Type: application/json
X-Request-ID: abc123
Name: my-service

You can access those headers in Nginx using:

$upstream_http_content_type
$upstream_http_x_request_id
$upstream_http_name

Rules:

  • Header names are lowercased
  • Dashes (-) become underscores (_)
  • Prefixed with upstream_http_

🔹 Example Usage

1️⃣ Logging an upstream header

log_format custom '$remote_addr - $upstream_http_name';

access_log /var/log/nginx/access.log custom;

2️⃣ Passing upstream header to client

location / {
    proxy_pass http://backend;

    add_header X-Backend-Name $upstream_http_name;
}

3️⃣ Conditional logic

if ($upstream_http_name = "internal-service") {
    return 403;
}

🔹 Important Notes

  • It only works after a successful upstream response

  • If the upstream doesn’t return the header → variable is empty

  • Works with:

    • proxy_pass
    • fastcgi_pass
    • uwsgi_pass
    • grpc_pass

VariableMeaning
$http_nameRequest header from client
$sent_http_nameResponse header sent by Nginx to client
$upstream_http_nameResponse header from upstream