Built-In Variables
Built-in variables in APISIX are pre-defined variables that can be directly referenced in configurations. They are often used in plugin configurations, route matching, and log customization.
APISIX supports three types of built-in variables:
- NGINX Variables
- APISIX Variables
- Custom Variables
APISIX resolves overlapping variable names according to its evaluation order.
NGINX Variables
NGINX provides a set of variables that can be used to access various request-specific information.
Commonly used variables include:
upstream_addrremote_addrrequest_uriserver_nameurihttp_user_agent
When ext-plugin-post-resp fetches an upstream response, it also populates upstream_addr and upstream_response_time for logging. The response time includes the upstream connection, headers, and body read, but excludes external runner processing and time spent sending the response to the client.
See the complete list of NGINX variables for more information.
In plugin code, ctx.var.request_uri is the normalized path plus the query string. NGINX $request_uri remains the original request target, available to plugins as ctx.var.real_request_uri. See Plugin Request URI Values.
APISIX Variables
In addition to NGINX variables, APISIX offers a variety of built-in variables:
| Variable Name | Description |
|---|---|
post_arg_* | HTTP POST form data when the content type is application/x-www-form-urlencoded. The asterisk is to be replaced with the actual name of the POST form data. |
post_arg.* | HTTP POST body parameter when the content type is application/json, application/x-www-form-urlencoded, or multipart/form-data. The asterisk is to be replaced with the actual name of the POST parameter. Supports JSON path-like selection, such as post_arg.model.version and post_arg.messages[*].content[*].type. |
arg_* | URL query string. The asterisk is to be replaced with the actual query parameter name. |
uri_param_* | URL parameter when APISIX uses the radixtree_uri_with_parameter router. The asterisk is to be replaced with the actual query parameter name. |
http_* | HTTP request header. The asterisk is to be replaced with the actual name of the header. |
cookie_* | Request cookie. The asterisk is to be replaced with the actual name of the cookie. |
balancer_ip | Upstream server IP. |
balancer_port | Upstream server port. |
consumer_name | Consumer username. |
consumer_group_id | Consumer group ID. |
graphql_name | GraphQL operation name. |
graphql_operation | GraphQL operation type. |
graphql_root_fields | GraphQL root fields. |
rate_limiting_info | JSON rate-limiting evaluation result. Empty before a supported rate-limiting plugin runs. See Rate Limiting Information. |
route_id | Route ID. |
route_name | Route name. |
service_id | Service ID. |
service_name | Service name. |
upstream_unresolved_host | Host or IP address of the selected upstream node before DNS resolution. For a node configured as httpbin.org, this variable remains httpbin.org while balancer_ip contains the resolved address. |
resp_body | HTTP response body. |
request_type | Request category used by observability plugins, such as traditional_http, ai_chat, or ai_stream. A successful WebSocket upgrade is classified as websocket. |
mqtt_client_id | Client ID in MQTT protocol. |
redis_cmd_line | Redis command. |
rpc_time | RPC request round-trip time. |
Custom Variables
Custom variables make request-specific values available to plugins when the built-in variables do not cover a use case. Register a variable with core.ctx.register_var; the registered function receives the request context and returns the value when a plugin reads the variable.
Custom variables can customize logging plugin formats or serve as keys in rate-limiting plugins. They cannot be used by features that depend on NGINX directives, such as access_log_format.
Choose a registration mechanism based on how registration should be triggered:
| Mechanism | When it is registered | Use when |
|---|---|---|
| Custom Lua startup hook | APISIX starts or reloads with the configured Lua module | The variable should be available before requests are handled |
| Serverless pre-function or post-function | The configured serverless function runs | A route or service should trigger registration |
| Plugin pre-function | The plugin runs in a request-processing phase | A plugin should trigger registration immediately before it runs |
A startup hook uses extra_lua_path and lua_module_hook. Changes to the module or static configuration require an APISIX reload; reloading plugins through the Admin API does not reload the hook.
See Register Custom Variables for runnable examples of all three mechanisms.
Evaluation Order
APISIX resolves variable names in the following order:
- Variables that follow special naming patterns for request arguments, headers, cookies, GraphQL values, and route parameters.
- Named APISIX variables, such as
route_id, and custom variables registered withcore.ctx.register_var. - NGINX variables.
A custom variable replaces a named APISIX or custom variable registered under the same name. It cannot replace one of the special variable patterns handled by the first group.
Variable Syntax
A valid variable name can include letters, digits, underscores (_), and periods (.).
Escaped variables with a backslash (\) are not treated as variables, for example, \$variable_name.
Simple and Braced Forms
A variable can be referenced in two forms:
$variable_name${variable_name}
Both forms are supported; however, the braced form is required in certain contexts to ensure correct parsing.
The unbraced form $variable is valid when the following character cannot be part of a variable name. For example, the following forms are equivalent:
$http_hostand${http_host}$arg_username-$arg_useridand${arg_username}-${arg_userid}
When a variable is followed by a character that could belong to a variable name, the parser will incorrectly interpret it as a longer variable name. For example, with https://$http_baseurl.com, the parser would treat the entire string http_baseurl.com as the variable, which is incorrect. In this case, you should use the braced form https://${http_baseurl}.com to clearly delimit the variable.
You would also need the braced form if you use the ?? operator.
Default Values with ??
You can specify a default value for a variable using the ?? operator. If the variable is not defined, the value after the operator will be used.
| Example | Behavior |
|---|---|
${http_username ?? anonymous} | If the HTTP request header username is defined, its value is used; otherwise the string anonymous is used. |
${http_count ?? 10} | If the HTTP request header count is defined, its value is used; otherwise the string 10 is used. |