nginx ingress "sometimes" returns 502

5/6/2021

I serve static files through an nginx instance there's also another nginx instance acting as an ingress controller. It's provided by my cloud provider. Sometimes when I hit the page the network tab in the page inspector (in google chrome: ctrl+maj+i) mentions 502 error on some static files.

  • If I press f5 the errors disappear
  • If I press ctrl + f5, some errors reappear (not always the same files), until I press f5 gain
  • If I look up the nginx ingress logs (kubectl logs ingress_pod_name -n kube-system), I see two cases: 1. multiples lines of 502 connect() failed (111: Connection refused) while connecting to upstream, then 200 at some point, 2. multiple lines of 502 and that's it
  • If I enter the url of a static file in the browser (e.g mywebsite.cloud/static/css.css) I can always see it, repeating f5 or ctrl + f5 does not change that. But, the ingress logs shows 502 errors and ctrl + f5 shows me a "favicon.ico" 404 not found in the inspector's network tab, I don't think it's relevant but it's weird that it appears whenever there's a 502
  • Lastly my nginx instance serving the static files does not shows any 502 log errors

My service and ingress :

apiVersion: v1
kind: Service
metadata:
  name: myapp-service
  labels:
    app: myapp
spec:
  type: ClusterIP
  selector:
    app: myapp
  ports:
    - name: myapp-backend
      port: 9000
      targetPort: 9000
    - name: myapp-frontend
      port: 8080
      targetPort: 8080
    - name: myapp-nginx
      port: 8001
      targetPort: 8001
apiVersion: networking.k8s.io/v1
kind: Ingress
metadata:
  name: myapp-ingress
  labels:
      app: myapp
  annotations:
    nginx.ingress.kubernetes.io/use-regex: "true"
    nginx.ingress.kubernetes.io/proxy-body-size: "10m"
spec:
  rules:
  - host: myapp.example.cloud
    http:
      paths:
      - pathType: Prefix
        path: /core
        backend:
          service:
            name: myapp-service
            port: 
              number: 9000
      - pathType: Prefix
        path: "/core_media|core_static/"
        backend:
          service:
            name: myapp-service
            port: 
              number: 8001
      - pathType: Prefix
        path: "/"
        backend:
          service:
            name: myapp-service
            port: 
              number: 8080
-- Sheed
kubernetes
kubernetes-ingress
nginx

0 Answers