gpt4 book ai didi

nginx - Kubernetes 入口路径

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

我使用以下 yaml 文件为前端和后端设置了 nginx-ingress。

apiVersion: networking.k8s.io/v1beta1
kind: Ingress
metadata:
name: polls-ingress
annotations:
kubernetes.io/ingress.class: "nginx"
nginx.ingress.kubernetes.io/rewrite-target: /$1
nginx.ingress.kubernetes.io/service-upstream: "true"
cert-manager.io/cluster-issuer: "letsencrypt-prod"
spec:
tls:
- hosts:
- example.com
secretName: polls-tls
rules:
- host: example.com
http:
paths:
- path: /(.*)
backend:
serviceName: frontend
servicePort: 3000
- path: /graphql/(.*)
backend:
serviceName: polls
servicePort: 8000

目前,我的重写目标重新路由如下

example.com  => / of frontend app
example.com/ => / of frontend app
example.com/graphql => / of frontend app
example.com/graphql/post => / of frontend app

example.com/graphql/ => / of backend app
example.com/graphql/post/ => /post of backend app

但我希望后端应用程序同时用于 graphql/post 和 graphql/post/,如下所示。

example.com => / of frontend app
example.com/ => / of frontend app
example.com/hello => /hello of frontend app
example.com/test => /test of frontend app

example.com/graphql => / of backend app
example.com/graphql/ => / of backend app
example.com/grpahql/post => /post of backend app
example.com/graphql/post/ => /post of backend app

谁能告诉我如何实现它?

最佳答案

您需要像下面这样更改 graphql 路径的正则表达式:

apiVersion: networking.k8s.io/v1beta1
kind: Ingress
metadata:
name: polls-ingress
annotations:
kubernetes.io/ingress.class: "nginx"
nginx.ingress.kubernetes.io/rewrite-target: /$1
nginx.ingress.kubernetes.io/service-upstream: "true"
cert-manager.io/cluster-issuer: "letsencrypt-prod"
spec:
tls:
- hosts:
- example.com
secretName: polls-tls
rules:
- host: example.com
http:
paths:
- path: /(.*)
backend:
serviceName: frontend
servicePort: 3000
- path: /graphql/?(.*) # <- HERE add "?"
backend:
serviceName: polls
servicePort: 8000

来自正则表达式的文档:

The question mark indicates zero or one occurrences of the preceding element. For example, colou?r matches both "color" and "colour".

关于nginx - Kubernetes 入口路径,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/66299680/

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