<p>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. </p>

<p>My Ingress controller looks like this</p>

<pre><code>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
</code></pre>

<p>I've also configured my site to use forwarded headers</p>

<pre><code> app.UseForwardedHeaders(new ForwardedHeadersOptions
 {
      ForwardedHeaders = ForwardedHeaders.All
 });
</code></pre>

<p>I can browse to the website at <code>{proxyaddress}/web</code>
This loads the site, but all links, css and images break as they go to the root proxy address without the <code>/web</code> that is the configured path for the ingress controller. </p>

<p>I did output the headers that website receives and I can see:</p>

<pre><code>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     
</code></pre>

<p>I can see the header <code>X-Original-URI</code> has the value of <code>/web</code> which my website needs to use as its base for all links. </p>

<p>I've tried <code>app.UsePathBase("/web");</code>
and </p>

<pre><code>app.UseForwardedHeaders(new ForwardedHeadersOptions
            {
                ForwardedForHeaderName = "X-Original-URI",
                OriginalForHeaderName = "X-Original-URI",
                OriginalHostHeaderName = "X-Original-URI",
                ForwardedHeaders = ForwardedHeaders.XForwardedFor | ForwardedHeaders.XForwardedProto
            });
</code></pre>

<p>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 <code>/web</code> thats configured by the proxy?</p>

Steve
<p>I found the answer, because I'm using a path base on the nginx proxy of <code>/web</code></p>

<p>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 <code>/web</code></p>

<pre><code>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
</code></pre>

<p>and then configure the path base to equal <code>/web</code> in the app configure section. </p>

<pre><code> public void Configure(IApplicationBuilder app, IHostingEnvironment env)
        {
            if (!String.IsNullOrEmpty(Configuration["PathBase"]))
                app.UsePathBase(Configuration["PathBase"]);
</code></pre>

<p>I've also had to add an environment variable to the app, so that the <code>/web</code> is configurable as I can't access the <code>X-Original-URI</code> header on app start. </p>
