I cannot get my asp.net core websites to work behind an ngnix ingress controller in Kubernetes. I can view the site, but all links, css and images are broken.
My Ingress controller looks like this
apiVersion: extensions/v1beta1
kind: Ingress
metadata:
name: apps-ingress
annotations:
kubernetes.io/ingress.class: nginx
nginx.ingress.kubernetes.io/rewrite-target: /$2
spec:
rules:
- host:
http:
paths:
- path: /web(/|$)(.*)
backend:
serviceName: web-service
servicePort: 80
- path: /middle(/|$)(.*)
backend:
serviceName: middle-api-service
servicePort: 80
I've also configured my site to use forwarded headers
app.UseForwardedHeaders(new ForwardedHeadersOptions
{
ForwardedHeaders = ForwardedHeaders.All
});
I can browse to the website at {proxyaddress}/web
This loads the site, but all links, css and images break as they go to the root proxy address without the /web
that is the configured path for the ingress controller.
I did output the headers that website receives and I can see:
header X-Real-IP - val: 10.240.0.4
header X-Forwarded-For - val: 10.240.0.4
header X-Forwarded-Host - val: {ProxyAddress}
header X-Forwarded-Port - val: 443
header X-Forwarded-Proto - val: https
header X-Original-URI - val: /web/
header X-Scheme - val: https
I can see the header X-Original-URI
has the value of /web
which my website needs to use as its base for all links.
I've tried app.UsePathBase("/web");
and
app.UseForwardedHeaders(new ForwardedHeadersOptions
{
ForwardedForHeaderName = "X-Original-URI",
OriginalForHeaderName = "X-Original-URI",
OriginalHostHeaderName = "X-Original-URI",
ForwardedHeaders = ForwardedHeaders.XForwardedFor | ForwardedHeaders.XForwardedProto
});
Nothing seems to work, and I cannot find any info online as to what I need to set to get the website to work under the path of /web
thats configured by the proxy?
I found the answer, because I'm using a path base on the nginx proxy of /web
Instead of rewriting the target so that the controllers won't return a 404 in the asp.net core app, you need to leave the path as it is with the /web
apiVersion: extensions/v1beta1
kind: Ingress
metadata:
name: apps-ingress
labels:
name: apps-ingress
annotations:
kubernetes.io/ingress.class: nginx
spec:
rules:
- host:
http:
paths:
- path: /web
backend:
serviceName: web
servicePort: 80
- path: /middle
backend:
serviceName: middle-api
servicePort: 80
and then configure the path base to equal /web
in the app configure section.
public void Configure(IApplicationBuilder app, IHostingEnvironment env)
{
if (!String.IsNullOrEmpty(Configuration["PathBase"]))
app.UsePathBase(Configuration["PathBase"]);
I've also had to add an environment variable to the app, so that the /web
is configurable as I can't access the X-Original-URI
header on app start.