User Manual

This manual provides detailed information on Apache APISIX core features and usage, including route configuration, upstream management, load balancing, rate limiting, authentication, and more.

Basic Concepts

Route

A route is the path to access upstream services. In APISIX, a route first matches client requests using predefined rules, then loads and executes corresponding plugins, and finally forwards the request to the specific upstream service.

The simplest route consists only of a matching path and upstream address.

Upstream

An upstream is a collection of nodes with the same functionality, an abstraction of virtual hosts. Upstreams can perform load balancing across multiple service nodes using predefined rules.

Consumer

A consumer is an application or developer using the API. In APISIX, a consumer requires a globally unique name and must be bound to an authentication plugin.

Plugin

APISIX provides a rich plugin system for extending gateway functionality. Plugins can be configured at route, consumer, or global levels.

Configure Routes

You can easily create routes through the Admin API. The following example creates a route that forwards requests to httpbin.org:

curl -i "http://127.0.0.1:9180/apisix/admin/routes" -X PUT -d '
{
  "id": "getting-started-ip",
  "uri": "/ip",
  "upstream": {
    "type": "roundrobin",
    "nodes": {
      "httpbin.org:80": 1
    }
  }
}'

Successful creation returns HTTP/1.1 201 Created.

Verify Route

curl "http://127.0.0.1:9080/ip"

This returns:

{
  "origin": "183.94.122.205"
}

Route Matching Rules

APISIX supports multiple route matching methods:

URI Matching:

{
  "uri": "/api/v1/*",
  "uri": ["/api/v1/*", "/api/v2/*"]
}

Host Matching:

{
  "host": "example.com",
  "host": ["example.com", "*.example.com"]
}

Method Matching:

{
  "methods": ["GET", "POST"]
}

Header Matching:

{
  "headers": {
    "Content-Type": "application/json",
    "X-API-Key": "*"
  }
}

Load Balancing

APISIX supports multiple load balancing algorithms including roundrobin, weighted roundrobin, and least connections.

Configure Weighted Load Balancing

Create a route with multiple upstream services:

curl -i "http://127.0.0.1:9180/apisix/admin/routes" -X PUT -d '
{
  "id": "load-balance-route",
  "uri": "/headers",
  "upstream": {
    "type": "roundrobin",
    "nodes": {
      "httpbin.org:443": 1,
      "mock.api7.ai:443": 1
    },
    "pass_host": "node",
    "scheme": "https"
  }
}'

Parameters:

  • type: Load balancing algorithm - roundrobin, weighted_roundrobin, or least_conn
  • nodes: Upstream service nodes in format {"host:port": weight}
  • pass_host: Set to node to pass request headers to upstream
  • scheme: Upstream protocol, http or https

Verify Load Balancing

Generate 100 requests to test balancing:

hc=$(seq 100 | xargs -I {} curl "http://127.0.0.1:9080/headers" -sL | grep "httpbin" | wc -l); echo httpbin.org: $hc, mock.api7.ai: $((100 - $hc))

Requests are nearly evenly distributed between the two upstream services.

Rate Limiting

APISIX provides flexible rate limiting through the limit-count plugin.

Enable Rate Limiting

Add the limit-count plugin to an existing route:

curl -i "http://127.0.0.1:9180/apisix/admin/routes/getting-started-ip" -X PATCH -d '
{
  "plugins": {
    "limit-count": {
      "count": 2,
      "time_window": 10,
      "rejected_code": 503
    }
  }
}'

Parameters:

  • count: Maximum requests allowed in time window
  • time_window: Time window size in seconds
  • rejected_code: HTTP status code returned when limit is exceeded

This configuration limits incoming traffic to a maximum of 2 requests per 10 seconds.

Verify Rate Limiting

Generate 100 requests to test:

count=$(seq 100 | xargs -I {} curl "http://127.0.0.1:9080/ip" -I -sL | grep "503" | wc -l); echo "200": $((100 - count)), "503": $count

Result: 2 requests succeed (200), 98 requests rejected (503).

Disable Rate Limiting

curl -i "http://127.0.0.1:9180/apisix/admin/routes/getting-started-ip" -X PATCH -d '
{
  "plugins": {
    "limit-count": {
      "_meta": {
        "disable": true
      }
    }
  }
}'

Advanced Rate Limiting

Using Variables:

{
  "plugins": {
    "limit-count": {
      "count": 100,
      "time_window": 60,
      "key": "remote_addr"
    }
  }
}

Using Redis Cluster:

{
  "plugins": {
    "limit-count": {
      "count": 100,
      "time_window": 60,
      "key": "remote_addr",
      "policy": "redis",
      "redis_host": "127.0.0.1",
      "redis_port": 6379,
      "redis_password": "",
      "redis_database": 1
    }
  }
}

Authentication

APISIX supports multiple authentication methods including Key Auth, Basic Auth, JWT, OAuth2, and more.

Key Authentication

Create Consumer

curl -i "http://127.0.0.1:9180/apisix/admin/consumers" -X PUT -d '
{
  "username": "tom",
  "plugins": {
    "key-auth": {
      "key": "secret-key"
    }
  }
}'

⚠️ Use complex keys in production.

Enable Key Authentication for Route

curl -i "http://127.0.0.1:9180/apisix/admin/routes/getting-started-ip" -X PATCH -d '
{
  "plugins": {
    "key-auth": {}
  }
}'

Verify Authentication

Request without key:

curl -i "http://127.0.0.1:9080/ip"

Returns: HTTP/1.1 401 Unauthorized

Request with wrong key:

curl -i "http://127.0.0.1:9080/ip" -H 'apikey: wrong-key'

Returns: HTTP/1.1 401 Unauthorized

Request with correct key:

curl -i "http://127.0.0.1:9080/ip" -H 'apikey: secret-key'

Returns: HTTP/1.1 200 OK

Other Authentication Methods

JWT Authentication:

{
  "plugins": {
    "jwt-auth": {
      "key": "user-key",
      "secret": "my-secret-key"
    }
  }
}

Basic Authentication:

{
  "plugins": {
    "basic-auth": {
      "hide_credentials": false
    }
  }
}

HMAC Authentication:

{
  "plugins": {
    "hmac-auth": {
      "secret_key": "my-secret-key"
    }
  }
}

Service Governance

Gray Release

APISIX supports weight-based gray release and canary releases:

curl -i "http://127.0.0.1:9180/apisix/admin/routes" -X PUT -d '
{
  "id": "canary-route",
  "uri": "/api/*",
  "upstream": {
    "type": "weighted_roundrobin",
    "nodes": [
      {"host": "v1.example.com", "port": 80, "weight": 90},
      {"host": "v2.example.com", "port": 80, "weight": 10}
    ]
  }
}'

Request Rewriting

Use the proxy-rewrite plugin to rewrite requests:

curl -i "http://127.0.0.1:9180/apisix/admin/routes" -X PUT -d '
{
  "id": "rewrite-route",
  "uri": "/old-path/*",
  "plugins": {
    "proxy-rewrite": {
      "uri": "/new-path/$uri",
      "headers": {
        "X-New-Header": "value"
      }
    }
  },
  "upstream": {
    "nodes": {
      "backend.example.com:80": 1
    }
  }
}'

Response Caching

Use the proxy-cache plugin to cache upstream responses:

{
  "plugins": {
    "proxy-cache": {
      "cache_key": ["uri", "query_string"],
      "cache_http_status": [200, 301, 404],
      "cache_ttl": 60
    }
  }
}

Plugin Configuration

Plugin Execution Order

Specify plugin execution phases using phase in plugin configuration:

  • rewrite: Request rewrite phase
  • access: Access control phase
  • before_proxy: Pre-proxy phase
  • header_filter: Response header filter phase
  • body_filter: Response body filter phase
  • log: Log phase

Disable Plugin

Set _meta.disable to true:

curl -i "http://127.0.0.1:9180/apisix/admin/routes/route-id" -X PATCH -d '
{
  "plugins": {
    "limit-count": {
      "_meta": {
        "disable": true
      }
    }
  }
}'

Monitoring and Observability

Integrate Prometheus

{
  "plugins": {
    "prometheus": {
      "prefer_name": true
    }
  }
}

Access metrics:

curl "http://127.0.0.1:9091/prometheus"

Logging

APISIX supports multiple log outputs:

HTTP Logger:

{
  "plugins": {
    "http-logger": {
      "uri": "http://log-server:8080/logs"
    }
  }
}

Error Logger:

{
  "plugins": {
    "error-log-logger": {
      "level": "ERROR",
      "uri": "http://log-server:8080/logs"
    }
  }
}

Best Practices

  1. Use meaningful IDs: Use descriptive IDs for routes, upstreams, etc.
  2. Set appropriate timeouts: Configure reasonable timeouts based on upstream response times
  3. Enable health checks: Configure active and passive health checks for critical services
  4. Configure appropriate rate limits: Set rate limits based on business needs and server capacity
  5. Enable authentication: Must enable authentication in production
  6. Use TLS: Use HTTPS for externally exposed interfaces
  7. Monitor key metrics: Configure Prometheus monitoring and set alerts