Question

Dockerized Nginx not forwarding to Dockerized Gunicorn

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


Submit an answer

This textbox defaults to using Markdown to format your answer.

You can type !ref in this text area to quickly search our full set of tutorials, documentation & marketplace offerings and insert the link!

Sign In or Sign Up to Answer

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.

Accepted 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

Want to learn more? Join the DigitalOcean Community!

Join our DigitalOcean community of over a million developers for free! Get help and share knowledge in Q&A, subscribe to topics of interest, and get courses and tools that will help you grow as a developer and scale your project or business.