Deploy ASGI Daphne/WSGI Gunicorn Nginx

Problem: Error 400 when accessing application with ASGI (Daphne) and WSGI (Gunicorn) via Nginx

Guys, I’m facing problems when trying to deploy an application that uses ASGI with Daphne on port 9001 and WSGI with Gunicorn on port 8000. To forward requests, I configured Nginx to listen on port 8080. However, when trying to access the application through that port, I get error 400.

I would like to know what could be causing this error and how I can fix it. My current configuration is as follows:

ASGI (Daphne): Running on port 9001.
WSGI (Gunicorn): Running on port 8000.
Nginx: Configured to listen on port 8080 and forward requests to the Daphne and Gunicorn servers as needed.
I would appreciate any help or suggestions to resolve this issue.

my docker Compose

services:

  django:
    build: 
      context: ./mapa
      dockerfile: Dockerfile.prod
    
    command: gunicorn mapa.wsgi:application --bind 0.0.0.0:8000
    volumes:
        - ./mapa:/usr/src/app
        - static_volume:/home/app/django/static
    ports:
      - 8000:8000
    environment:
      - DEBUG=1
      - DJANGO_ALLOWED_HOSTS=localhost,127.0.0.1
      - CELERY_BROKER=redis://redis:6379/0
      - CELERY_BACKEND=redis://redis:6379/0
    depends_on:
      - redis

      
  daphne:
    build: 
      context: ./mapa
      dockerfile: Dockerfile.prod
    container_name: daphne
    command: daphne -b 0.0.0.0 -p 9001 mapa.asgi:application
    volumes:
      - .:/usr/src/app/
    ports:
      - "9001:9001"
    depends_on:
      - django
      - redis
  
  celery:
    build: 
      context: ./mapa
      dockerfile: Dockerfile.prod
    volumes:
      - .:/usr/src/app
    depends_on:
      - django
      - redis
      
  celerybeat:
    build: 
      context: ./mapa
      dockerfile: Dockerfile.prod
    command: celery -A mapa beat -l INFO
    volumes:
      - .:/usr/src/app
    depends_on:
      - django
      - redis
  
  redis:
    image: "redis:alpine"


  nginx:
    build: ./nginx
    volumes:
      - static_volume:/home/app/django/static

    ports:
      - 8080:8080
    depends_on:
      - django



volumes:
  static_volume:

my nginx default.conf


upstream mapa {
    server django:8000;
}

upstream daphne {
    server daphne:9001;
}

server {
    listen 8080;
 
    location / {
        proxy_pass http://mapa;
        proxy_set_header Host $host;
        proxy_set_header X-Real-IP $remote_addr;
        proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
        # proxy_set_header X-Forwarded-Proto $scheme;
        proxy_set_header Host $host;
        proxy_redirect off;
    }

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


    location /ws/ {
        proxy_pass http://daphne/ws/;
        proxy_set_header Upgrade $http_upgrade;
        proxy_set_header Connection "upgrade";
        proxy_redirect off;
        proxy_set_header Host $host;
        proxy_set_header X-Real-IP $remote_addr;
        proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
}
}

meu roting.py

from django.urls import re_path
from app_geoip import consumer

ws_patterns = [
    re_path(r'ws/tables/', consumer.MapaConsumer.as_asgi()),
    #re_path(r'ws/tab1/', consumer.Tab1Consumer.as_asgi()),
    #re_path(r'ws/tables/(?P<nome_grupo>\w+)/$)', consumer.MapaConsumer.as_asgi())
]

The DEBUG in setting give me this text

DisallowedHost at /

Invalid HTTP_HOST header: ‘0.0.0.0,0.0.0.0’. The domain name provided is not valid according to RFC 1034/1035.

Request Method: GET
Request URL: http://0.0.0.0,0.0.0.0/
Django Version: 4.2.13
Exception Type: DisallowedHost
Exception Value: Invalid HTTP_HOST header: ‘0.0.0.0,0.0.0.0’. The domain name provided is not valid according to RFC 1034/1035.
Exception Location: /usr/local/lib/python3.11/site-packages/django/http/request.py, line 150, in get_host
Raised during: app_geoip.views.home2
Python Executable: /usr/local/bin/python
Python Version: 3.11.4
Python Path: [‘/home/app/django’, ‘/home/app/django’, ‘/usr/local/bin’, ‘/usr/local/lib/python311.zip’, ‘/usr/local/lib/python3.11’, ‘/usr/local/lib/python3.11/lib-dynload’, ‘/usr/local/lib/python3.11/site-packages’]
Server time: Mon, 10 Jun 2024 15:57:06 +0000

Hi, I’m not a specialist of nginx, but can’t the problem (mentionning 2 IPs in HTTP_HOST header) be a consequence of the following being duplicated in your nginx configuration:

1 Like