<p>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.</p>
<p>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:</p>
<pre><code>*3 recv() failed (104: Connection reset by peer) while reading response header from upstream
</code></pre>
<p>I already have 127.0.0.1 and localhost added to the allowed hosts in django settings.</p>
<p>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.</p>
<pre><code>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
</code></pre>
<p>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.</p>

Johnny Pastrami
<blockquote>
<p>I already have 127.0.0.1 and localhost added to the allowed hosts</p>
</blockquote>
<p>This is not the correct configuration for this setup because the connection is not made using <code>127.0.0.1</code> -- it's made using the hostname configured in NGINX e.g. <code>my-django-svc</code> so you must add <em>that</em> to the allowed_hosts setting.</p>
<pre><code>ALLOWED_HOSTS = ['my-django-svc']
</code></pre>
