Nginx Variables: Read Request Metadata
Read a value from the current request
A variable is a named value that Nginx can read while processing a request. A name such as $request_method refers to metadata for this request, not to an operating-system environment variable. Directives decide whether they accept variable expressions. For example, return can build a response body from variables, but listen needs a listener configured before requests arrive. The following fragment belongs inside a lab server block.
location = /inspect { default_type text/plain; return 200 "method=$request_method scheme=$scheme\n";}For a GET to the plain HTTP listener, expect method=GET scheme=http followed by a newline. A POST changes the method value; HTTPS changes the scheme when TLS terminates here. This example does not need an upstream. It is not a shell command.
Three different meanings of host
$http_host comes from the incoming Host field, which may contain a port. $host follows a precedence rule: request-target hostname first, then Host, then the selected server name. $server_name identifies the server that accepted the request. They need not be equal. Neither client-controlled hostname nor a forwarded header is evidence of identity.
Configured server_name: studio.test www.studio.testGET /inspect HTTP/1.1Host: www.studio.test:18141
$http_host = www.studio.test:18141$host = www.studio.test$server_name = studio.testThe alias matches the configured virtual server, while the configured primary name is studio.test. Avoid constructing external redirect destinations from unvalidated host fields. Use a configured canonical destination instead.
Original target versus current path
GET /docs/start?lang=en HTTP/1.1Host: studio.test
$request_uri = /docs/start?lang=en$uri = /docs/start$args = lang=en$request_uri preserves the original URI including its query. $uri is the current normalized path and can change during internal routing. $args contains the current query arguments and can also be modified; $arg_lang retrieves the named argument in this example. Do not copy entire query strings into operational logs: tokens or personal data may appear there.
Headers, network identity and correlation
X-Lab-Theme: paperX-Forwarded-For: 192.0.2.9
$http_x_lab_theme = paper$http_x_forwarded_for = 192.0.2.9$remote_addr = 127.0.0.1 (direct local connection)Header names become lowercase and hyphens become underscores in the $http_ family. An absent header generally contributes an empty value in an expression; log formatting may represent unavailable values differently. $remote_addr is the client address as Nginx knows it; trusted real-IP configuration can change it. The forwarded header in this direct fixture is just a claim, not the peer address. Do not use it as authentication.
log_format lab_meta escape=json '{"id":"$request_id","method":"$request_method",' '"status":"$status","time":"$time_iso8601"}';Attach this format with access_log inside a lab server. $request_id (available since 1.11.0) is a generated hexadecimal identifier; it is useful for correlation, not an access token. $time_iso8601 records local time with an offset; $msec represents time in epoch seconds with millisecond precision. Neither should have a fixed value in an assertion. $upstream_addr can describe several attempted backends or a group name when selection fails. A local return has no contacted backend, so do not expect an upstream address on every request.
Use variables without exposing private data
Prefer logging selected metadata over cookies, authorization headers and complete URLs. For practice, predict the host, path, method and scheme from the synthetic examples, then compare your answers to the manual exercise. Next, turn uncontrolled input into a small set of approved labels with map.
Check Your Understanding
Solidify your learning with the interactive assessment directly below this lesson.
Concept Quiz & Questions
Request metadata
Choose the best answer for the chapter examples.
Which variable includes the original query string?
Host is www.studio.test:18141 and primary server name is studio.test. Which variable preserves the header with its port?
A direct local request contains X-Forwarded-For: 192.0.2.9. With no real-IP override, what is the peer address?
Which variable is best for correlating a request across logs?
Why might $upstream_addr be unavailable for a request?