gpt4 book ai didi

nginx - kubernetes nginx 入口错误与配置片段

转载 作者:行者123 更新时间:2023-12-02 11:34:28 24 4
gpt4 key购买 nike

我有以下 ingress.yaml 文件

apiVersion: extensions/v1beta1
kind: Ingress
metadata:
name: nginx-configuration-snippet
annotations:
nginx.ingress.kubernetes.io/rewrite-target: /$2
nginx.ingress.kubernetes.io/configuration-snippet: |
location /base/path/v1/api/update {
deny all;
return 404;
}
spec:
rules:
- http:
paths:
- path: /base/path(/|$)(.*)
backend:
serviceName: myApi
servicePort: 8080
但是当我向 https:///base/path/v1/api/update 发送请求时,它成功了,并且在 nginx 入口 Controller 中出现了以下错误
Error: exit status 1
2020/08/06 18:35:07 [emerg] 1734#1734: location "/base/path/v1/api/update" is outside location "^/base/path(/|$)(.*)" in /tmp/nginx-cfg008325631:2445
nginx: [emerg] location "/base/path/v1/api/update" is outside location "^/base/path(/|$)(.*)" in /tmp/nginx-cfg008325631:2445
nginx: configuration file /tmp/nginx-cfg008325631 test failed
有人可以帮忙吗?

最佳答案

configuration-snippet是添加配置到地点 .
如果您想向 server 添加自定义位置上下文,您应该使用 server-snippet反而:

Using the annotation nginx.ingress.kubernetes.io/server-snippet it ispossible to add custom configuration in the server configurationblock.


您还需要使用一些修饰符和正则表达式来使其工作( ~*^ )。
以下配置应该可以工作:
apiVersion: extensions/v1beta1
kind: Ingress
metadata:
name: nginx-configuration-snippet
annotations:
nginx.ingress.kubernetes.io/rewrite-target: /$2
nginx.ingress.kubernetes.io/server-snippet: |
location ~* "^/base/path/v1/api/update" {
deny all;
return 403;
}
spec:
rules:
- http:
paths:
- path: /base/path(/|$)(.*)
backend:
serviceName: myApi
servicePort: 8080
最后 nginx.config应该这样结束:
$ kubectl exec -n kube-system nginx-ingress-controller-6fc5bcc8c9-chkxf -- cat /etc/nginx/nginx.conf

[...]

location ~* "^/base/path/v1/api/update" {
deny all;
return 403;
}

location ~* "^/base/path(/|$)(.*)" {
[...]
}

关于nginx - kubernetes nginx 入口错误与配置片段,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/63289848/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com