title: Kubernetes Multiple Path Rewrites
body: <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>