django with nginx reverse proxy in docker compose

I am having problems configuring docker compose, being more specific in the nginx configuration since when I want to consume the API it returns the following

running

docker logs webserver-callservices

[01/May/2023:19:41:53 +0000]
“\x16\x03\x01\x02\x00\x01\x00\x01\xFC\x03\x03d\x1F\xC3\x01\x8D\xEF\xADz-\xD2Me\xA1\x01\x09Rbd\x93\xED\xCA\xEB\xD6”
400 157 “-” “-”

But running

docker logs callservicebackend-backendcallservices-1

I get

Watching for file changes with StatReloader Performing system
checks…

System check identified no issues (0 silenced). May 01, 2023 -
17:01:54 Django version 3.0, using settings ‘callservices.settings’
Starting development server at http://0.0.0.0:8000/ Quit the server
with CONTROL-C.

From what I understand there is a problem in the nginx configuration

this is the docker-compose.yml

version: '3.8'

services:
  webservercallservices:
    image: nginx:latest
    container_name: webserver-callservices
    restart: always
    depends_on:
      - backendcallservices
    ports:
      - "443:443"
    volumes:
      - ./:/var/www
      - ./Dockerfiles/nginx:/etc/nginx/conf.d
      - /etc/letsencrypt:/etc/letsencrypt 
    networks:
      app-network:

  backendcallservices:
    build:
      context: ./Dockerfiles/python
      dockerfile: Dockerfile
    command: sh -c 'python manage.py makemigrations && python manage.py migrate && python manage.py runserver 0.0.0.0:8000'
    volumes:
      - ./:/code
    ports:
      - "8000:8000"
    environment:
      - CELERY_BROKER_URL=redis://redis:6379/0
      - CELERY_RESULT_BACKEND=redis://redis:6379/0
    depends_on:
      - databasecallservices
      - redis
    networks:
      app-network:


  celery:
    build:
      context: ./Dockerfiles/python
      dockerfile: Dockerfile
    command: celery -A callservices worker -l info
    volumes:
      - ./:/code
    depends_on:
      - databasecallservices
      - redis
    environment:
      - CELERY_BROKER_URL=redis://redis:6379/0
      - CELERY_RESULT_BACKEND=redis://redis:6379/0
    networks:
      app-network:

  redis:
    image: redis:latest
    container_name: redis
    restart: always
    ports:
      - "6379:6379"
    networks:
      app-network:

  databasecallservices:
    image: postgres
    volumes:
      - ./data/db:/var/lib/postgresql/data
    environment:
      - POSTGRES_DB=postgres
      - POSTGRES_USER=postgres
      - POSTGRES_PASSWORD=postgres
    ports:
      - "5432:5432"
    networks:
      app-network:

networks:
  app-network:

This is the config of nginx

server {
    listen 443 ssl;
    index index.html;
    error_log  /var/log/nginx/error.log;
    access_log /var/log/nginx/access.log;
    root /var/www/public;

    ssl_certificate /etc/letsencrypt/live/xxxx.com/fullchain.pem;
    ssl_certificate_key /etc/letsencrypt/live/xxxx.com/privkey.pem;

    location / {
        proxy_pass http://backendcallservices:8000;
        proxy_set_header Host $host;
        proxy_set_header X-Real-IP $remote_addr;
        proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
    }
}

Is it possible that the problem is because django doesn’t have https?

In this situation, nginx is your ssl endpoint - it should be talking straight http to your backend. Django doesn’t need to handle any part of the ssl.

Do you have any other location sections in your nginx config file?

What (if anything) does your nginx error.log file show? How about the access.log? Does it look like it’s the right url being submitted?

Note, you should not be using runserver for backendcallservices. You should be using either uwsgi, gunicorn, or some other “production-ready” wsgi service.

(Side note: Your compose file and your nginx conf file are both very similar to what we use for the same purposes.)

1 Like