gpt4 book ai didi

nginx - Q : How to rewrite single path among many with the ingress-nginx

转载 作者:行者123 更新时间:2023-12-02 16:06:52 26 4
gpt4 key购买 nike

我的 Ingress 上已经有很多域和很多路径,因为这是一个包含许多微服务的环境。

我如何以某种方式编辑我的入口,例如,当有人访问路径 /servicex 时,它会变成 /serviceb

我当前的入口如下(为简单起见,我省略了其他主机的一些路径)

apiVersion: networking.k8s.io/v1
kind: Ingress
metadata:
name: main-ingress
annotations:
nginx.ingress.kubernetes.io/ssl-redirect: "true"
nginx.ingress.kubernetes.io/force-ssl-redirect: "false"
nginx.ingress.kubernetes.io/proxy-body-size: "100m"
nginx.ingress.kubernetes.io/max-worker-connections: "0"
nginx.ingress.kubernetes.io/max-worker-open-files: "0"
nginx.ingress.kubernetes.io/client-header-buffer-size: "4k"
spec:
tls:
- hosts:
- subdomain-a.domain.com
- subdomain-b.domain.com
- subdomain-c.domain.com
- subdomain-d.domain.com
- subdomain-e.domain.com
secretName: domain-com-secret
rules:
- host: subdomain-a.domain.com
http:
paths:
- path: /
pathType: Prefix
backend:
service:
name: default-service
port:
number: 80
- path: /serviceb
pathType: Prefix
backend:
service:
name: b-service
port:
number: 80
- path: /servicec
pathType: Prefix
backend:
service:
name: c-service
port:
number: 80
- path: /serviced
pathType: Prefix
backend:
service:
name: d-service
port:
number: 80
- path: /servicee
pathType: Prefix
backend:
service:
name: e-service
port:
number: 80
- path: /servicee
pathType: Prefix
backend:
service:
name: e-service
port:
number: 80
- path: /servicef
pathType: Prefix
backend:
service:
name: f-service
port:
number: 80
- path: /serviceg
pathType: Prefix
backend:
service:
name: g-service
port:
number: 80

- host: subdomain-b.domain.com
< tons of other rules >

- host: subdomain-c.domain.com
< tons of other rules >

- host: subdomain-d.domain.com
< tons of other rules >

- host: subdomain-e.domain.com
< tons of other rules >

换句话说,如果用户输入以下任何内容,我们想要访问 /serviceb:

我已经查看了重写选项,但在我看来,如果我添加注释 nginx.ingress.kubernetes.io/rewrite-target:/servicex/$2 例如,然后尝试执行像这样的东西:

- path: /serviceb(/|$)(.*)
pathType: Prefix
backend:
service:
name: serviceb
port:
number: 80

行不通,因为首先,我们已经采用了该路径,其次,我认为注释将应用于整个事物,这会破坏所有其他路径。

提前致谢,如有任何帮助,我们将不胜感激

最佳答案

NGINX 入口与 NGINX 中几乎相同。在大多数情况下,内置注释可帮助您配置 NGINX 中的标准操作。

但是,如果您遇到任何类型的定制,如您的情况,您也可以通过这两个注释自由输入服务器或配置片段。

nginx.ingress.kubernetes.io/server-snippetnginx.ingress.kubernetes.io/configuration-snippet

在代码片段中,您可以像普通的 NGINX 一样做任何您想做的事情。

从您的回复中抓取配置以供其他人引用。

nginx.ingress.kubernetes.io/configuration-snippet: |-
rewrite ^(/servicex)(.*) /serviceb$2 last;

关于nginx - Q : How to rewrite single path among many with the ingress-nginx,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/69182868/

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