简体   繁体   English

Kubernetes 入口路径

[英]Kubernetes Ingress Path

I have nginx-ingress setup for both frontend and backend using below yaml file.我使用 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

Currently, my rewrite-target reroutes as below目前,我的重写目标重新路由如下

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

But I want the backend app to be used for both graphql/post and graphql/post/ as below.但我希望后端应用程序同时用于 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

Could anyone tell me how to achieve it?谁能告诉我如何实现它?

You need to change the regex for graphql path like the following:您需要更改 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

From docs on regex:来自正则表达式的文档:

The question mark indicates zero or one occurrences of the preceding element.问号表示前面的元素出现零次或一次。 For example, colou?r matches both "color" and "colour".例如,colou?r 匹配“颜色”和“颜色”。

i couldn't comment so i had t post a response here, did you ever manage to solve this issue?我无法发表评论,所以我没有在这里发表回应,你有没有设法解决这个问题? i'm facing the same thing.我面临着同样的事情。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM