kubernetesnginxkubernetes-ingressamazon-eksingress-controller

Nginx incorrectly getting web files locally instead of where it is proxying the request to


Background:

I have deployed various applications into AWS/Kubernetes. Some of these applications will ASP.NET Core MVC web applications, with Kestrel as their web server. Each of them will be running in their own pod.

All the applications are correctly running in their respective pods with communications to them and between them working fine. In addition to these applications, I have also deployed an Nginx Ingress Controller that will handle traffic being sent to some of these web applications. This is where the problem is.

Issue:

The Kubernetes Ingress resource I am deploying is causing problems for me. When connecting to the web application through Nginx proxying it, the browser has not loaded any javascript, css, images, etc. It's just the unformatted login page of my web app.

Note that I am not deploying the built-in Kubernetes Ingress resource. I am deploying the Nginx Custom Resource called "VirtualServer", but I can only imagine the same would happen with the built-in Kubernetes Ingress resource. Below is an example of the resource I am deploying:

kind: VirtualServer
apiVersion: k8s.nginx.org/v1
metadata: 
  name: some-name-nginx-dmz-vs
  namespace: dmz
spec: 
  host: some-host-name-that-directs-to-nginx-pod.com
  upstreams: 
  - name: com-dmz-webinterfaceclient-service-1-us
    service: com-dmz-webinterfaceclient-service
    port: 80
  - name: com-dmz-webinterfaceclient-service-2-us
    service: com-dmz-webinterfaceclient-service
    port: 443
  routes: 
  - path: /webinterfaceUIP
    action: 
      proxy: 
        upstream: com-dmz-webinterfaceclient-service-1-us
        requestHeaders: 
            pass: true
        rewritePath: /
  - path: /webinterfaceUIP2
    action: 
      proxy: 
        upstream: com-dmz-webinterfaceclient-service-2-us
        requestHeaders: 
            pass: true
        rewritePath: /
---

The logs from the Ingress Controller errors complaining about missing files. It seams to be looking in its own local pod, instead of the remote pod it is actually proxying the request to. Here is an example of the errors:

2023/06/20 14:28:46 [error] 114#114: *55067 open() "/etc/nginx/html/js/wtk.api.view.js" failed (2: No such file or directory)

Are there any ideas on how I can fix this issue? Perhaps there is a way to set the content root that could for the files to be found? The main goal here is just proxying the request, in the future, more will be added to the Nginx config, but I am not there just yet.

As a side note:

When I Connect directly to the pod (so not going through Nginx), it correctly loads the page with javascript, CSS, etc. Only when going through Nginx is there an issue with files not being found.


Solution

  • I have determined what was missing. Adding in an additional empty path gets it to load the files as expected, CSS, javascript, image, etc:

    kind: VirtualServer
    apiVersion: k8s.nginx.org/v1
    metadata: 
      name: some-name-nginx-dmz-vs
      namespace: dmz
    spec: 
      host: some-host-name-that-directs-to-nginx-pod.com
      upstreams: 
      - name: com-dmz-webinterfaceclient-service-1-us
        service: com-dmz-webinterfaceclient-service
        port: 80
      routes: 
      - path: /
        action: 
          proxy: 
            upstream: com-dmz-webinterfaceclient-service-1-us
            requestHeaders: 
                pass: true
            rewritePath: /
      - path: /webinterfaceUIP
        action: 
          proxy: 
            upstream: com-dmz-webinterfaceclient-service-1-us
            requestHeaders: 
                pass: true
            rewritePath: /
    ---