Occasional 502 responses from Kubernetes NGINX ingress

12/20/2019

Recently, I've moved my PHP website from a basic VM to Kubernetes with NGINX ingress on Google Cloud. I've noticed a strange issue where 502 errors are returned occasionally (from openresty/1.15.8.2). This seems to happen especially for people who have used the application before and is resolved for them by clearing any cache and cookies for the website. The website itself hasn't changed at all.

The strangest thing is that it only happens occasionally (a few times a day from the logs), so there are no issues in references between yml files etc.

ingress.yml:

apiVersion: extensions/v1beta1
kind: Ingress
metadata:
  name: example-ingress
  annotations:
    kubernetes.io/ingress.class: nginx
    nginx.ingress.kubernetes.io/ssl-redirect: "true"
    cert-manager.io/acme-challenge-type: http01
    cert-manager.io/cluster-issuer: letsencrypt

spec:
  rules:
    - host: "example.app"
      http:
        paths:
        - path: /
          backend:
            serviceName: example-service
            servicePort: 80
  tls:
    - hosts:
        - "example.app"
      secretName: example.app-tls

configmap.yml:

kind: ConfigMap
apiVersion: v1
metadata:
  name: nginx-config
data:
  nginx.conf: |
    events {
    }

    http {
      server {
        listen 80;
        listen [::]:80;

        root /app/public;
        index index.php index.html index.htm;
        server_name example.app;

        location / {
          try_files $uri $uri/ /index.php?$query_string;
        }

        location ~ \.css {
          add_header Content-Type text/css;
          try_files $uri $uri/ /index.php?$query_string;
        }

        location ~ \.php$ {
          include fastcgi_params;
          fastcgi_param REQUEST_METHOD $request_method;
          fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
          fastcgi_pass 127.0.0.1:9000;
        }

        proxy_connect_timeout 180s;
        proxy_send_timeout 180s;
        proxy_read_timeout 180s;
        send_timeout 180s;

        fastcgi_read_timeout 180s;
      }
    }

I can provide more yml files if thought to be relevant.

-- Wouter Florijn
google-cloud-platform
kubernetes
nginx

0 Answers