API 网关选型:Kong、APISIX、Nginx 对比

0 阅读3分钟

API 网关选型:Kong、APISIX、Nginx 对比

API 网关是微服务架构的核心组件。本文对比三大主流方案,帮你做出选择。


一、为什么需要 API 网关?

请求 → 网关 → 路由 → 鉴权 → 限流 → 后端服务

核心功能

  • 请求路由
  • 身份认证
  • 限流熔断
  • 日志监控
  • 协议转换

二、三大方案对比

特性KongAPISIXNginx
架构Kong + PostgreSQL无数据库纯 Nginx
性能最高
功能丰富插件丰富 + 热更新需自行开发
配置REST APIAdmin API + YAML配置文件
学习曲线
支持商业+社区社区社区
LicenseKong EnterpriseApache 2.0Nginx Plus

三、Kong

3.1 架构

┌─────────────┐
│   Client    │
└──────┬──────┘
       │
┌──────▼──────┐
│    Kong     │  ← Lua 脚本
│  (Gateway)  │
└──────┬──────┘
       │
┌──────▼──────┐
│ PostgreSQL  │  ← 配置存储
└─────────────┘

3.2 快速开始

# Docker 启动
docker run -d --name kong \
  -e "KONG_DATABASE=postgres" \
  -e "KONG_PG_HOST=postgres" \
  -e "KONG_PROXY_ACCESS_LOG=/dev/stdout" \
  -p 8000:8000 \
  kong:latest

# 添加服务
curl -X POST http://localhost:8001/services \
  -d 'name=user-service' \
  -d 'url=http://user-service:8080'

# 添加路由
curl -X POST http://localhost:8001/services/user-service/routes \
  -d 'paths[]=/users' \
  -d 'strip_path=true'

3.3 插件配置

# 限流
curl -X POST http://localhost:8001/plugins \
  -d 'name=rate-limiting' \
  -d 'config.minute=100'

# JWT 认证
curl -X POST http://localhost:8001/plugins \
  -d 'name=jwt' \
  -d 'config.key_names=jwt'

# 请求转换
curl -X POST http://localhost:8001/plugins \
  -d 'name=request-transformer' \
  -d 'config.add.headers=X-Kong-Version:1.0'

四、APISIX

4.1 架构

┌─────────────┐
│   Client    │
└──────┬──────┘
       │
┌──────▼──────┐
│   APISIX    │  ← 无数据库、热更新
│  (Gateway)  │
└─────────────┘
       │
┌──────▼──────┐
│   etcd      │  ← 配置存储
└─────────────┘

4.2 快速开始

# Docker 启动
docker run -d --name apisix \
  -e "discovery.type=consul" \
  -v ./config.yaml:/usr/local/apisix/conf/config.yaml \
  -p 8000:8000 \
  apache/apisix:latest

# 添加路由(YAML 配置)
curl -X PUT http://localhost:9180/apisix/admin/routes/1 \
  -H 'X-API-KEY: edd1c9f034335f136f87ad81b25c962f' \
  -d '{
    "uri": "/user/*",
    "upstream": {
      "type": "roundrobin",
      "nodes": {
        "user-service:8080": 1
      }
    }
  }'

4.3 动态限流

# limit-req 插件
plugins:
  limit-req:
    rate: 100
    burst: 50
    key: remote_addr

4.4 热更新

# 修改路由无需重启
curl http://localhost:9180/apisix/admin/routes/1 \
  -X PUT -d '...新配置...'
# 立即生效

五、Nginx

5.1 传统配置

upstream backend {
    server user-service:8080;
    server user-service:8081;
    keepalive 32;
}

server {
    listen 80;
    server_name api.example.com;
    
    location /users/ {
        proxy_pass http://backend;
        proxy_set_header Host $host;
        
        # 限流
        limit_req zone=api_limit burst=20 nodelay;
        
        # JWT 验证
        auth_jwt "realm";
        auth_jwt_key_file /etc/nginx/jwt.key;
    }
}

5.2 Nginx Unit / OpenResty

-- OpenResty + Lua
location /api {
    access_by_lua_block {
        local jwt = require("resty.jwt")
        local token = ngx.var.http_authorization
        local jwt_obj = jwt:verify("secret", token)
        if not jwt_obj.verified then
            ngx.exit(401)
        end
    }
    
    proxy_pass http://backend;
}

5.3 新选择:Nginx Proxy Manager

# Web UI 管理 Nginx
docker run -d --name nginx-proxy-manager \
  -p 80:80 -p 443:443 \
  -v data:/data \
  jc21/nginx-proxy-manager:latest

六、选型建议

场景推荐理由
快速上线APISIX安装简单、热更新
企业级Kong插件丰富、商业支持
极致性能Nginx性能最高
KubernetesAPISIX云原生友好
老系统改造Kong迁移平滑

七、性能对比

网关QPS延迟内存
Kong5,0002-5ms200MB
APISIX15,0001-2ms100MB
Nginx50,000<1ms50MB

八、总结

  • APISIX:性能好、云原生、热更新,推荐!
  • Kong:功能全、企业级,有商业需求选
  • Nginx:性能极致,但开发成本高