ASP.Net Core 2.2 Kubernetes Ingress:找不到自定义路径的静态内容



我有以下入口.yml:

apiVersion: extensions/v1beta1
kind: Ingress 
metadata:
  name: ingress 
  namespace: default 
  annotations:
    kubernetes.io/ingress.class: "nginx" 
    nginx.ingress.kubernetes.io/ssl-redirect: "false" 
    nginx.ingress.kubernetes.io/rewrite-target: /$2
  labels: 
    app: ingress 
spec:
  rules:  
    - host: 
      http:   
        paths:
          - path: /apistarter(/|$)(.*)
            backend:
              serviceName: svc-aspnetapistarter
              servicePort: 5000
          - path: //apistarter(/|$)(.*)
            backend:
              serviceName: svc-aspnetapistarter
              servicePort: 5000

部署我的 ASP.Net Core 2.2 API 应用程序并导航到 http://localhost/apistarter/ 后,浏览器调试器控制台显示加载静态内容和 Javascript 时出错。此外,导航到http://localhost/apistarter/swagger/index.html会导致

Fetch error Not Found /swagger/v2/swagger.json

我正在使用不同路径前缀对多个微服务使用 SAME 入口。它使用 microk8s 在我的本地 kubernetes 集群上运行。尚未在任何云提供商上。我已查看如何配置 ASP.NET Core 多微服务应用程序和 Azure AKS 入口路由,以便它不会破坏 wwwroot 文件夹中的资源,https://learn.microsoft.com/en-us/aspnet/core/host-and-deploy/proxy-load-balancer?view=aspnetcore-2.1 但这些都无济于事。

按照以下步骤运行代码:

  1. 入口:从入口.yml 中删除 URL 重写
apiVersion: extensions/v1beta1
kind: Ingress 
metadata:
  name: ingress 
  namespace: default 
  annotations:
    kubernetes.io/ingress.class: "nginx" 
    nginx.ingress.kubernetes.io/ssl-redirect: "false" 
  labels: 
    app: ingress 
spec:
  rules:  
    - host: 
      http:   
        paths:
          - path: /apistarter # <---
            backend:
              serviceName: svc-aspnetapistarter
              servicePort: 5000
  1. 部署:在入口.yml 中使用路径基础传递环境变量
apiVersion: apps/v1
kind: Deployment
# ..
spec:
  # ..
  template:
    # ..
    spec:
      # ..
      containers:
        - name: test01
          image: test.io/test:dev
          # ...
          env:
            # define custom Path Base (it should be the same as 'path' in Ingress-service)
            - name: API_PATH_BASE # <---
              value: "apistarter"
  1. 程序:在程序中启用加载环境参数.cs
var builder = new WebHostBuilder()
    .UseContentRoot(Directory.GetCurrentDirectory())
    // ..
    .ConfigureAppConfiguration((hostingContext, config) =>
    {
        // ..  
        config.AddEnvironmentVariables(); // <---
        // ..
    })
    // ..
    启动
  1. :在启动中应用 UsePathBaseMiddleware.cs
public class Startup
{
    public Startup(IConfiguration configuration)
    {
        _configuration = configuration;
    }
    private readonly IConfiguration _configuration;
    public void Configure(IApplicationBuilder app, IHostingEnvironment env)
    {
        var pathBase = _configuration["API_PATH_BASE"]; // <---
        if (!string.IsNullOrWhiteSpace(pathBase))
        {
            app.UsePathBase($"/{pathBase.TrimStart('/')}");
        }
        app.UseStaticFiles(); // <-- StaticFilesMiddleware must follow UsePathBaseMiddleware
        // ..
        app.UseMvc();
    }
    // ..
}

相关内容

  • 没有找到相关文章

最新更新