在kubernetes GKE上为Nginx入口控制器设置缓存控制标头 [英] Setting cache-control headers for nginx ingress controller on kubernetes GKE
问题描述
我有一个Ingress-nginx控制器来处理到GKE上托管的Kubernetes集群的流量.我按照文档中的头盔安装说明进行了设置:
I have an ingress-nginx controller handling traffic to my Kubernetes cluster hosted on GKE. I set it up using helm installation instructions from docs:
大多数情况下一切正常,但是如果我尝试通过server-snippet
注释设置与缓存相关的参数,则所有应该获取缓存控制标头的提供的内容都会以404
的形式返回.
For the most part everything is working, but if I try to set cache related parameters via a server-snippet
annotation, all of the served content that should get the cache-control headers comes back as a 404
.
这是我的ingress-service.yaml
文件:
apiVersion: extensions/v1beta1
kind: Ingress
metadata:
name: ingress-service
annotations:
kubernetes.io/ingress.class: nginx
nginx.ingress.kubernetes.io/proxy-read-timeout: "4000"
nginx.ingress.kubernetes.io/proxy-send-timeout: "4000"
nginx.ingress.kubernetes.io/server-snippet: |
location ~* \.(js|css|gif|jpe?g|png)$ {
expires 1M;
add_header Cache-Control "public";
}
spec:
tls:
- hosts:
- example.com
secretName: example-com
rules:
- host: example.com
http:
paths:
- path: /
backend:
serviceName: client-cluster-ip-service
servicePort: 5000
- path: /api/
backend:
serviceName: server-cluster-ip-service
servicePort: 4000
同样,只有与正则表达式匹配的资源才以404
的形式返回(所有.js
文件,.css
文件等).
Again, it's only the resources that are matched by the regex that come back as 404
(all .js
files, .css
files, etc.).
对为什么会发生这种情况有任何想法吗?
Any thoughts on why this would be happening?
感谢您的帮助!
推荐答案
这些location
块是最后一场比赛和/或最长比赛获胜,并且由于入口自身不提供任何此类内容,因此nginx依赖于 configuration-snippet:
相反,可能与 if ($request_uri ~* ...) {
添加标题.
Those location
blocks are last and/or longest match wins, and since the ingress itself is not serving any such content, the nginx relies on a proxy_pass
directive pointing at the upstream server. Thus, if you are getting 404s, it's very likely because your location
is matching, thus interfering with the proxy_pass
one. There's a pretty good chance you'd actually want configuration-snippet:
instead, likely in combination with if ($request_uri ~* ...) {
to add the header.
可以使用指向python3 -m http.server 9090
或任何伪造的上游目标的nginx.conf进行本地尝试.
One can try this locally with a trivial nginx.conf pointing at python3 -m http.server 9090
or whatever fake upstream target.
另外,对于调试nginx入口问题,通常可以参考其实际的nginx.conf
(可以从任何一个入口Pod中获取)和/或查询入口Pod的日志(在此Nginx会发出帮助),这是非常宝贵的调试文本.
Separately, for debugging nginx ingress problems, it is often invaluable to consult its actual nginx.conf
, which one can grab from any one of the ingress Pods, and/or consulting the logs of the ingress Pods where nginx will emit helpful debugging text.
这篇关于在kubernetes GKE上为Nginx入口控制器设置缓存控制标头的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!