Search code examples
djangodockernginxgunicornproduction

When I set DEBUG=False Django gives me 400 bad request am using [docker, nginx, django, gunicorn]


I am trying to define a production env for Django using docker and Nginx and Gunicorn and It works fine when debug=True If I make debug=False the Issue start here gives me Bad Request (400) my Nginx file like this:

upstream API {
     server backend-stack:8000;
}
server {

  listen 80;
  server_name localhost;


  location / {
    proxy_set_header Host $host;
    proxy_pass http://api;
    proxy_set_header X-Forwarded-Host $server_name;
    proxy_set_header X-Real-IP $remote_addr;
  }
  location /static/ {
    alias /backend/static/;
  }
  location /media/ {
    alias /backend/media/;
  }
}

my allowed_hosts In settings.py

if env.get("ALLOWED_HOSTS"):
    ALLOWED_HOSTS = env.get("ALLOWED_HOSTS")

my gunicorn execute command from entrypoint.sh file:

gunicorn core.wsgi:application --bind 0.0.0.0:8000

also here Is my nginx container In docker-compose:

nginx:
container_name: nginx-stack
build: ./nginx
restart: always
volumes:
  - ./nginx/config:/etc/nginx/conf.d
  - ./nginx/log:/var/log/nginx
  - static_volume:/app_backend/static
  - media_volume:/app_backend/media
ports:
  - 80:80
  - 443:443
depends_on:
  - backend
networks:
  - app-network

my allowed_hosts value:

ALLOWED_HOSTS = ['localhost', '127.0.0.1', '0.0.0.0', '*']

finaly access log file for nginx :

192.168.144.1 - - [12/Jun/2021:15:18:02 +0000] "GET / HTTP/1.1" 400 154 "-" "Mozilla/5.0 (Macintosh; Intel Mac OS X 10_15_7) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/91.0.4472.101 Safari/537.36" "-"

Solution

  • ok, I did It the problem was happening because I don't create any view yet or any page with a URL after I create a new page and set it as the home page It's working now.