【问题标题】:ASP.Net Core 2.2 Kubernetes Ingress: not found static content for custom pathASP.Net Core 2.2 Kubernetes Ingress:未找到自定义路径的静态内容
【发布时间】:2019-10-30 17:21:40
【问题描述】:

我有以下 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

我对使用不同路径前缀的多个微服务使用相同的入口。它使用 microk8s 在我的本地 kubernetes 集群上运行。还没有在任何云提供商上。我已经查看了 How to configure an ASP.NET Core multi microservice application and Azure AKS ingress routes so that it doesn't break resources in the wwwroot folderhttps://docs.microsoft.com/en-us/aspnet/core/host-and-deploy/proxy-load-balancer?view=aspnetcore-2.1 但这些都没有帮助。

【问题讨论】:

    标签: c# asp.net-core kubernetes kubernetes-ingress nginx-ingress


    【解决方案1】:

    按照以下步骤运行您的代码:

    1. ingress:从 ingress.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. 部署:在 ingress.yml 中使用 path base 传递环境变量
    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. program:在Program.cs中启用加载环境参数
    var builder = new WebHostBuilder()
        .UseContentRoot(Directory.GetCurrentDirectory())
        // ..
        .ConfigureAppConfiguration((hostingContext, config) =>
        {
            // ..  
            config.AddEnvironmentVariables(); // <---
            // ..
        })
        // ..
    
    1. startup:在Startup.cs中应用UsePathBaseMiddleware
    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();
        }
    
        // ..
    }
    

    【讨论】:

    • 感谢您提供出色的分步说明,这解决了我的问题!
    • 我在以前的项目中也一直在努力解决这个问题,并且没有合适的解决方案。我不想在我的新项目中遇到同样的问题。这正是我所需要的。谢谢!
    • @ThomasLuijken 没问题我很乐意帮助你。
    猜你喜欢
    • 2019-05-06
    • 2018-02-20
    • 2020-03-05
    • 2019-11-14
    • 2020-05-06
    • 1970-01-01
    • 1970-01-01
    • 2019-05-03
    • 1970-01-01
    相关资源
    最近更新 更多