🤔 Introducing APISIX AI Gateway – Built for LLMs and AI workloads. Learn More

Global Rules

Description

A Global Rule configures Plugins for incoming requests independently of Plugins bound to a Route, Service, Consumer, or Plugin Config. Rate-limiting and observability Plugins are often enabled globally to apply consistent behavior across APIs.

Global Rule Plugins run before locally bound Plugins within the same phase. The complete order is global rewrite, local rewrite, global access, then local access. A global access handler therefore does not run before a local rewrite handler. Priorities order Plugins within each phase and scope, and an early response prevents later handlers from running. See Plugins execution order for details.

Example

The example below shows how you can use the limit-count Plugin on all requests:

NOTE

You can fetch the admin_key from config.yaml and save to an environment variable with the following command:

admin_key=$(yq '.deployment.admin.admin_key[0].key' conf/config.yaml | sed 's/"//g')
curl -X PUT \
  http://{apisix_listen_address}/apisix/admin/global_rules/1 \
  -H 'Content-Type: application/json' \
  -H "X-API-KEY: $admin_key" \
  -d '{
        "plugins": {
            "limit-count": {
                "time_window": 60,
                "policy": "local",
                "count": 2,
                "key": "remote_addr",
                "rejected_code": 503
            }
        }
    }'

You can also list all the Global rules by making this request with the Admin API:

curl http://{apisix_listen_address}/apisix/admin/global_rules -H "X-API-KEY: $admin_key"