Skip to main content
Version: 3.21.0

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_addr
  • remote_addr
  • request_uri
  • server_name
  • uri
  • http_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 NameDescription
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_ipUpstream server IP.
balancer_portUpstream server port.
consumer_nameConsumer username.
consumer_group_idConsumer group ID.
graphql_nameGraphQL operation name.
graphql_operationGraphQL operation type.
graphql_root_fieldsGraphQL root fields.
rate_limiting_infoJSON rate-limiting evaluation result. Empty before a supported rate-limiting plugin runs. See Rate Limiting Information.
route_idRoute ID.
route_nameRoute name.
service_idService ID.
service_nameService name.
upstream_unresolved_hostHost 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_bodyHTTP response body.
request_typeRequest category used by observability plugins, such as traditional_http, ai_chat, or ai_stream. A successful WebSocket upgrade is classified as websocket.
mqtt_client_idClient ID in MQTT protocol.
redis_cmd_lineRedis command.
rpc_timeRPC 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:

MechanismWhen it is registeredUse when
Custom Lua startup hookAPISIX starts or reloads with the configured Lua moduleThe variable should be available before requests are handled
Serverless pre-function or post-functionThe configured serverless function runsA route or service should trigger registration
Plugin pre-functionThe plugin runs in a request-processing phaseA 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:

  1. Variables that follow special naming patterns for request arguments, headers, cookies, GraphQL values, and route parameters.
  2. Named APISIX variables, such as route_id, and custom variables registered with core.ctx.register_var.
  3. 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_host and ${http_host}
  • $arg_username-$arg_userid and ${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.

ExampleBehavior
${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.