X-Cache-Status 始终使用 Kong 代理缓存插件绕过

Posted

技术标签:

【中文标题】X-Cache-Status 始终使用 Kong 代理缓存插件绕过【英文标题】:X-Cache-Status is always Bypass using Kong proxy-cache plugin 【发布时间】:2019-07-19 22:24:02 【问题描述】:

我在 Kong 有服务,我已经为该服务设置了代理缓存插件。

curl -X POST http://localhost:8001/plugins --data "name=proxy-cache" --data "config.strategy=redis" --data 'service_id=2f0a285d-7b25-48d6-adc3-bbf28ffe5f47' --data "config.redis.host=127.0.0.1" --data "config.redis.port=6379" --data "config.redis.password=my_redis_password"

当我从该服务调用 API 时:

curl -i -X GET --url http://localhost:3002/v1/currency --header 'apikey: MY_API_KEY'

一切正常,但 X-Cache-Status 总是 Bypass

HTTP/1.1 200 OK                                                                                                                                       
Content-Type: application/json; charset=utf-8                                                                                                         
Content-Length: 3654                                                                                                                                  
Connection: keep-alive                                                                                                                                
X-RateLimit-Limit-second: 100                                                                                                                         
X-RateLimit-Remaining-second: 99                                                                                                                      
X-Cache-Key: 3e18cdfc6e02359fb0f874efdf5788d8                                                                                                         
X-Cache-Status: Bypass                                                                                                                                
X-Powered-By: Express
...

如何调试绕过原因?

【问题讨论】:

2年过去了,Kong还是有这个问题。 【参考方案1】:

为避免绕过 X-Cache-Status,您必须在创建代理缓存插件时添加此配置

--data "config.content_type=application/json; charset=utf-8" 

【讨论】:

那行不通。它仍然总是绕过【参考方案2】:

Kong 社区版附带的插件proxy-cache 只允许内存缓存。如果要使用 Redis 进行缓存,则必须使用 Kong Enterprise 版本。更多信息here

作为替代方案,Github 上有一个名为 kong-plugin-proxy-cache 的开源插件。您必须先从 Luarocks 安装插件,然后在 Kong config 中启用插件

    # Install plugin dependency
    sudo luarocks install lua-resty-redis-connector

    # install plugin
    sudo luarocks install kong-plugin-proxy-cache

    # Enable plugin in kong.conf
    plugins = bundled,proxy-cache

    # After enabling, you can use plugin with any service, route or consumer.
    # To enable it for a service
    curl -X POST http://localhost:8001/services/<service-name>/plugins \
    --data "name=proxy-cache"  \
    --data "config.cache_ttl=300" \
    --data "config.cache_control=false" \
    --data "config.redis.host=<redis-host>" \
    --data "config.redis.port=<redis-port>"

【讨论】:

以上是关于X-Cache-Status 始终使用 Kong 代理缓存插件绕过的主要内容,如果未能解决你的问题,请参考以下文章

微服务之kong+consul

KONG基础使用

linux 安装kong gateway

使用kong-dashboard添加Certificates的问题

1. 使用 Docker 安装 Kong

Kong Ingress Controller - 删除 Kong 相关的标头