<p>Alright, various permutations of this question have been asked and I feel terrible asking; I'm throwing the towel in and was curious if anyone could point me in the right direction (or point out where I'm wrong). I went ahead and tried a number of <a href="https://github.com/kubernetes/ingress-nginx/tree/main/docs/examples/rewrite#examples" rel="nofollow noreferrer">examples</a> from the docs, but to no avail (see below).</p>
<p>I'm trying to route traffic to the appropriate location under Kubernetes using an Ingress controller.</p>
<h3>Server Setup</h3>
<p>I have a server, <code>myserver.com</code> and three services running at:</p>
<p><code>myserver.com/services/</code></p>
<p><code>myserver.com/services/service_1/</code></p>
<p><code>myserver.com/services/service_2/</code></p>
<p>Note that I'm not doing anything (purposefully) to <code>myserver.com/</code>.</p>
<p>At each of the three locations, there's a webapp running. For example, <code>myserver.com/services/service_2</code> needs to load css files at <code>myserver.com/services/service_2/static/css</code>, etc...</p>
<h3>Kubernetes Ingress</h3>
<p>To manage the networking, I'm using a Kubernetes Ingress controller, which I've defined below. The CORS annotations aren't <em>super</em> relevant, but I've included them to clear up any confusion.</p>
<pre><code>apiVersion: networking.k8s.io/v1
kind: Ingress
metadata:
  name: myServices
  namespace: myServices
  annotations:
    nginx.ingress.kubernetes.io/enable-cors: &quot;true&quot;
    nginx.ingress.kubernetes.io/cors-allow-methods: &quot;GET, POST, OPTIONS&quot;
    nginx.ingress.kubernetes.io/cors-allow-origin: '$http_origin'
    nginx.ingress.kubernetes.io/cors-allow-credentials: &quot;true&quot;
    nginx.ingress.kubernetes.io/rewrite-target: /
    nginx.ingress.kubernetes.io/use-regex: &quot;true&quot;

spec:
  ingressClassName: nginx
  tls:
  - hosts:
      - myserver.com
  rules:
  - host: myserver.com
    http:
      paths:
      - path: /services
        pathType: Prefix
        backend:
          service:
            name: web-service
            port:
              number: 80
      - path: /services/service_1(/|$)
        pathType: Prefix
        backend:
          service:
            name: web-service-1
            port:
              number: 80
      - path: /services/service_2(/|$)
        pathType: Prefix
        backend:
          service:
            name: web-service-2
            port:
              number: 80
</code></pre>
<h3>Targets</h3>
<p>I noticed that one helpful thing to do is give some path examples. From the examples below <em>it looks like the paths aren't that complicated</em>. I <em>think</em> this is what I'm after. Note that I'd like each service to be able to resolve its css and image files.</p>
<pre><code>myserver.com/services -&gt; myserver.com/services
myserver.com/services/xxx/xxx -&gt; myserver.com/services/xxx/xxx
myserver.com/services/service_1 -&gt; myserver.com/services/service_1
myserver.com/services/service_1/xxx/xxx -&gt; myserver.com/services/service_1/xxx/xxx
myserver.com/services/service_2/xxx/xxx -&gt; myserver.com/services/service_2/xxx/xxx


</code></pre>
<h3>Attempts</h3>
<p>I know that this issue has to do a lot with the <code>nginx.ingress.kubernetes.io/rewrite-target</code> rule and its interaction with the paths I've defined.</p>
<p>I <em>know</em> that I don't want <code>nginx.ingress.kubernetes.io/rewrite-target: $1</code> because that gives a 500 when visiting <code>myserver.com/services</code></p>
<p>I <em>know</em> that I don't want <code>nginx.ingress.kubernetes.io/rewrite-target: $1/$2</code> because when I visit <code>myserver.com/services/service_1</code> I actually get part of the content at <code>myserver.com/services</code> rendered on the page.</p>
<h4>SO Attempt 1</h4>
<p>I also attempted to replicate the accepted solution from <a href="https://stackoverflow.com/questions/65703968/kubernetes-ingress-nginx-use-regex-to-match-exact-url-path">this</a> question.</p>
<p>In this attempt I set</p>
<p><code>nginx.ingress.kubernetes.io/rewrite-target: &quot;/$1&quot;</code> and one of the service paths to</p>
<p><code>- path: /(services/service_1(?:/|$).*)</code></p>
<p>When I visit <code>myserver.com/services/service_1/xyz</code>, the HTML from <code>myserver.com/services/service_1</code> gets rendered.</p>
<h3>Concluding Thoughts</h3>
<p>Something ain't quite right with the path rewrite and paths rules. Any suggestions?</p>

<p>The problem you reported in your most recent comment is resolved by looking at the <a href="https://kubernetes.github.io/ingress-nginx/examples/rewrite/" rel="nofollow noreferrer">rewrite example</a> in the nginx-ingress documentation.</p>
<p>The <code>rewrite-target</code> annotation configures the ingress such that matching paths will be rewritten to that value. Since you've specified a static value of <code>/</code>, anything matching your ingress rules will get rewritten to <code>/</code>, which is exactly the behavior you're seeing.</p>
<p>The solution is to capture the portion of the path we care about, and then use that in the <code>rewrite-target</code> annotation.  For example:</p>
<pre><code>apiVersion: networking.k8s.io/v1
kind: Ingress
metadata:
  name: myservices
  annotations:
    nginx.ingress.kubernetes.io/enable-cors: &quot;true&quot;
    nginx.ingress.kubernetes.io/cors-allow-methods: &quot;GET, POST, OPTIONS&quot;
    nginx.ingress.kubernetes.io/cors-allow-origin: '$http_origin'
    nginx.ingress.kubernetes.io/cors-allow-credentials: &quot;true&quot;
    nginx.ingress.kubernetes.io/rewrite-target: /$2
    nginx.ingress.kubernetes.io/use-regex: &quot;true&quot;
    nginx.ingress.kubernetes.io/ssl-redirect: &quot;false&quot;

spec:
  ingressClassName: nginx
  rules:
  - host: myserver.com
    http:
      paths:
      - path: /services/service_1(/|$)(.*)
        pathType: Prefix
        backend:
          service:
            name: webservice-service1
            port:
              number: 80
      - path: /services/service_2(/|$)(.*)
        pathType: Prefix
        backend:
          service:
            name: webservice-service2
            port:
              number: 80
      - path: /services(/|$)(.*)
        pathType: Prefix
        backend:
          service:
            name: webservice
            port:
              number: 80

</code></pre>
<p>Here, we've modified the match expression so that they look like:</p>
<pre><code>      - path: /services/service_1(/|$)(.*)
</code></pre>
<p>The second capture group <code>(.*)</code> captures everything after the path
portion that matches literally. We then use that capture group (<code>$2</code>,
because it's the second group) in the <code>rewrite-target</code> annotation:</p>
<pre><code>    nginx.ingress.kubernetes.io/rewrite-target: /$2
</code></pre>
<p>With this configuration in place, a request to <code>/services/service_2</code>
results in:</p>
<pre><code>This is service2.
</code></pre>
<p>But a request to <code>/services/service_2/foo/bar</code> results in:</p>
<pre><code>&lt;html&gt;&lt;head&gt;&lt;title&gt;404 Not Found&lt;/title&gt;&lt;/head&gt;&lt;body&gt;
&lt;h1&gt;Not Found&lt;/h1&gt;
The URL you requested (/foo/bar) was not found.
&lt;hr&gt;
&lt;/body&gt;&lt;/html&gt;
</code></pre>
<p>And looking at the backend server logs, we see:</p>
<pre><code>10.42.0.32 - - [21/Jan/2022:20:33:23 +0000] &quot;GET / HTTP/1.1&quot; 200 211 &quot;&quot; &quot;curl/7.79.1&quot;
10.42.0.32 - - [21/Jan/2022:20:33:45 +0000] &quot;GET /foo/bar HTTP/1.1&quot; 404 311 &quot;&quot; &quot;curl/7.79.1&quot;
</code></pre>
<p>I've updated <a href="https://github.com/larsks/k3s-nginx-example/tree/main" rel="nofollow noreferrer">my example repository</a> to match this configuration.</p>
