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

Become a contributor for community

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

DigitalOcean Documentation

Full documentation for every DigitalOcean product.

Resources for startups and SMBs

The Wave has everything you need to know about building a business, from raising funding to marketing your product.

Get our newsletter

Stay up to date by signing up for DigitalOcean’s Infrastructure as a Newsletter.

New accounts only. By submitting your email you agree to our Privacy Policy

The developer cloud

Scale up as you grow — whether you're running one virtual machine or ten thousand.

Get started for free

Sign up and get $200 in credit for your first 60 days with DigitalOcean.*

*This promotional offer applies to new accounts only.