<p>I have two SPA dotnetcore/Angular sites running in my AKS cluster and am trying to configure the ingress service to route traffic to each one. Currently both applications are sitting behind separate cluster IP services and are definitely running. I have also tested them running in Docker containers and know that both are reachable on the configured ports. If I configure one of the applications to be route-able via the root subpath <code>/?(.*)</code> the site loads correctly.</p>

<p>However, the other application that is routed via any other subpath <code>/another/?(.*)</code> cannot load any of the static (JS, CSS, etc.) files - the requests seem to return index.html.</p>

<p>From ingress-controller.yaml</p>

<pre><code>...
 annotations:
    kubernetes.io/ingress.class: nginx
    nginx.ingress.kubernetes.io/rewrite-target: /$1
    nginx.ingress.kubernetes.io/backend-protocol: "HTTPS"
spec:
  rules:
    - http:
        paths:
          - path: /?(.*)
            backend:
              serviceName: cluster-ip-app-1
              servicePort: 5001
          - path: /app2/?(.*)
            backend:
              serviceName: cluster-ip-app-2
              servicePort: 5003
</code></pre>

<p>Cluster-ip-app-1 is configured to listen on port 5001 as is the underlying application. The same can be said for app-2 (but on port 5003).</p>

<p>I have also tried adding various server-snippet/configuration-snippet's to no avail (not necessarily at the same time) e.g.</p>

<pre><code>nginx.ingress.kubernetes.io/server-snippet (or configuration-snippet): |
      root   /path/to/dist;
      index  index.html index.htm;
      location / {
        if (!-e $request_filename){
            rewrite ^(.*)$ /index.html break;
        }
      }
      location ~* \.(js|css|png|jpg|jpeg|gif|ico)$ {
          expires max;
          log_not_found off;
      }
      try_files $uri $uri/ /index.html?$args =404;
      try_files $uri /index.html =404;
      include  /etc/nginx/mime.types;
      rewrite /path/to/dist/([^.]+)$ /app/ClientApp/dist break;  
      rewrite /app2/?(.*) / break;
</code></pre>

<p>In the above configuration, app-1 loads properly when visiting the PIP over https. However, when trying to visit https:///app2/ some of the static file requests fail and others seem to be returning the index page rather than the file itself. </p>

<p><img src="https://i.stack.imgur.com/i4HAy.png" alt="static resources">
<img src="https://i.stack.imgur.com/f9JaU.png" alt="index returning instead of resource"></p>

<p>It should be possible to set up some an alias record for the PIP in azure if that is the only way to resolve this.</p>

<p>Lastly, are there any good Nginx configuration tutorials for newbies (a bonus if it is more geared towards nginx ingress)? 😁</p>

<p>Thank you for your help in advanced!</p>

Alex Skinner
<p>What you should do is, firstly add line to you annotations:</p>

<pre><code>nginx.ingress.kubernetes.io/use-regex: "true"
</code></pre>

<p>Try to change your location rule to: <code>- path: /app2(/|$)(.*)</code> and the rewrite annotation to: <code>nginx.ingress.kubernetes.io/rewrite-target: /$2</code>. It will looks similar to this:</p>

<pre><code>...
 annotations:
    kubernetes.io/ingress.class: nginx
    nginx.ingress.kubernetes.io/rewrite-target: /$2
    nginx.ingress.kubernetes.io/backend-protocol: "HTTPS"
    nginx.ingress.kubernetes.io/use-regex: "true"
spec:
  rules:
    - http:
        paths:
          - path: /?(.*)
            backend:
              serviceName: cluster-ip-app-1
              servicePort: 5001
          - path: /app2(/|$)(.*)
            backend:
              serviceName: cluster-ip-app-2
              servicePort: 5003
</code></pre>

<p>You want to hit app1 or app2 depending on the request URL. Also, please look for documentation about mapping <a href="https://kubernetes.io/docs/concepts/services-networking/ingress/#simple-fanout" rel="nofollow noreferrer">each path to different services</a>.</p>

<p>If you still looking for interesting Nginx Ingress configuration tutorials I will recommend you those two:</p>

<ul>
<li><a href="https://gardener.cloud/050-tutorials/content/howto/service-access/" rel="nofollow noreferrer">How Ingress works</a></li>
<li><a href="https://kubernetes.github.io/ingress-nginx/examples/rewrite/" rel="nofollow noreferrer">How rewrite in Ingress works</a></li>
</ul>

<p>I hope it will helps you.</p>
