NGINX Container Not Loading Static Files using Traefik / Kubernetes

12/22/2018

I am running the Traefik Ingress Controller on Kubernetes (AKS). I've successfully deployed my Django application using the Traefik Ingress but it's not currently loading any static files (and therefore the styling isn't working).

Static files are served from a custom NGINX container with /static. So if my domain name is xyz.com, static is served from xyz.com/static.

apiVersion: v1
kind: Service
metadata:
  name: nginxstaticservice
  labels:
    app: nginxstatic
spec:
  selector:
    k8s-app: traefik-ingress-lb
  ports:
    - name: http
      port: 80
      targetPort: 80
      protocol: TCP
  selector:
    app: nginxstatic

---

apiVersion: extensions/v1beta1
kind: Ingress
metadata:
  name: nginxstatic-ingress
  annotations:
    kubernetes.io/ingress.class: traefik
    traefik.frontend.rule.type: PathPrefixStrip
    # traefik.ingress.kubernetes.io/frontend-entry-points: http,https
spec:
  rules:
  - host: xyz.com
    http:
      paths:
      - path: /static
        backend:
          serviceName: nginxstaticservice
          servicePort: http

---
apiVersion: apps/v1
kind: Deployment
metadata:
  name: nginxstatic-deployment
  labels:
    app: nginxstatic
spec:
  replicas: 1
  selector:
    matchLabels:
      app: nginxstatic
  template:
    metadata:
      labels:
        app: nginxstatic
    spec:
      containers:
      - name: nginxstatic
        image: nginxstatic:latest
        ports:
        - containerPort: 80
      imagePullSecrets:

This is the default.conf running on the NGINX container (this was previously working in a Website configuration.

server {
   listen                      80;
   server_name                 _;
   client_max_body_size        200M;
   set                         $cache_uri $request_uri;

   location                    = /favicon.ico { log_not_found off; access_log off; }
   location                    = /robots.txt  { log_not_found off; access_log off; }
   ignore_invalid_headers      on;
   add_header                  Access-Control-Allow_Origin *;

   location /static {
       autoindex on;
       alias /static;
   }

   location /media {
       autoindex on;
       alias /media;
   }

   access_log                  /var/log/nginx/access.log;
   error_log                   /var/log/nginx/error.log;
}
-- Rutnet
kubernetes
nginx
traefik-ingress

1 Answer

12/22/2018

Resolved in comments, PathPrefixStrip was used incorrectly which caused Nginx to see different paths than expected.

-- coderanger
Source: StackOverflow