简体   繁体   English

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

[英]ASP.Net Core 2.2 Kubernetes Ingress: not found static content for custom path

I have the following ingress.yml:我有以下 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

After deploying my ASP.Net Core 2.2 API application and navigate to http://localhost/apistarter/ , browser debugger console shows errors loading the static content and Javascripts.部署我的 ASP.Net Core 2.2 API 应用程序并导航到http://localhost/apistarter/ ,浏览器调试器控制台显示加载静态内容和 Javascript 的错误。 In addition, navigating to http://localhost/apistarter/swagger/index.html results in此外,导航到http://localhost/apistarter/swagger/index.html结果

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

I am using the SAME ingress for multiple micro-services using different path prefix.我对使用不同路径前缀的多个微服务使用相同的入口。 It is running on my local kubernetes cluster using microk8s.它使用 microk8s 在我的本地 kubernetes 集群上运行。 Not on any cloud provider yet.还没有在任何云提供商上。 I have checked out 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 folder and https://docs.microsoft.com/en-us/aspnet/core/host-and-deploy/proxy-load-balancer?view=aspnetcore-2.1 but none of these helps.我已经查看了如何配置 ASP.NET Core 多微服务应用程序和 Azure AKS 入口路由,以便它不会破坏 wwwroot 文件夹https://docs.microsoft.com/en-us/aspnet/core 中的资源/host-and-deploy/proxy-load-balancer?view=aspnetcore-2.1但这些都没有帮助。

Follow these steps to run your code:请按照以下步骤运行您的代码:

  1. ingress : remove URL-rewriting from ingress.yml 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. deployment : pass environment variable with path base in ingress.yml部署:在ingress.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. program : enable loading environment params in Program.cs program : 在Program.cs 中启用加载环境参数
var builder = new WebHostBuilder()
    .UseContentRoot(Directory.GetCurrentDirectory())
    // ..
    .ConfigureAppConfiguration((hostingContext, config) =>
    {
        // ..  
        config.AddEnvironmentVariables(); // <---
        // ..
    })
    // ..
  1. startup : apply UsePathBaseMiddleware in Startup.cs启动:在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();
    }

    // ..
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM