ASP.Net Core 2.2 Kubernetes Ingress: not found static content for custom path
Follow these steps to run your code:
- ingress: remove URL-rewriting from ingress.yml
apiVersion: extensions/v1beta1kind: 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
- deployment: pass environment variable with path base in ingress.yml
apiVersion: apps/v1kind: 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: enable loading environment params in Program.cs
var builder = new WebHostBuilder() .UseContentRoot(Directory.GetCurrentDirectory()) // .. .ConfigureAppConfiguration((hostingContext, config) => { // .. config.AddEnvironmentVariables(); // <--- // .. }) // ..
- startup: apply UsePathBaseMiddleware in Startup.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(); } // ..}