<p>I am trying to use Kubernetes Ingress Nginx Controller and running a simple nginx server in AWS EKS.</p>
<p>Browser (https) --&gt; Route 53 (DNS) --&gt; CLB --&gt; nginx Ingress (Terminate TLS) --&gt; Service --&gt; POD</p>
<p>But I am receiving 404 error in browser (url used: <a href="https://example.com/my-nginx" rel="nofollow noreferrer">https://example.com/my-nginx</a>):</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;center&gt;&lt;h1&gt;404 Not Found&lt;/h1&gt;&lt;/center&gt;
&lt;hr&gt;&lt;center&gt;nginx/1.19.10&lt;/center&gt;
&lt;/body&gt;
&lt;/html&gt;
</code></pre>
<p>and in ingress logs (kubectl logs -n nginx-ingress nginx-ingress-nginx-controller-6db6f85bc4-mfpwx), I can see below:</p>
<p>192.168.134.181 - - [24/Apr/2021:19:02:01 +0000] &quot;GET /my-nginx HTTP/2.0&quot; 404 154 &quot;-&quot; &quot;Mozilla/5.0 (Windows NT 10.0; Win64; x64; rv:88.0) Gecko/20100101 Firefox/88.0&quot; 219 0.002 [eshop-dev-my-nginx-9443] [] 192.168.168.105:80 154 0.000 404 42fbe692a032bb40bf193954526369cd</p>
<p>Here is my deployment yaml:</p>
<pre><code>apiVersion: apps/v1
kind: Deployment
metadata:
  name: my-nginx
  namespace: eshop-dev
spec:
  selector:
    matchLabels:
      run: my-nginx
  replicas: 2
  template:
    metadata:
      labels:
        run: my-nginx
    spec:
      containers:
      - name: my-nginx
        image: nginx
        ports:
        - containerPort: 80
</code></pre>
<p>Service yaml:</p>
<pre><code>apiVersion: v1
kind: Service
metadata:
  namespace: eshop-dev
  name: my-nginx
spec:
  selector:
    run: my-nginx
  ports:
    - name: server
      port: 9443
      targetPort: 80
      protocol: TCP
</code></pre>
<p>and ingress yaml:</p>
<pre><code>apiVersion: networking.k8s.io/v1
kind: Ingress
metadata:
  name: test-ingress
  namespace: eshop-dev
  annotations:
    kubernetes.io/ingress.class: &quot;nginx&quot;
spec:
  rules:
  - host: example.com
    http:
      paths:
      - path: /my-nginx
        pathType: ImplementationSpecific
        backend:
          service:
            name: my-nginx
            port:
                number: 9443
  tls:
  - hosts:
    - example.com
    secretName: externaluicerts
</code></pre>
<p>I have verified that service returns the desired output, when used with port forwarding:</p>
<pre><code>kubectl -n eshop-dev port-forward service/my-nginx 9443:9443
</code></pre>
<p>I'm not sure if the ingress is incorrectly configured or if it is another problem.Thanks in advance for the help!</p>
<p><a href="https://i.stack.imgur.com/SJllT.png" rel="nofollow noreferrer">nginx-port-forward</a></p>
<p>Here is the output of kubectl get ingress -n eshop-dev test-ingress -o yaml</p>
<pre><code>kubectl get ingress -n eshop-dev test-ingress -o yaml
Warning: extensions/v1beta1 Ingress is deprecated in v1.14+, unavailable in v1.22+; use networking.k8s.io/v1 Ingress
apiVersion: extensions/v1beta1
kind: Ingress
metadata:
  annotations:
    kubectl.kubernetes.io/last-applied-configuration: |
      {&quot;apiVersion&quot;:&quot;networking.k8s.io/v1&quot;,&quot;kind&quot;:&quot;Ingress&quot;,&quot;metadata&quot;:{&quot;annotations&quot;:{&quot;kubernetes.io/ingress.class&quot;:&quot;nginx&quot;},&quot;name&quot;:&quot;test-ingress&quot;,&quot;namespace&quot;:&quot;eshop-dev&quot;},&quot;spec&quot;:{&quot;rules&quot;:[{&quot;host&quot;:&quot;example.com&quot;,&quot;http&quot;:{&quot;paths&quot;:[{&quot;backend&quot;:{&quot;service&quot;:{&quot;name&quot;:&quot;my-nginx&quot;,&quot;port&quot;:{&quot;number&quot;:9443}}},&quot;path&quot;:&quot;/my-nginx&quot;,&quot;pathType&quot;:&quot;ImplementationSpecific&quot;}]}}],&quot;tls&quot;:[{&quot;hosts&quot;:[&quot;example.com&quot;],&quot;secretName&quot;:&quot;externaluicerts&quot;}]}}
    kubernetes.io/ingress.class: nginx
  creationTimestamp: &quot;2021-04-24T13:16:21Z&quot;
  generation: 13
  managedFields:
  - apiVersion: networking.k8s.io/v1beta1
    fieldsType: FieldsV1
    fieldsV1:
      f:status:
        f:loadBalancer:
          f:ingress: {}
    manager: nginx-ingress-controller
    operation: Update
    time: &quot;2021-04-24T13:16:40Z&quot;
  - apiVersion: extensions/v1beta1
    fieldsType: FieldsV1
    fieldsV1:
      f:metadata:
        f:annotations: {}
    manager: kubectl-client-side-apply
    operation: Update
    time: &quot;2021-04-24T13:18:36Z&quot;
  - apiVersion: networking.k8s.io/v1
    fieldsType: FieldsV1
    fieldsV1:
      f:metadata:
        f:annotations:
          f:kubectl.kubernetes.io/last-applied-configuration: {}
          f:kubernetes.io/ingress.class: {}
      f:spec:
        f:rules: {}
        f:tls: {}
    manager: kubectl-client-side-apply
    operation: Update
    time: &quot;2021-04-24T16:33:47Z&quot;
  name: test-ingress
  namespace: eshop-dev
  resourceVersion: &quot;7555944&quot;
  selfLink: /apis/extensions/v1beta1/namespaces/eshop-dev/ingresses/test-ingress
  uid: a7694655-20c6-48c7-8adc-cf3a53cf2ffe
spec:
  rules:
  - host: example.com
    http:
      paths:
      - backend:
          serviceName: my-nginx
          servicePort: 9443
        path: /my-nginx
        pathType: ImplementationSpecific
  tls:
  - hosts:
    - example.com
    secretName: externaluicerts
status:
  loadBalancer:
    ingress:
    - hostname: xxxxxxxxxxxxxxxxdc75878b2-433872486.eu-west-1.elb.amazonaws.com
</code></pre>

<p>From the image you posted of the nginx-port-forward, I see you went on <code>localhost:9443</code> directly, which means the Nginx server you are trying to access serve its content under <code>/</code></p>
<p>But in the ingress definition, you define that the service will be served with <code>path: /my-nginx</code>. This could be the problem, as you are requesting <code>https://example.com/my-nginx</code> which will basically go to <code>my-nginx:9443/my-nginx</code> and, depending on the Pod behind this service, it could return a 404 if there's nothing at that path.</p>
<p>To test if the problem is what I specified above, you have a few options:</p>
<ul>
<li>easiest one, remove <code>path: /my-nginx</code> an, instead, go with <code>path: /</code>. You could also specify <code>pathType: Prefix</code> which means that everything matching the subPath specified will be served by the service.</li>
<li>Add a rewrite target, which is necessary if you want to serve a service at a different path from the one expected by the application.</li>
</ul>
<p>Add an annotation similar to the following:</p>
<pre><code>apiVersion: networking.k8s.io/v1
kind: Ingress
metadata:
  name: test-ingress
  namespace: eshop-dev
  annotations:
    kubernetes.io/ingress.class: &quot;nginx&quot;
    # this will rewrite request under / + second capture group
    nginx.ingress.kubernetes.io/rewrite-target: /$2
spec:
  rules:
  - host: example.com
    http:
      paths:
        # this will serve all paths under /my-nginx and capture groups for regex annotations
      - path: /my-nginx(/|$)(.*)
        pathType: ImplementationSpecific
        backend:
          service:
            name: my-nginx
            port:
              number: 9443
  tls:
  - hosts:
    - example.com
    secretName: externaluicerts
</code></pre>
<ul>
<li>Configure your application to know that it will be served under the path you desire. This is often the better approach, as frontend applications should almost always be served under the path that they expect to be.</li>
</ul>
<p>From the info you posted, I think this is the problem an once fixed, your setup <strong>should</strong> work.</p>
<hr />
<p>If you are curious about rewrite targets or how paths work in an Ingress, here is some documentation:</p>
<p>Rewrites ( <a href="https://kubernetes.github.io/ingress-nginx/examples/rewrite/#rewrite" rel="nofollow noreferrer">https://kubernetes.github.io/ingress-nginx/examples/rewrite/#rewrite</a> )</p>
<p>Path types ( <a href="https://kubernetes.io/docs/concepts/services-networking/ingress/#path-types" rel="nofollow noreferrer">https://kubernetes.io/docs/concepts/services-networking/ingress/#path-types</a> )</p>
<hr />
<p><strong>Update</strong></p>
<p>About why configuring the application to directly serve its content at the path specified in the Ingress (basically to know at which path it will served) is the best solution:</p>
<p>Let's say you serve a complex application in your Pod which will serve its content under /. The main page will try to load several other resources like css, js code and so on, everything from the root directory. Basically, if I open <code>/</code>, the app will load also:</p>
<pre><code>/example.js
/my-beautiful.css
</code></pre>
<p>Now, if I serve this app behind an ingress at another path, let's say under <code>/test/</code> <strong>with a rewrite target</strong>, the main page will work, because:</p>
<pre><code>/test/ --&gt; /  # this is my rewrite rule
</code></pre>
<p>but then, the page will request <code>/example.js</code>, and the rewrite works in one direction only, so the browser will request a resource which will go in 404, because the request should have been <code>/test/example.js</code> (as that would rewrite to remove the /test part of the path)</p>
<p>So, with frontend applications, rewrite targets may not be enough, mostly if the applications request resources with absolute paths. With just REST API or single requests instead, rewrites usually works great.</p>
