<p>The goal is to support the following two use cases:</p>
<ol>
<li><p>Our organization has one shared domain, <code>shared.domain.com</code>, so each service needs to have a unique path. For example, for our service, <code>myservice</code>, we choose <code>/myservice</code>. So we want a request to <code>shared.domain.com/myservice/users</code> to be rewritten and routed to our service with the path <code>/user</code>.</p>
</li>
<li><p>We also have a service-specific domain, <code>myservice.domain.com</code>. In this case, we don't need a rewrite, per se: a request to <code>myservice.domain.com/users</code> should be passed through to our service with the path <code>/user</code>. However, since we need a rewrite to satisfy #1 above, we need to work within the &quot;framework&quot; of a rewrite for this use case as well.</p>
</li>
</ol>
<p>We're using a Kubernetes Ingress NGINX rewrite (<code>nginx.ingress.kubernetes.io/rewrite-target</code>). Use case #1 is working fine. However, we can't figure out how to get #2 working.</p>
<p>For now we had to use the same path for both domains which is not ideal because it's not backwards compatible for anyone who was calling <code>myservice.domain.com/users</code>. Now they have to call <code>myservice.domain.com/myservice/users</code>. We could make a code change to make this backwards compatible for our callers, but again that's not ideal.</p>
<p>Here's our configuration:</p>
<pre class="lang-yaml prettyprint-override"><code># ingress.yaml
apiVersion: networking.k8s.io/v1beta1
kind: Ingress
metadata:
  name: $APP_NAME
  annotations:
    kubernetes.io/ingress.class: nginx
    nginx.ingress.kubernetes.io/use-regex: &quot;true&quot;
    service.beta.kubernetes.io/aws-load-balancer-backend-protocol: http
    service.beta.kubernetes.io/aws-load-balancer-ssl-ports: 'https'
    nginx.ingress.kubernetes.io/from-to-www-redirect: &quot;true&quot;
    nginx.ingress.kubernetes.io/rewrite-target: /$2
    nginx.ingress.kubernetes.io/proxy-redirect-from: &quot;http://&quot;
    nginx.ingress.kubernetes.io/proxy-redirect-to: &quot;https://&quot;
spec:
  rules:
    - host: shared.domain.com
      http:
        paths:
          - path: /myservice(/|$)(.*)
            backend:
              serviceName: $APP_NAME
              servicePort: http
    - host: myservice.domain.com
      http:
        paths:
          - path: &quot;(/|$)(.*)&quot; # Doesn't work
            backend:
              serviceName: $APP_NAME
              servicePort: http

</code></pre>
<p>We used these docs as a reference: <a href="https://kubernetes.github.io/ingress-nginx/examples/rewrite/#rewrite-target" rel="nofollow noreferrer">https://kubernetes.github.io/ingress-nginx/examples/rewrite/#rewrite-target</a>.</p>

David Good
<p>You need to use this regex in the path for the <code>myservice.domain.com</code>:</p>
<p><code>/*(/|$)(.*)</code></p>
<p>Also you're using <code>v1beta1</code> api which is already depricated and will be unavailable soon:</p>
<blockquote>
<p>Warning: networking.k8s.io/v1beta1 Ingress is deprecated in v1.19+,
unavailable in v1.22+; use networking.k8s.io/v1 Ingress</p>
</blockquote>
<p>Below <code>ingress.yaml</code> written using <code>v1</code> with correct regex for <code>myservice.domain.com</code>:</p>
<pre><code>apiVersion: networking.k8s.io/v1
kind: Ingress
metadata:
  name: $APP_NAME
  annotations:
    kubernetes.io/ingress.class: nginx
    nginx.ingress.kubernetes.io/use-regex: &quot;true&quot;
    service.beta.kubernetes.io/aws-load-balancer-backend-protocol: http
    service.beta.kubernetes.io/aws-load-balancer-ssl-ports: 'https'
    nginx.ingress.kubernetes.io/from-to-www-redirect: &quot;true&quot;
    nginx.ingress.kubernetes.io/rewrite-target: /$2
    nginx.ingress.kubernetes.io/proxy-redirect-from: &quot;http://&quot;
    nginx.ingress.kubernetes.io/proxy-redirect-to: &quot;https://&quot;
spec:
  rules:
    - host: shared.domain.com
      http:
        paths:
          - path: /myservice(/|$)(.*)
            pathType: Prefix
            backend:
              service:
                name: $APP_NAME
                port:
                  number: 80
    - host: myservice.domain.com
      http:
        paths:
          - path: /*(/|$)(.*)
            pathType: Prefix
            backend:
              service:
                name: $APP_NAME
                port:
                  number: 80
</code></pre>
