Question

How to increase Server Gateway Timeout more then 1 minute when using dockerize django project?

My droplet configuration: OS: Ubuntu 18.04.3 (LTS) x64 CPU: 4 core RAM : 8GB SSD: 80GB

On droplet, installed gitlab, docker, docker-compose. Deployed Django, Gunicorn, NGINX, Postgresql using Docker and GitLab CI/CD on DO droplet.

Below are configuration files:

nginx.conf

upstream keev {
    server web:8000;
}

server {

    listen 80;

    location / {
        proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
        proxy_set_header Host $host;
        proxy_redirect off;

        <^>proxy_connect_timeout   120;
        proxy_send_timeout      120;
        proxy_read_timeout      120;
        send_timeout            120;
        client_body_timeout     120;<^>

        proxy_pass http://keev;
    }
    location /staticfiles/ {
        alias /home/app/web/staticfiles/;
    }

    location /mediafiles/ {
        alias /home/app/web/mediafiles/;
    }
    proxy_connect_timeout   120;
    proxy_send_timeout      120;
    proxy_read_timeout      120;
    send_timeout            120;
    client_body_timeout     120;
}

docker-compose-prod.yml


version: '3.7'

services:

  # Redis
  redis:
    image: redis:5.0-alpine

  db:
    image: postgres:12-alpine
    env_file:
      - .env

  web:
    image: "${WEB_IMAGE}"
    expose:
      - "8000"
    volumes:
      - ./app:/app
      - static_volume:/home/app/web/staticfiles
      - media_volume:/home/app/web/mediafiles
    command: sh -c "python manage.py wait_for_db &&
                    python manage.py migrate &&
                    python manage.py collectstatic --no-input --clear &&
                    gunicorn keev.wsgi:application --workers 8 --threads 8 -t 120 --bind 0.0.0.0:8000"
    env_file:
      - .env
    depends_on:
      - db
      - redis


  nginx:
    image: "${NGINX_IMAGE}"
    ports:
      - "8001:80"
    volumes:
      - static_volume:/home/app/web/staticfiles
      - media_volume:/home/app/web/mediafiles
    depends_on:
      - web
    links: 
      - web
    restart: on-failure

volumes:
  static_volume:
  media_volume:

Dockerfile

FROM python:3.7-alpine

# set environment variables
ENV PYTHONDONTWRITEBYTECODE 1
ENV PYTHONUNBUFFERED 1
# some packages install needed
...
...
...
COPY ./requirements.txt /requirements.txt
RUN pip install --no-cache-dir -r requirements.txt

# Setup directory structure
RUN mkdir -p /app
ENV APP_HOME=/app

RUN mkdir APP_HOME
RUN mkdir $APP_HOME/staticfiles
RUN mkdir $APP_HOME/mediafiles
WORKDIR $APP_HOME

COPY /app $APP_HOME

Dockerfile is correctly configured.

By using Gitlab CI/CD build container image and pulled on droplet and run it.

The thing is that my some request need more than 1 minute to process in my Django project. And I have already figure it out that how to increase the request timeout to 2 minutes from gunicorn and nginx but it worked on local system.

gunicorn gunicorn keev.wsgi:application --workers 8 --threads 8 -t 120 --bind 0.0.0.0:8000

nginx

    proxy_connect_timeout   120;
    proxy_send_timeout      120;
    proxy_read_timeout      120;
    send_timeout            120;
    client_body_timeout     120;

But when it deployed to droplet it didn’t work, it gives response 504 Gateway timeout after request take longer than 1 minute to process. Don’t know how it goes wrong on server. Read lot of articles and post regarding docker, nginx, django and gateway timeout.

Please need help for this, kind of stuck for this.

Thanks for helping me.

Show comments

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.

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.

Hey KEEV,

I had the same issue and it took me weeks to solve it.

Check this article! It explains well how to solve the issue.

I remember that I always tried to solve the issue by increasing the timeout of nginx. However, increasing the timeout of gunicorn eventually solved the issue. You can easily do it through the command line.

Under gunicorn is timing out the solution to your problem is explained. https://www.datadoghq.com/blog/nginx-502-bad-gateway-errors-gunicorn/

Please let me know if this solved your issue.

Kind regards

Marcel