<p>UPDATE:</p>
<p>I have domain my.shops.de which will be internal forward all traffic to kubernetes service &quot;shop-service&quot;.</p>
<p>On this domains I am managing different shops for my clients. eg. my.shops.de/11111 for client with id 11111 and other eg. my.shops.de/22222 for client with id 22222.</p>
<p>Now the client with id 11111 have own domain &quot;example.com&quot; which should be referenced to &quot;my.shops.de/11111&quot; domain.</p>
<p>To achieve this, client has created a CNAME record with &quot;my.shops.de.&quot; as  value.</p>
<p>Now on my side, I have some how to tell ingress, if I got request from the host &quot;example.com/(<em>)&quot; please forward all request to my internal kubernetes service &quot;shop-service&quot; with path /11111/(</em>).</p>
<p>I'm trying to rewrite the path to the backend service with nginx ingress on kubernetes.</p>
<p>i have service in kubernetes which called shop-service</p>
<p>What I need is this:</p>
<ul>
<li><p><a href="http://example.com" rel="nofollow noreferrer">http://example.com</a> =&gt; shop-service/11111</p>
</li>
<li><p><a href="http://example.com/path/bar" rel="nofollow noreferrer">http://example.com/path/bar</a> =&gt; shop-service/11111/path/bar</p>
</li>
<li><p><a href="http://example.com/any/path?with=query" rel="nofollow noreferrer">http://example.com/any/path?with=query</a> =&gt; shop-service/11111/any/path?with=query</p>
</li>
<li><p><a href="http://other-example.com" rel="nofollow noreferrer">http://other-example.com</a> =&gt; shop-service/22222</p>
</li>
<li><p><a href="http://other-example.com/path/bar" rel="nofollow noreferrer">http://other-example.com/path/bar</a> =&gt; shop-service/22222/path/bar</p>
</li>
<li><p><a href="http://other-example.com/any/path?with=query" rel="nofollow noreferrer">http://other-example.com/any/path?with=query</a> =&gt; shop-service/22222/any/path?with=query</p>
</li>
</ul>
<p>Is this possible nginx ingress?</p>
<p>I've tried it with this code, but it doesn't work.</p>
<pre><code>apiVersion: networking.k8s.io/v1
kind: Ingress
metadata:
  name: cname-redirect-ingress
  namespace: shop
  annotations:
    kubernetes.io/ingress.class: &quot;nginx&quot;
    nginx.ingress.kubernetes.io/rewrite-target: /11111$2
spec:  
  rules:
    - host: example.com
      http:
        paths:
          - path: (/|$)(.*)
            pathType: Prefix
            backend:
              service:
                name: shop-service
                port:
                  number: 80
---
</code></pre>

user1167253
<p>Try something like this:</p>
<pre><code>apiVersion: networking.k8s.io/v1
kind: Ingress
metadata:
  name: cname-redirect-ingress
  namespace: shop
  annotations:
    kubernetes.io/ingress.class: &quot;nginx&quot;
    cert-manager.io/cluster-issuer: &quot;letsencrypt&quot;
    acme.cert-manager.io/http01-edit-in-place: &quot;true&quot;
    kubernetes.io/ingress.class: &quot;nginx&quot;
    nginx.ingress.kubernetes.io/server-snippet: |
      if ($host = 'example.com') {
      rewrite ^/(/?)$ https://$host/shop-service/11111$1 permanent;
      rewrite ^/path/bar(/?)$ https://$host/shop-service/11111/path/bar$1 permanent;
      rewrite ^/(/?)$ /shop-service/11111$1 break;
      rewrite ^/path/bar(/?)$ /shop-service/11111$1 break;    
      }
      if ($host = 'other.example.com') {
      rewrite ^/(/?)$ https://$host/shop-service/22222$1 permanent;
      rewrite ^/path/bar(/?)$ https://$host/shop-service/22222/path/bar$1 permanent;
      rewrite ^/(/?)$ /shop-service/22222$1 break;
      rewrite ^/path/bar(/?)$ /shop-service/22222$1 break;
      }
spec:
  ingressClassName: nginx
  rules:
    - host: example.com
      http:
        paths:
          - path: /
            pathType: Prefix
            backend:
              service:
                name: shop-service
                port:
                  number: 80
    - host: other.example.com
      http:
        paths:
          - path: /
            pathType: Prefix
            backend:
              service:
                name: shop-service
                port:
                  number: 80
</code></pre>
<p>Use <code>permanent</code> for explicit redirect, <code>break</code> for implicit rewrite.<br />
I advise against having path as anything but <code>/</code> (unless you have specific route you want to restrict to) and from using <code>rewrite-target</code>, because at least on AWS first blocks you off anything above subpath due to how AWS load balancer works, and latter leads to redirect loops, from my experience.<br />
And yes, having multiple hosts pointing to the same service is perfectly fine.</p>
<p>Be sure to include this if you intend to use HTTPS, since without this you may have redirect loop headache:</p>
<pre><code>spec:
  tls: 
  - secretName: shop-tls
    hosts:
    - example.com
    - other.example.com
</code></pre>
<p>Be sure to check <a href="https://getbetterdevops.io/k8s-ingress-with-letsencrypt/" rel="nofollow noreferrer">here</a> for prerequisites of using the above if using LE fits your needs (if you install everything correctly, it does the rest except announcing the secret for you in the ingress manifest), unless you already have compatible SSL certificate you can put into secret (AWS ACM, for example, doesn't allow you to just export certificates left and right).</p>
