I am following a tutorial on how to dockerize django with postgres, gunicorn and nginx on an ubuntu 20.04 system.
(see as reference: https://testdriven.io/blog/dockerizing-django-with-postgres-gunicorn-and-nginx/ )

Unfortunately while setting up the nginx part of the app I’ve seem to have stumbled accross a problem with setting up the reverse proxy correctly.

When I try to access the django app via <ip-address>:1337 I get ‘The requested resource was not found on this server.’ error.

While I know that this is due to django not being properly served to nginx, but I’m quite new at web deployment and currently at a loss on how to proceed.

The relevant settings:

docker-compose.prod.yml

services:
  web:
    build:
      context: ./app
      dockerfile: Dockerfile.prod
    command: gunicorn hello_django.wsgi:application --bind 0.0.0.0:8000
    expose:
      - 8000
    env_file:
      - ./.env.prod
    depends_on:
      - db
  db:
    image: postgres:12.0-alpine
    volumes:
      - postgres_data:/var/lib/postgresql/data/
    env_file:
      - ./.env.prod.db
  nginx:
    build: ./nginx
    ports:
      - 1337:80
    depends_on:
      - web

volumes:
  postgres_data:

nginx.conf

upstream hello_django {
    server web:8000;
}

server {

    listen 80;

    location / {
        proxy_pass http://hello_django;
        proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
        proxy_set_header Host $host;
        proxy_redirect off;
    }

}

nginx Dockerfile

FROM nginx:1.19.0-alpine

RUN rm /etc/nginx/conf.d/default.conf
COPY nginx.conf /etc/nginx/conf.d

settings.py

ALLOWED_HOSTS = ['<ip-address>', 'localhost']

Any help would be really appreciated.

These answers are provided by our Community. If you find them useful, show some love by clicking the heart. If you run into issues leave a comment, or add your own answer to help others.

×
1 answer

Hi there @mikewinkel,

What I could suggest is checking the error log of both your Nginx and Web containers.

To do that you can run the following command:

  • docker ps -a

This would return a list of your containers, and then get the container ID and check the logs:

  • docker logs container_id_here

This will give you more information on why the services are not returning any responses.

Feel free to share the errors here if you wish.

Regards,
Bobby

Submit an Answer