在自定义 domain/URL 公开 Kong API 网关

Expose Kong API gateway at custom domain/URL

我已经按照文档 here 设置了 Kong API 网关。 使用下面的 curl 命令,我可以确认 Kong 已启动并通过本地主机响应。

curl -i http://localhost:8001/services
    
    HTTP/1.1 200 OK
    Date: Thu, 13 May 2021 15:39:32 GMT
    Content-Type: application/json; charset=utf-8
    Connection: keep-alive
    Access-Control-Allow-Origin: http://localhost:8002
    X-Kong-Admin-Request-ID: xeUkwshQPRXA5T9bPQmxsmPcspJCGU2L
    vary: Origin
    Access-Control-Allow-Credentials: true
    Content-Length: 23
    X-Kong-Admin-Latency: 5
    Server: kong/2.3.3.2-enterprise-edition

我的问题是,对于生产,如何配置 Kong 以响应 DNS/URL?

我已经在 AWS Route 53 上设置了一条 DNS 记录,以将流量指向部署了 Kong 的这台服务器的 IP。 对于一个独立的应用程序——例如 Node/Express 应用程序,我会设置一个反向代理,即 NGINX,以将收到的请求从 URL 转发到应用程序。 但是,由于 Kong 是 API 网关,因此不合适。

总结一下;

如果你想根据主机路由,这是一个classic feature

首先创建一个服务(你的节点应用):

然后创建路由:

然后就可以测试路由匹配了

curl -i --header 'kong-debug: 1' http://my-gateway-url.com:8000

HTTP/1.1 502 Bad Gateway
Date: Fri, 14 May 2021 08:11:54 GMT
Content-Type: text/plain; charset=utf-8
Connection: keep-alive
Kong-Route-Id: 9f5584d7-4ac5-4720-a90c-f809c47faf8d
Kong-Route-Name: my_gw
Kong-Service-Id: 77443f69-80f4-49ea-b910-77eebdeb9385
Kong-Service-Name: node_app
Server: kong/2.0.3
Content-Length: 58

相比
curl -i --header 'kong-debug: 1' 0.0.0.0:8000
HTTP/1.1 404 Not Found
Date: Thu, 13 May 2021 20:11:23 GMT
Content-Type: application/json; charset=utf-8
Connection: keep-alive
Content-Length: 48

{"message":"no Route matched with those values"}

另一件要检查的事情是你不能公开哪个端口 80 或 8000,并检查路由是否在你的安全级别上打开(例如 AWS 上的安全组)。