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

编程入门 行业动态 更新时间:2024-10-16 18:22:55
本文介绍了ASP.NET Core 2.2 Kubernetes Inress:未找到自定义路径的静态内容的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧! 问题描述

我有以下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 Core2.2API应用程序并导航到localhost/apistarter/后,浏览器调试器控制台在加载静态内容和Java脚本时显示错误。此外,导航到localhost/apistarter/swagger/index.html会导致

Fetch error Not Found /swagger/v2/swagger.json 对于使用不同路径前缀的多个微服务,我使用相同的入口。它运行在我本地的Kubernetes集群上,使用的是microk8。目前还没有出现在任何云提供商身上。我已签出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和docs.microsoft/en-us/aspnet/core/host-and-deploy/proxy-load-balancer?view=aspnetcore-2.1,但这些都没有帮助。

推荐答案

按照以下步骤运行代码:

  • 入口:从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
  • 部署:将环境变量与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"
  • 程序:启用Program.cs中的环境参数加载
  • var builder = new WebHostBuilder() .UseContentRoot(Directory.GetCurrentDirectory()) // .. .ConfigureAppConfiguration((hostingContext, config) => { // .. config.AddEnvironmentVariables(); // <--- // .. }) // ..
  • 启动:在Startup.cs中应用UsePath BaseMiddleware 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(); } // .. }
  • 更多推荐

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

    本文发布于:2023-11-14 04:08:34,感谢您对本站的认可!
    本文链接:https://www.elefans.com/category/jswz/34/1586173.html
    版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
    本文标签:自定义   静态   路径   未找到   内容

    发布评论

    评论列表 (有 0 条评论)
    草根站长

    >www.elefans.com

    编程频道|电子爱好者 - 技术资讯及电子产品介绍!