Question

Dockerized Nginx not forwarding to Dockerized Gunicorn

Posted July 24, 2020 174 views
NginxDocker

I am successfully running my Django web application using Gunicorn inside a docker container. This is running on a Ubuntu 16.04.6 system. What I am trying to do is have Nginx (running inside a Docker container) act as my web server and forward the request to the gunicorn docker container. Note: At the command line I can execute my nginx image and have it successfully ping the gunicorn one by using: sudo docker container exec -ti 11e65460b93c ping django_web where 11e65460b93c is the container id of the nginx container and django_web is the name of the gunicorn container.

When I browse to http://my_ip:1337/ I get the message:
Welcome to nginx!
If you see this page, the nginx web server is successfully installed and working. Further configuration is required.

I don’t know what that further configuration is. Any help would be greatly appreciated.

Here are my files:
Dockerfile

FROM python:3.6
ENV PYTHONUNBUFFERED 1
ENV PYTHONDONTWRITEBYTECODE 1
ARG HTTP_PROXY
ARG HTTPS_PROXY

COPY ./requirements.txt /requirements.txt
RUN pip install -r /requirements.txt

RUN mkdir /code
WORKDIR /code

ADD . /code/
RUN useradd user
USER user

CMD ["gunicorn", "Django.wsgi:application", "--bind", "0.0.0.0:8000"]

docker-compose.yml

version: "3"

services:
  web:
      build: 
          context: .
          args:
            HTTP_PROXY: http://amec.zscaler.company.com:9480
            HTTPS_PROXY: http://amec.zscaler.company.com:9480
      image: django_web  
      container_name: django_web
      expose:
        - "8000"
  nginx:
      build: ./nginx
      ports:
        - "1337:80"
      depends_on:
        - web

nginx/Dockerfile

# this file resides in a folder called nginx which is a child of DJango (project root folder)
FROM nginx:alpine
COPY nginx.conf /etc/nginx/conf.d

nginx/nginx.conf

# this file resides in a folder called nginx which is a child of DJango (project root folder)
upstream django_web {
    server web:8000;
    server localhost:8000; 
    server 127.0.0.1:8000;
}

server {
    listen 80;

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

Thanks,
Phil

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 @philox,

To me it looks like that the Nginx configuration file is loading correctly due to the default.conf Nginx file. What I could suggest is updating your Docker file to:

nginx/Dockerfile
FROM nginx:alpine

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

Let me know how it goes.
Regards,
Bobby

Submit an Answer