<p>I have an app set up which can be contacted via the service-IP, but not using the Ingress Rule.</p>
<p>Consider the following Ingress Manifest:</p>
<pre><code>apiVersion: networking.k8s.io/v1
kind: Ingress
metadata:
  name: myapp
  namespace: default
  annotations:
    kubernetes.io/ingress.class: public
    cert-manager.io/cluster-issuer: letsencrypt-prod
    nginx.ingress.kubernetes.io/rewrite-target: /$0
    nginx.ingress.kubernetes.io/force-ssl-redirect: &quot;true&quot;
spec:
  tls:
  - secretName: letsencrypt-prod
    hosts:
    - my.host.net
  rules:
  - host: my.host.net
    http:
      paths:
      - path: /myapp(/|$)(.*)
        pathType: Prefix
        backend:
          service:
            name: myapp
            port:
              number: 1234
</code></pre>
<p>The relevant service is up and running. Doing a <code>curl 10.152.183.91/myapp/path/</code> with <code>10.152.183.91</code> being the service IP gives the desired result.</p>
<p>When I go through <code>curl my.host.net/myapp/path/</code> however, I get a <code>308 Permanent Redirect</code>. Other apps on the same cluster are running as expected, so the cluster itself as well as nginx-ingress and CoreDNS are doing their job.</p>
<p>Where did I go wrong? Is the <code>nginx.ingress.kubernetes.io/rewrite-target: /$0</code> wrong?</p>

petwri
<p>First and foremost, you will need to change the:</p>
<ul>
<li>from: <code>nginx.ingress.kubernetes.io/rewrite-target: /$0</code></li>
<li>to: <code>nginx.ingress.kubernetes.io/rewrite-target: /$2</code></li>
</ul>
<p>Explanation:</p>
<blockquote>
<h2><a href="https://kubernetes.github.io/ingress-nginx/examples/rewrite/#rewrite-target" rel="nofollow noreferrer">Rewrite Target</a></h2>
<h3>Note</h3>
<p><a href="https://www.regular-expressions.info/refcapture.html" rel="nofollow noreferrer">Captured groups</a> are saved in numbered placeholders, chronologically, in the form $1, $2 ... $n. These placeholders can be used as parameters in the <code>rewrite-target</code> annotation.</p>
<p>Create an Ingress rule with a rewrite annotation:</p>
<pre class="lang-yaml prettyprint-override"><code>apiVersion: networking.k8s.io/v1
kind: Ingress
metadata:
  annotations:
    nginx.ingress.kubernetes.io/use-regex: &quot;true&quot;
     nginx.ingress.kubernetes.io/rewrite-target: /$2
  name: rewrite
  namespace: default
spec:
  ingressClassName: nginx
  rules:
  - host: rewrite.bar.com
    http:
      paths:
      - path: /something(/|$)(.*)
        pathType: Prefix
        backend:
          service:
            name: http-svc
            port: 
              number: 80
</code></pre>
<p>In this ingress definition, any characters captured by <code>(.*)</code> will be assigned to the placeholder <code>$2</code>, which is then used as a parameter in the <code>rewrite-target</code> annotation.</p>
<p>For example, the ingress definition above will result in the following rewrites:</p>
<ul>
<li><code>rewrite.bar.com/something</code> rewrites to <code>rewrite.bar.com/</code></li>
<li><code>rewrite.bar.com/something/</code> rewrites to <code>rewrite.bar.com/</code></li>
<li><code>rewrite.bar.com/something/new</code> rewrites to <code>rewrite.bar.com/new</code></li>
</ul>
<p>-- <em><a href="https://kubernetes.github.io/ingress-nginx/examples/rewrite/" rel="nofollow noreferrer">Kubernetes.github.io: Ingress-nginx: Examples: Rewrite</a></em></p>
</blockquote>
<hr />
<p>As for the <code>curl</code> part. By running exactly the same as you did:</p>
<ul>
<li><code>curl my.host.net/myapp/path/</code></li>
</ul>
<p>You will receive a <code>308 Permanent Redirect</code>.</p>
<p><code>curl</code> by default is not following the redirection. Try with <code>curl -L</code>.</p>
<p>Example with setup similar to yours:</p>
<pre class="lang-bash prettyprint-override"><code>❯ curl kruklabs.internal/myapp/      
&lt;html&gt;
&lt;head&gt;&lt;title&gt;308 Permanent Redirect&lt;/title&gt;&lt;/head&gt;
&lt;body&gt;
&lt;center&gt;&lt;h1&gt;308 Permanent Redirect&lt;/h1&gt;&lt;/center&gt;
&lt;hr&gt;&lt;center&gt;nginx&lt;/center&gt;
&lt;/body&gt;
&lt;/html&gt;

❯ curl kruklabs.internal/myapp/ -L -k  
Here is the nginx container!
</code></pre>
<blockquote>
<p>A side note!</p>
<p>You could also try to contact <code>Ingress</code> directly with <code>HTTPS</code>.</p>
</blockquote>
