Search code examples
kubernetesnginxkubernetes-ingress

How To Load Static Files Using Ingress


Lately, I have been trying to learn some Kubernetes, so I decided to create a simple Nginx page with a static image to test it holysemicolon/uwu-nginx:1.0,

I noticed when I use ingress to get the Nginx Page with ClusterType as the type of nginx service the static image won't load 404 not found, but when I use NodePort to expose the service without ingress the image loads successfully.

note that when I try to get the image directly with ingress 192.168.42.110/Holy_semicolon.png it gets fetched without any problem.

btw you will have to pass a header Host:app1.com to work, or curl -H "Host:app1.com" 192.168.42.110

nginx page

Here, after changing the service type to use type: NodePort :

nginx page good

btw the HTML page and the image are located within the same directory /var/www/html you can exec bash into the nginx container.

the static image is being called within HTML page like this <IMG SRC= "./Holy_semicolon.png">

Here are my files:

app-one.yaml:

apiVersion: apps/v1
kind: Deployment
metadata:
  name: app-one
spec:
  selector:
    matchLabels:
      app: app-one
  replicas: 1
  template:
    metadata:
      labels:
        app: app-one
    spec:
      containers:
      - name: app-one
        image: holysemicolon/uwu-nginx:1.0
        ports:
        - containerPort: 80

app-one-service.yaml:

apiVersion: v1
kind: Service
metadata:
  name: app-one
spec:
  selector:
    app: app-one
  ports:
  - name: http
    port: 80
    targetPort: 80
  type: ClusterIP
#type: NodePort

ingress.yaml:

apiVersion: networking.k8s.io/v1
kind: Ingress
metadata:
  name: my-app-ingress
  namespace: default
  #annotations:
    #nginx.ingress.kubernetes.io/use-regex: "true"
    #nginx.ingress.kubernetes.io/rewrite-target: /Enjine/$1
spec:
  rules:
  - host: app1.com  #Host Name 
    http:
      paths:
      - pathType: Prefix
        path: /
        backend:
          service:
            name: app-one
            port:
              number: 80
$  kubectl get all
NAME                           READY   STATUS    RESTARTS   AGE
pod/app-one-78b7874867-g2b92   1/1     Running   0          6m46s

NAME                 TYPE        CLUSTER-IP    EXTERNAL-IP   PORT(S)   AGE
service/kubernetes   ClusterIP   10.43.0.1     <none>        443/TCP   165m
service/app-one      ClusterIP   10.43.23.19   <none>        80/TCP    6m42s

NAME                      READY   UP-TO-DATE   AVAILABLE   AGE
deployment.apps/app-one   1/1     1            1           6m46s

NAME                                 DESIRED   CURRENT   READY   AGE
replicaset.apps/app-one-78b7874867   1         1         1       6m46s
$ kubectl get node -o wide
NAME       STATUS   ROLES                  AGE    VERSION        INTERNAL-IP      EXTERNAL-IP   OS-IMAGE                KERNEL-VERSION                CONTAINER-RUNTIME
iltafahs   Ready    control-plane,master   3h2m   v1.28.5+k3s1   192.168.42.110   <none>        CentOS Linux 7 (Core)   3.10.0-1160.99.1.el7.x86_64   containerd://1.7.11-k3s2


Solution

  • I think I found the problem, after a lot of debugging I tried to open the nginx page within Opera browser and it worked, Hence I was working with Edge.

    I compared the network page in both browsers to see why it works on one and the other won't, and I discovered that the problem is from the Host header, as you can see in the below images:

    Edge: Host: 192.168.42.110 enter image description here

    Opera: Host: app1.com Opera

    I set the host header using a browser extension, so when I call the master node IP 192.168.42.110, the ingress will know which service to serve, It's like the host header Host:app1.com is being set only for the main page, but for other sub URL calls like http://192.168.42.110/Holy_semicolon.png the header is not being set, "just in Edge"

    #EDIT

    It turns out the Host header problem was from the used extension, In Edge I was working with Modify Header Value (HTTP Headers) extension, but when I replaced it with ModHeader - Modify HTTP headers ,It worked. which is the same one I used in Opera.