Ingress Nginx won't load resources of a Django Application

11/5/2019

so I have a django application running in K8 but my resource, like .css, won't get loaded. I always receive a 404 on them. I use Ingress-Nginx to route incoming traffic.

The ingress config looks like this:

apiVersion: networking.k8s.io/v1beta1
kind: Ingress
metadata:
  name: ingress-aboi
  annotations:
    kubernetes.io/ingress.class: nginx
    ingress.kubernetes.io/rewrite-target: /
spec:
  tls:
  - secretName: tls-cert
  rules:
  #- host: dev.<ip>.xip.io
  - http:
      paths:
      - path: /
        backend:
          serviceName: django-service
          servicePort: 8000

For the index.html I tried with leading slashes and without. Because I found this information in this stackoverflow thread here. Kubernetes Ingress Nginx loading resources 404 Sadly, it doesn't solve my problem.

Index.html

<html lang="en">
    <head>
    <title>Log in | Django site admin</title>
    <link rel="stylesheet" type="text/css" href="static/admin/css/base.css">
    <link rel="stylesheet" type="text/css" href="static/admin/css/login.css">
    <link rel="stylesheet" type="text/css" href="static/admin/css/responsive.css">
    </head>
<body>
</body>
</html>

Before I was using a nginx with this config, but I don't want to use another pod if I can handle the traffic with a ingress-nginx component alone.

nginx.conf

server {
   listen      443 ssl;
   location /media  {
       alias /website/media;
   }

   location /static {
       alias /website/static;
   }
   location / {
       uwsgi_pass  django;
       include     /etc/nginx/uwsgi_params;
       uwsgi_read_timeout 300;
       proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
       proxy_set_header X-Forwarded-Proto https;
       proxy_set_header Host $http_host;
       proxy_redirect off;
   }

So to sum it up. I can use the app but without resources. The resources are located in one of the django application pods. I checked if they are inside, and so are they. I also tried a rewrite snippet like this, to match the nginx config from before. It doesn't work either.

    nginx.ingress.kubernetes.io/configuration-snippet: |
     rewrite (.*)/media/(.*) /website/media/$2 break;
     rewrite (.*)/static/(.*) /website/static/$2 break;

By the way. If I use a host like xip.io I can't access my application at all. So I really don't know what I messed up at the end. A domain would be nice but more important would it be to be able to load the needed resources.

Any help, tips & tricks are appreatiated.

Kind regards from Berlin.

-- Stephan_Berlin
django
gunicorn
kubernetes-ingress
nginx-ingress
python

1 Answer

11/6/2019

I learned that static files should not be served by Ingess. It should be handled by a web server like nginx oder a cdn. Ingress is for routing, it should not be treated like a web server. Because I don't want to spin up and extra pod for nginx I'll use a Middleware for the Django application which is called whitenoise for now. This will deliver static files via gunicorn. I have to rethink what the best solution will be in the future and will probably use a cdn from Google or AWS.

-- Stephan_Berlin
Source: StackOverflow