我想把这样的设置放在一起:

  • example.com#前端
  • example.com/api
  • example.com/authentication

  • 而且显然每个应用程序都是分开的应用程序,应该能够继续自己的路径ex. http://example.com/api/v1/test?v=ok现在我有一个像这样的yaml:
    apiVersion: networking.k8s.io/v1beta1
    kind: Ingress
    metadata:
     name: test-ingress
     annotations:
       nginx.ingress.kubernetes.io/rewrite-target: /$2
    spec:
     rules:
     - http:
         paths:
         - path: /
           backend:
             serviceName: frontend-service
             servicePort: 80
         - path: /api(/|$)(.*)
           backend:
             serviceName: backend-service
             servicePort: 80
         - path: /authentication(/|$)(.*)
           backend:
             serviceName: identityserver-service
             servicePort: 80
    
    / api和/ authentication以我想要的方式运行,但是前端的子路径不起作用。因此,例如,找不到 http://example.com/css/bootstrap.css
    到目前为止,我已经尝试过
    1-在前端路径的和处添加(/|$)(.*)2-添加具有相同背景和/.*的端口和路径的前端路径的副本
    他们都没有解决问题。
    这是描述结果:
    Name:             test-ingress
    Namespace:        default
    Address:          127.0.0.1
    Default backend:  default-http-backend:80 (<error: endpoints "default-http-backend" not found>)
    Rules:
      Host        Path  Backends
      ----        ----  --------
      *
                  /                          frontend-service:80 (10.1.80.38:80,10.1.80.43:80,10.1.80.50:80)
                  /api(/|$)(.*)              backend-service:80 (10.1.80.39:80,10.1.80.42:80,10.1.80.47:80)
                  /authentication(/|$)(.*)   identityserver-service:80 (10.1.80.40:80,10.1.80.41:80,10.1.80.45:80)
    Annotations:  nginx.ingress.kubernetes.io/rewrite-target: /$2
    Events:
      Type    Reason  Age                 From                      Message
      ----    ------  ----                ----                      -------
      Normal  UPDATE  43s (x14 over 13h)  nginx-ingress-controller  Ingress default/test-ingress
    
    PS:我发现了一些答案,似乎早于0.22.0版,并且以前无法使用。 kubernetes ingress with multiple target-rewrite

    最佳答案

    您的问题在于正则表达式不正确。
    如果启用了多行标志,则$运算符匹配字符串的末尾或行的末尾。在第一个组中,您正在捕获/$,但是您的字符串没有结尾并且不匹配。
    我对此进行了测试,并使用了此正则表达式:

     paths:
     - path: /()(.*)
       backend:
         serviceName: frontend-service
         servicePort: 80
    

    10-01 11:01
    查看更多