nginx built-in variables
nginx exposes a large set of $variables you can reference in directives like
log_format, proxy_set_header, return, map and if. They expose request
metadata, proxy and upstream details, and TLS information. This reference groups
the common ones by source module and explains each.
How it works
Variables are evaluated per request. Some are fixed names ($remote_addr,
$status); others follow a dynamic pattern where part of the name is supplied
by you:
log_format main '$remote_addr "$request" $status '
'$body_bytes_sent "$http_user_agent" '
'rt=$request_time urt=$upstream_response_time';
location /api/ {
proxy_set_header Host $host;
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
proxy_pass http://backend;
}
The dynamic prefixes are $http_NAME (request headers), $sent_http_NAME
(response headers), $arg_NAME (query arguments) and $cookie_NAME (cookies),
where NAME is lowercased with hyphens replaced by underscores.
Tips and notes
$request_urikeeps the query string and original form;$uriis normalised and rewrite-aware.- Use
$proxy_add_x_forwarded_forto correctly append the client IP when chaining proxies. - Upstream timing variables (
$upstream_response_time,$upstream_connect_time) are great for latency logs. - The
$ssl_family only has values on HTTPS server blocks with TLS negotiated.