skip to Main Content

I have a pretty simple setup, a single django container in a pod and an nginx container in another pod. I’m using nginx because I want to move the django app into production and I need an actual web server like nginx to put an ssl cert on the site.

The problem is that it seems like django is rejecting all of the traffic from the nginx container as the web browser gets 502 bad gateway error when browsing to localhost and the nginx logs show:

*3 recv() failed (104: Connection reset by peer) while reading response header from upstream

I already have 127.0.0.1 and localhost added to the allowed hosts in django settings.

Below is the kubernetes file with the nginx config that I’m loading through a config map. I’ve tried about 30 different nginx.conf configurations, this is just the most recent and simplest one.

apiVersion: v1
kind: Service
metadata:
  name: my-nginx-svc
  labels:
    app: nginx
spec:
  type: LoadBalancer
  ports:
  - port: 80
  selector:
    app: nginx
---
apiVersion: apps/v1
kind: Deployment
metadata:
  name: my-nginx
  labels:
    app: nginx
spec:
  replicas: 1
  selector:
    matchLabels:
      app: nginx
  template:
    metadata:
      labels:
        app: nginx
    spec:
      containers:
        - name: nginx
          image: nginx:latest
          ports:
          - containerPort: 80
          volumeMounts:
          - name: test-vol
            mountPath: /etc/nginx/conf.d
            readOnly: True
      volumes: 
      - name: test-vol
        configMap:
          name: nginx-conf
---
apiVersion: v1
kind: ConfigMap
metadata:
  name: nginx-conf
  labels:
    app: config
data:
  nginx.conf: |
    server {
      server_name               my-django-svc;
      listen                    80;
      location / {
        proxy_pass              http://my-django-svc:8000;
        proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
        proxy_set_header Host $http_host;
        proxy_redirect off;
      }
    }

---
apiVersion: v1
kind: Service
metadata:
  name: my-django-svc
  labels:
    app: web
spec:
  type: LoadBalancer
  ports:
  - port: 8000
  selector:
    app: backend
---
apiVersion: apps/v1
kind: Deployment
metadata:
  name: backend
spec:
  selector:
    matchLabels:
      app: web
      tier: backend
      track: stable
  replicas: 1
  template:
    metadata:
      labels:
        app: web
        tier: backend
        track: stable
    spec:
      containers:
        - name: web
          image: localhost:5000/kubernetes/web
          ports:
            - name: http
              containerPort: 8000
          imagePullPolicy: Always

I am out of ideas on how to narrow this problem down any further. I saw some other people using wsgi on top of their django container but I’m not sure if it’s necessary or what the benefits are.

2

Answers


  1. I already have 127.0.0.1 and localhost added to the allowed hosts

    This is not the correct configuration for this setup because the connection is not made using 127.0.0.1 — it’s made using the hostname configured in NGINX e.g. my-django-svc so you must add that to the allowed_hosts setting.

    ALLOWED_HOSTS = ['my-django-svc']
    
    Login or Signup to reply.
  2. You are trying to access this nginx application using my-django-svc domain, if no, than please be specific like server_name example.com in nginx.conf. If you are not sure than drop server_name in nginx.conf file.

    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search