Salta ai contenuti

Apache APISIX Cheat Sheet

Overview

Apache APISIX is a dynamic, real-time, high-performance cloud-native API gateway built on top of Nginx and OpenResty. It provides rich traffic management features including load balancing, dynamic upstream, canary releases, service discovery, rate limiting, and over 80 built-in plugins for authentication, observability, and traffic control.

APISIX uses etcd as its configuration store, enabling dynamic configuration changes without restarts. It supports gRPC, WebSocket, MQTT, HTTP/2, and HTTP/3 protocols, making it suitable for microservices, Kubernetes ingress, and API management. APISIX also provides a dashboard for visual management and supports custom plugins in Lua, Java, Python, Go, and WebAssembly.

Installation

Docker Compose

git clone https://github.com/apache/apisix-docker.git
cd apisix-docker/example
docker compose up -d

# Components:
# APISIX: localhost:9080 (proxy), localhost:9180 (Admin API)
# etcd: localhost:2379
# APISIX Dashboard: localhost:9000

Helm Chart (Kubernetes)

helm repo add apisix https://charts.apiseven.com
helm repo update

helm install apisix apisix/apisix \
  --namespace apisix --create-namespace \
  --set gateway.type=NodePort \
  --set admin.allow.ipList="{0.0.0.0/0}"

Standalone Binary

# Install dependencies
sudo apt-get install -y libreadline-dev libpcre3-dev libssl-dev

# Install via RPM/DEB
sudo yum install -y apisix
# or
sudo apt-get install -y apisix

# Start
apisix start

# Stop
apisix stop

Admin API

Route Management

# Create a route
curl -X PUT http://127.0.0.1:9180/apisix/admin/routes/1 \
  -H "X-API-KEY: edd1c9f034335f136f87ad84b625c8f1" \
  -d '{
    "uri": "/api/users/*",
    "methods": ["GET", "POST"],
    "upstream": {
      "type": "roundrobin",
      "nodes": {
        "backend1:8080": 1,
        "backend2:8080": 1
      }
    }
  }'

# Get a route
curl http://127.0.0.1:9180/apisix/admin/routes/1 \
  -H "X-API-KEY: edd1c9f034335f136f87ad84b625c8f1"

# List all routes
curl http://127.0.0.1:9180/apisix/admin/routes \
  -H "X-API-KEY: edd1c9f034335f136f87ad84b625c8f1"

# Delete a route
curl -X DELETE http://127.0.0.1:9180/apisix/admin/routes/1 \
  -H "X-API-KEY: edd1c9f034335f136f87ad84b625c8f1"

Upstream Management

# Create an upstream
curl -X PUT http://127.0.0.1:9180/apisix/admin/upstreams/1 \
  -H "X-API-KEY: edd1c9f034335f136f87ad84b625c8f1" \
  -d '{
    "type": "roundrobin",
    "nodes": {
      "backend1:8080": 3,
      "backend2:8080": 2,
      "backend3:8080": 1
    },
    "retries": 3,
    "timeout": {
      "connect": 6,
      "send": 6,
      "read": 6
    },
    "checks": {
      "active": {
        "http_path": "/health",
        "healthy": { "interval": 5, "successes": 2 },
        "unhealthy": { "interval": 3, "http_failures": 3 }
      }
    }
  }'

# Reference upstream in route
curl -X PUT http://127.0.0.1:9180/apisix/admin/routes/1 \
  -H "X-API-KEY: edd1c9f034335f136f87ad84b625c8f1" \
  -d '{
    "uri": "/api/*",
    "upstream_id": "1"
  }'

Plugins

Authentication

# Key Authentication
curl -X PUT http://127.0.0.1:9180/apisix/admin/routes/1 \
  -H "X-API-KEY: edd1c9f034335f136f87ad84b625c8f1" \
  -d '{
    "uri": "/api/*",
    "plugins": {
      "key-auth": {}
    },
    "upstream_id": "1"
  }'

# Create a consumer with API key
curl -X PUT http://127.0.0.1:9180/apisix/admin/consumers/user1 \
  -H "X-API-KEY: edd1c9f034335f136f87ad84b625c8f1" \
  -d '{
    "username": "user1",
    "plugins": {
      "key-auth": {
        "key": "my-api-key-123"
      }
    }
  }'

# JWT Authentication
curl -X PUT http://127.0.0.1:9180/apisix/admin/routes/2 \
  -H "X-API-KEY: edd1c9f034335f136f87ad84b625c8f1" \
  -d '{
    "uri": "/secure/*",
    "plugins": {
      "jwt-auth": {}
    },
    "upstream_id": "1"
  }'

Rate Limiting

curl -X PUT http://127.0.0.1:9180/apisix/admin/routes/1 \
  -H "X-API-KEY: edd1c9f034335f136f87ad84b625c8f1" \
  -d '{
    "uri": "/api/*",
    "plugins": {
      "limit-req": {
        "rate": 100,
        "burst": 50,
        "rejected_code": 429,
        "key_type": "var",
        "key": "remote_addr"
      },
      "limit-count": {
        "count": 1000,
        "time_window": 3600,
        "rejected_code": 429,
        "key": "remote_addr"
      }
    },
    "upstream_id": "1"
  }'

Common Plugins

PluginCategoryDescription
key-authAuthAPI key authentication
jwt-authAuthJWT token validation
basic-authAuthHTTP basic auth
openid-connectAuthOIDC/OAuth2
limit-reqTrafficRequest rate limiting
limit-countTrafficRequest count limiting
limit-connTrafficConnection limiting
proxy-cacheTrafficResponse caching
corsSecurityCORS headers
ip-restrictionSecurityIP whitelist/blacklist
uri-blockerSecurityURI pattern blocking
prometheusObservabilityMetrics export
zipkinObservabilityDistributed tracing
kafka-loggerLoggingLog to Kafka
http-loggerLoggingLog to HTTP endpoint
grpc-transcodeProtocolgRPC to HTTP
redirectTrafficURL redirects
response-rewriteTransformModify responses

Configuration

Config File (conf/config.yaml)

apisix:
  node_listen: 9080
  admin_listen:
    ip: 0.0.0.0
    port: 9180
  admin_key:
    - name: admin
      key: edd1c9f034335f136f87ad84b625c8f1
      role: admin
  ssl:
    enable: true
    listen_port: 9443

deployment:
  role: traditional
  role_traditional:
    config_provider: etcd
  etcd:
    host:
      - "http://127.0.0.1:2379"
    prefix: "/apisix"

plugin_attr:
  prometheus:
    export_uri: /apisix/prometheus/metrics
    export_addr:
      ip: 0.0.0.0
      port: 9091

Advanced Usage

Service Discovery

# Consul service discovery
curl -X PUT http://127.0.0.1:9180/apisix/admin/routes/1 \
  -H "X-API-KEY: edd1c9f034335f136f87ad84b625c8f1" \
  -d '{
    "uri": "/api/*",
    "upstream": {
      "service_name": "my-service",
      "discovery_type": "consul",
      "type": "roundrobin"
    }
  }'

Canary Release

curl -X PUT http://127.0.0.1:9180/apisix/admin/routes/1 \
  -H "X-API-KEY: edd1c9f034335f136f87ad84b625c8f1" \
  -d '{
    "uri": "/api/*",
    "plugins": {
      "traffic-split": {
        "rules": [
          {
            "weighted_upstreams": [
              { "upstream_id": "1", "weight": 90 },
              { "upstream_id": "2", "weight": 10 }
            ]
          }
        ]
      }
    }
  }'

Global Plugins

curl -X PUT http://127.0.0.1:9180/apisix/admin/global_rules/1 \
  -H "X-API-KEY: edd1c9f034335f136f87ad84b625c8f1" \
  -d '{
    "plugins": {
      "prometheus": {},
      "cors": {
        "allow_origins": "*",
        "allow_methods": "GET,POST,PUT,DELETE"
      }
    }
  }'

Monitoring

# Enable Prometheus metrics
# Access: http://localhost:9091/apisix/prometheus/metrics

# Key metrics:
# apisix_http_status — HTTP status codes
# apisix_bandwidth — request/response bandwidth
# apisix_http_latency — request latency histogram
# apisix_upstream_status — upstream health status
# apisix_http_requests_total — total request count

Troubleshooting

IssueSolution
502 Bad GatewayCheck upstream health; verify backend is reachable from APISIX
404 Not FoundVerify route URI pattern; check route is active
Admin API 401Use correct X-API-KEY header; check admin key in config
Plugin not loadingVerify plugin is listed in config.yaml plugins array
etcd connection failedCheck etcd is running; verify etcd host in config
SSL certificate errorVerify cert/key paths; check certificate chain completeness
Rate limiting not appliedVerify plugin config on route; check key configuration
Hot reload not workingetcd must be healthy; check APISIX error logs