首页 文章

无法将Google Cloud CDN缓存与入口控制器,Transfer-Encoding标头一起使用

提问于
浏览
1

我尝试将Google Cloud CDN配置为我的容器引擎项目 .

文档之后它有一个Content-Length标头或Transfer-Encoding标头,以便进行缓存 .

我的后端使用gzip压缩,所以我有Transfer-Encoding:chunked

问题是入口负载均衡器似乎删除了Transfer-encoding标头,因此我无法进行“缓存命中”

我使用“kubectl port-forward”将direclty连接到实例后端,我有Transfer-encoding头 .

但是当我连接到外部IP时,标头已经消失 .

这是我的入口配置

apiVersion: extensions/v1beta1
kind: Ingress
metadata:
  name: gateway-preprod3-ingress
  annotations:
    kubernetes.io/ingress.global-static-ip-name: gateway-preprod2-static-ip
spec:
  tls:
  - secretName: gateway-preprod-secret-2018-with-ca-7
  backend:
    serviceName: gateway-preprod
    servicePort: 80

这是我的部署配置

apiVersion: extensions/v1beta1
kind: Deployment
metadata:
  name: gateway-preprod
spec:
  replicas: 1
  strategy:
    type: RollingUpdate
  minReadySeconds: 50
  template:
    metadata:
      labels:
        app: gateway-preprod
    spec:
      containers:
      - name: gateway-preprod
        image: eu.gcr.io/writecontrol-1055/gateway:v305
        env:
        - name: writecontrolEnv
          value: preprod
        ports:
        - containerPort: 80

相反,对于某些没有GZIP压缩的资源,给出了Content-length头,我有一个成功的“缓存命中”

kubernetes版本是1.7.12-gke.1

这是一个测试它的URL:https://preprod-writecontrol.ovh

1 回答

  • 2

    我不确定为什么你的回复有 Transfer-Encoding Headers . 此标头不应位于源(您的应用程序),并且通常由其他跃点添加,例如Cloud HTTP Load Balancer(= Ingress)等代理 .

    有关 Transfer-Encoding 的更多信息:https://developer.mozilla.org/en-US/docs/Web/HTTP/Headers/Transfer-Encoding


    我能够使用 Content-Encoding Headers 让CDN使用GZIP响应:

    首先阅读https://cloud.google.com/cdn/docs/caching的缓存要求 . 您需要 Content-Length 和/或 Cache-Control: public 标头 . 因此,将这些标头编码到您的应用程序服

    在Ingress创建的负载均衡器上启用CDN后,make two requests ,如果在第二个请求中看到 Age 标头(如下所示),则表示您的请求现在正在从CDN提供 .

    curl -vH Accept-Encoding:gzip 35.186.195.233
    > [...]
    >
    < HTTP/1.1 200 OK
    < Content-Encoding: gzip
    < Date: Tue, 27 Mar 2018 19:38:20 GMT
    < Content-Length: 87
    < Content-Type: application/x-gzip
    < Via: 1.1 google
    < Cache-Control: max-age=600,public
    
    ��H����Q(�/�IQ�
    * Connection #0 to host 35.186.195.233 left intact
    K-*��ϳR0�3�3���/.�K�M�R�)+OM�55575��L�4ѭ�N+L���K��4A
    

    第二个请求(注意 Age Headers ):

    $ curl -vH Accept-Encoding:gzip 35.186.195.233
    [...]
    >
    < HTTP/1.1 200 OK
    < Cache-Control: max-age=600,public
    < Content-Encoding: gzip
    < Date: Tue, 27 Mar 2018 19:42:01 GMT
    < Content-Length: 87
    < Content-Type: application/x-gzip
    < Via: 1.1 google
    < Age: 314
    <
    ��H����Q(�/�IQ�
    * Connection #0 to host 35.186.195.233 left intact
    K-*��ϳR0�3�3���/.�K�M�R�)+OM�55575��L�4ѭ�N+L���K��4A
    

相关问题