Search code examples
djangodockernginxdocker-composegunicorn

django + nginx on docker bad request (400)


I'm trying to get django (on gunicorn) and nginx running with docker. Unfortunately, I keep getting a Bad Request (400) error after I run docker-compose up -d --build. Help.

I have tried changing directories, directory names, volumes, networks and exposed ports to no avail. I also tried adding and removing server_name in my nginx.conf file.

In settings.py I have:

DEBUG = False

ALLOWED_HOSTS = ['127.0.0.1', 'localhost']

This is my docker-compose.yml file:

version: '3.3'

services:
  web:
    build: ./app
    command: gunicorn my_server.wsgi:application --bind 0.0.0.0:8000
    volumes:
      - ./app/:/usr/src/app/
    expose:
      - 8000
    environment:
      - DATABASE=${DATABASE}
      - SECRET_KEY=${SECRET}
      - SQL_DATABASE=${POSTGRES_USER}
      - SQL_ENGINE=django.db.backends.postgresql
      - SQL_HOST=${POSTGRES_HOST}
      - SQL_PASSWORD=${POSTGRES_PASSWORD}
      - SQL_PORT=${POSTGRES_PORT}
      - SQL_USER=${POSTGRES_USER}
      - SU_NAME=${SU_NAME}
      - SU_EMAIL=${SU_EMAIL}
      - SU_PASSWORD=${SU_PASSWORD}
    depends_on:
      - db
    logging:
      driver: "json-file"
      options:
        max-size: "100k"
        max-file: "20"

  db:
    image: postgres:11.2-alpine
    volumes:
      - postgres_data:/var/lib/postgresql/data/
    environment:
      - POSTGRES_PASSWORD=${POSTGRES_PASSWORD}
      - POSTGRES_USER=${POSTGRES_USER}
    logging:
      driver: "json-file"
      options:
        max-size: "100k"
        max-file: "20"

  nginx:
    build: ./nginx
    restart: unless-stopped
    volumes:
      - static_volume:/usr/src/app/assets
    ports:
      - 80:80
    depends_on:
      - web
    logging:
      driver: "json-file"
      options:
        max-size: "100k"
        max-file: "20"

volumes:
  static_volume:
  postgres_data:
    external: true

and this is my nginx.conf file:

upstream my_server {
    server web:8000;
}

server {

    listen 80;

    location / {
        proxy_pass          http://my_server;
        proxy_set_header    Host                $http_host;
        proxy_set_header    Host                $host;
        proxy_set_header    X-Forwarded-For     $proxy_add_x_forwarded_for;
        proxy_set_header    X-Real-IP           $remote_addr;
        proxy_redirect off;
    }

    location /assets/ {
        alias /usr/src/app/assets;
    }

    location  /robots.txt {
        alias  /var/www/html/robots.txt;
    }
}

In my Dockerfile for django, I run makemigrations and migrate and I have confirmed that the database tables are created.

I expect to go to localhost or 127.0.0.1 and see my website served there. Instead, I get this error: Bad Request (400).

When I run docker ps I get:

CONTAINER ID        IMAGE                  COMMAND                  CREATED             STATUS              PORTS                NAMES
2dbc6ff7be78        my_server_nginx        "nginx -g 'daemon of…"   4 minutes ago       Up 4 minutes        0.0.0.0:80->80/tcp   my_server_nginx_1
a6173e017c93        my_server_web          "/usr/src/app/entryp…"   4 minutes ago       Up 4 minutes        8000/tcp             my_server_web_1
918e7bdae298        postgres:11.2-alpine   "docker-entrypoint.s…"   4 minutes ago       Up 4 minutes        5432/tcp             my_server_db_1

When I run docker logs my_server_nginx_1 I get lines like this:

172.18.0.1 - - [08/May/2019:22:25:07 +0000] "GET / HTTP/1.1" 400 37 "-" "Mozilla/5.0 (Macintosh; Intel Mac OS X 10_14_4) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/74.0.3729.131 Safari/537.36" "-"
172.18.0.1 - - [08/May/2019:22:25:07 +0000] "GET /favicon.ico HTTP/1.1" 400 37 "http://localhost/" "Mozilla/5.0 (Macintosh; Intel Mac OS X 10_14_4) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/74.0.3729.131 Safari/537.36" "-"

Solution

  • While trying to implement upinder kumar's solution I stumbled upon the solution. I will add here for anyone else facing this problem in the future:

    The nginx.conf cannot have both

    proxy_set_header    Host                $http_host;
    

    and

    proxy_set_header    Host                $host;
    

    Deleting either one solved the problem.