Nginx入口重写目标中断到.NET Core API的链接 [英] Nginx ingress rewrite target breaks link to .NET Core API

查看:87
本文介绍了Nginx入口重写目标中断到.NET Core API的链接的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个包含多种服务的安装程序,并且通过Ingress/Terraform/Kubernetes安装程序运行.当前,我所有的Vue.JS应用程序都通过NGINX提供服务,但是当我添加以下行"nginx.ingress.kubernetes.io/rewrite-target": "/$1"以确保其他路由(例如/frontend和/backend)正常工作时,它破坏了我的API,因此我无法再通过

平台设置:

resource "kubernetes_ingress" "ingress" {
metadata {
name      = "ingress"
namespace = var.namespace_name
annotations = {
  "nginx.ingress.kubernetes.io/force-ssl-redirect" = true
  "nginx.ingress.kubernetes.io/from-to-www-redirect" = true
  "nginx.ingress.kubernetes.io/ssl-redirect": true
  "nginx.ingress.kubernetes.io/add-base-url": false
  "nginx.ingress.kubernetes.io/rewrite-target": "/$1"
  "kubernetes.io/ingress.class": "nginx"
  "ncp/use-regex": true
}
}

spec {
tls {
  hosts = [var.domain_name, "*.${var.domain_name}"]
  secret_name = "tls-secret"
}
rule {
  host = var.domain_name
  http {
    path {
      path = "/(.*)"
      backend {
        service_name = "frontend"
        service_port = 80
      }
    }

    path {
      path = "/api(.*)"
      backend {
        service_name = "api"
        service_port = 80
      }
    }

    path {
      path = "/backend(.*)"
      backend {
        service_name = "backend"
        service_port = 80
      }
    }

    path {
      path = "/payment(.*)"
      backend {
        service_name = "payment"
        service_port = 80
      }
    }

  }
 }
}

wait_for_load_balancer = true
}

我的Startup.cs配置

    public void Configure(IApplicationBuilder app, IWebHostEnvironment env)
    {
        if (env.IsDevelopment())
        {
            app.UseDeveloperExceptionPage();
        }

        // Enable middleware to serve generated Swagger as a JSON endpoint.
        app.UseSwagger();

        // Enable middleware to serve swagger-ui (HTML, JS, CSS, etc.),
        // specifying the Swagger JSON endpoint.
        app.UseSwaggerUI(c =>
        {
            c.SwaggerEndpoint("/swagger/v1/swagger.json", "OLC API V1");
        });

        app.UseCors(AllowSpecificOrigins);

        app.UseRouting();


        app.UseEndpoints(endpoints =>
        {
            endpoints.MapControllers();
        });
    }

terraform设置似乎适用于我所有的vue.js容器.唯一的问题是无法再访问API.甚至都不通过/api/api这样的问题

我尝试仅重写特定的路线并更改应用程序,以便在其他路线上使用,但仍然出现404错误.即使创建响应/路由的控制器.删除重写目标行时,API会起作用.但是Vue.JS容器没有

解决方案

事实证明,这只是一个正则表达式问题,我无法正确设置路径.我更改了以下内容.

 path {
  path = "/api(.*)"
  backend {
    service_name = "api"
    service_port = 80
  }
}

进入

     path {
      path = "/(api.*)"
      backend {
        service_name = "olc-api"
        service_port = 80
      }
    }

以此将/api匹配到我的.NET核心应用,而不是尝试在vue.js容器中查找URL

I've got a setup with multiple services and are run via an Ingress / Terraform / Kubernetes setup. Currently, all my Vue.JS applications are served via NGINX however when I added the following line "nginx.ingress.kubernetes.io/rewrite-target": "/$1" to make sure my other routes work such as /frontend and /backend it broke my API and I can no longer access it via /api/

Terraform setup:

resource "kubernetes_ingress" "ingress" {
metadata {
name      = "ingress"
namespace = var.namespace_name
annotations = {
  "nginx.ingress.kubernetes.io/force-ssl-redirect" = true
  "nginx.ingress.kubernetes.io/from-to-www-redirect" = true
  "nginx.ingress.kubernetes.io/ssl-redirect": true
  "nginx.ingress.kubernetes.io/add-base-url": false
  "nginx.ingress.kubernetes.io/rewrite-target": "/$1"
  "kubernetes.io/ingress.class": "nginx"
  "ncp/use-regex": true
}
}

spec {
tls {
  hosts = [var.domain_name, "*.${var.domain_name}"]
  secret_name = "tls-secret"
}
rule {
  host = var.domain_name
  http {
    path {
      path = "/(.*)"
      backend {
        service_name = "frontend"
        service_port = 80
      }
    }

    path {
      path = "/api(.*)"
      backend {
        service_name = "api"
        service_port = 80
      }
    }

    path {
      path = "/backend(.*)"
      backend {
        service_name = "backend"
        service_port = 80
      }
    }

    path {
      path = "/payment(.*)"
      backend {
        service_name = "payment"
        service_port = 80
      }
    }

  }
 }
}

wait_for_load_balancer = true
}

My Startup.cs config

    public void Configure(IApplicationBuilder app, IWebHostEnvironment env)
    {
        if (env.IsDevelopment())
        {
            app.UseDeveloperExceptionPage();
        }

        // Enable middleware to serve generated Swagger as a JSON endpoint.
        app.UseSwagger();

        // Enable middleware to serve swagger-ui (HTML, JS, CSS, etc.),
        // specifying the Swagger JSON endpoint.
        app.UseSwaggerUI(c =>
        {
            c.SwaggerEndpoint("/swagger/v1/swagger.json", "OLC API V1");
        });

        app.UseCors(AllowSpecificOrigins);

        app.UseRouting();


        app.UseEndpoints(endpoints =>
        {
            endpoints.MapControllers();
        });
    }

The terraform setup seems to work for all my vue.js containers. Only problem is that API is no longer accessible. Not even via /api/api such as in this question

I've tried to rewrite only specific routes and change the app so that'd serve on a different route but I still get a 404 error. Even when creating a controller that responds to the / route. When removing the rewrite target line the API does work. But the Vue.JS containers do not

解决方案

It turns out that, this is simply a regex issue and me not setting paths correctly. I changed the following.

 path {
  path = "/api(.*)"
  backend {
    service_name = "api"
    service_port = 80
  }
}

Into

     path {
      path = "/(api.*)"
      backend {
        service_name = "olc-api"
        service_port = 80
      }
    }

With this it matches the /api to my .NET core app, instead of it trying to find a URL within the vue.js container(s)

这篇关于Nginx入口重写目标中断到.NET Core API的链接的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆