Nginx IC 虚拟服务器的基于路径的路由不起作用

问题描述 投票:0回答:1

我有一个 Nginx 入口控制器 virtualServre 清单为

apiVersion: k8s.nginx.org/v1
kind: VirtualServer
metadata:
  name: tea-vs
spec:
  host: apps.example.com
  routes:
  - path: /
    pathType: Prefix
    rewrite:
      path: /tea/
    route:
      path: /tea(.*)
      pathType: Prefix
      service:
        name: tea-svc
        port:
          number: 80

我的应用程序在

/
上运行。当我输入
apps.example.com
时,它会加载 UI,并将 URL 显示为
apps.example.com/index.html

我的要求是:

  1. 如果用户输入

    apps.example.com/tea
    ,swagger UI 甚至应该加载其在根目录下的服务。

  2. 我的茶服务中心应该收到

    apps.example.com
    的请求,但不是
    apps.example.com/tea

  3. 用户仍将 URL 视为

    apps.example.com/tea/index.html 
    (或
     apps.example.com/index.html

  4. 我不想使用 Nginx

    Kind: Ingress 

我的 dotnet 代码如下,我不想碰它,因为它已经在 PROD 中工作了。我这样做是迁移到 Nginx 入口控制器的一部分,并且希望从基于主机的路由迁移到基于路径的路由,我认为这会带来一些灵活性

 app.UseSwagger();

        app.UseSwaggerUI(c =>
        {
            c.SwaggerEndpoint("/swagger/v1/swagger.json", "V1");
            c.RoutePrefix = string.Empty;
        });

        app.UseRouting();

为此苦苦挣扎,因为当我尝试进行任何基于路径的路由时,我总是得到 404

对此的任何帮助都非常感谢

kubernetes nginx swagger nginx-ingress
1个回答
0
投票

按照此处的文档进行操作:

https://docs.nginx.com/nginx-ingress-controller/configuration/virtualserver-and-virtualserverroute-resources/

apiVersion: k8s.nginx.org/v1
kind: VirtualServer
metadata:
  name: cafe
  namespace: cafe-ns
spec:
  host: cafe.example.com
  upstreams:
  - name: tea
    service: tea-svc
    port: 80
  routes:
  - path: /tea
    action:
      pass: tea
  - path: /coffee
    route: coffee-ns/coffee
apiVersion: k8s.nginx.org/v1
kind: VirtualServerRoute
metadata:
  name: coffee
  namespace: coffee-ns
spec:
  host: cafe.example.com
  upstreams:
  - name: latte
    service: latte-svc
    port: 80
  - name: espresso
    service: espresso-svc
    port: 80
  subroutes:
  - path: /coffee/latte
    action:
      pass: latte
  - path: /coffee/espresso
    action:
      pass: espresso

你应该做上面那样的事情。

如果您正在尝试重写:

apiVersion: networking.k8s.io/v1
kind: Ingress
metadata:
  name: cafe-ingress
  annotations:
    nginx.org/rewrites: "serviceName=tea-svc rewrite=/;serviceName=coffee-svc rewrite=/beans/"
spec:
  rules:
  - host: cafe.example.com
    http:
      paths:
      - path: /tea/
        pathType: Prefix
        backend:
          service:
            name: tea-svc
            port:
              number: 80
      - path: /coffee/
        pathType: Prefix
        backend:
          service:
            name: coffee-svc
            port:
              number: 80

您可以使用这里的一些示例:

https://github.com/nginxinc/kubernetes-ingress/tree/v3.6.1/examples

如果有帮助请告诉我。

© www.soinside.com 2019 - 2024. All rights reserved.