$http_*

The variable $http_ holds the value of a specific HTTP header from the client request, prefixed by 'http_'. — NGINX Core (HTTP)

$http_* NGINX Core (HTTP)

Description

In NGINX, the variable "$http_" is a dynamic prefix for accessing HTTP request headers sent by the client. The syntax for this variable is "$http_", where `` is converted to lowercase, and any dashes are replaced with underscores. For example, if the client sends a header `X-Forwarded-For`, you can access its value using `$http_x_forwarded_for`. This functionality enables NGINX to handle various headers conveniently, providing easy access to any HTTP header with a standard naming pattern. When a client makes a request to the server, all HTTP headers are available in the request context, and NGINX populates the `$http_` variables accordingly during the processing of the request. These variables can be utilized across various contexts in the configuration file, such as in `server`, `location`, and `if` blocks. Common headers accessed through this variable include authorization headers, custom application headers, and standard headers like `User-Agent` or `Accept-Encoding`, allowing for flexible request manipulation or conditional processing based on the presence or value of certain headers.

Config Example

server {
    listen 80;
    location /example {
        if ($http_user_agent ~* "Googlebot") {
            return 403;
        }
        add_header X-Your-Header "$http_x_your_header";
    }
}

Subsystem

http

Cacheable

Yes

Type

Prefix variable

Contexts

http, server, location, if

Remember to convert the header name to lowercase and replace dashes with underscores when forming the variable name, as in `$http_x_forwarded_for`.

Ensure that the headers are present in the client request; accessing a non-existent header will simply yield an empty string.

Use the correct context for this variable to ensure it behaves as expected. It might not work as intended if invoked in the wrong context.