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.

Bobby Iliev
Site Moderator
Site Moderator badge
July 26, 2020
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

Try DigitalOcean for free

Click below to sign up and get $200 of credit to try our products over 60 days!

Sign up

Get our biweekly newsletter

Sign up for Infrastructure as a Newsletter.

Hollie's Hub for Good

Working on improving health and education, reducing inequality, and spurring economic growth? We'd like to help.

Become a contributor

Get paid to write technical tutorials and select a tech-focused charity to receive a matching donation.

Welcome to the developer cloud

DigitalOcean makes it simple to launch in the cloud and scale up as you grow — whether you're running one virtual machine or ten thousand.

Learn more
DigitalOcean Cloud Control Panel