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.

1 comment

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

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

  • @Marcelkresse Thanks for your help.
    I tried this solution in given link, but problem didn’t solve.
    https://www.datadoghq.com/blog/nginx-502-bad-gateway-errors-gunicorn/

    To check is this configuration work or not, so changed timeout to 30 seconds than got the 504 Gateway Time-out in 31.28s

    /etc/nginx/nginx.conf

    worker_processes  auto;
    
    error_log  /var/log/nginx/error.log warn;
    pid        /var/run/nginx.pid;
    
    
    events {
        worker_connections  1024;
    }
    
    
    http {
        include       /etc/nginx/mime.types;
        default_type  application/octet-stream;
    
        log_format  main  '$remote_addr - $remote_user [$time_local] "$request" '
                          '$status $body_bytes_sent "$http_referer" '
                          '"$http_user_agent" "$http_x_forwarded_for"';
    
        access_log  /var/log/nginx/access.log  main;
    
        sendfile        on;
        #tcp_nopush     on;
    
        keepalive_timeout  65;
    
        #gzip  on;
    
        include /etc/nginx/conf.d/*.conf;
    }
    
    
    

    /etc/nginx/conf.d/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_pass http://keev;
        }
        location /staticfiles/ {
            alias /home/app/web/staticfiles/;
        }
    
        location /mediafiles/ {
            alias /home/app/web/mediafiles/;
        }
    }
    proxy_connect_timeout   30;
    proxy_send_timeout      30;
    proxy_read_timeout      30;
    send_timeout            30;
    
    fastcgi_connect_timeout 30;
    fastcgi_send_timeout 30;
    fastcgi_read_timeout 30;
    

    Response

    504 Gateway Time-out
    nginx/1.17.4
    
    

    Then thought it works but if Timeout increase to 5 minutes then raise 504 Gateway Time-out after 1 minute but the difference is that it’s response is not from nginx/1.17.4

    nginx.conf

    
    proxy_connect_timeout   300;
    proxy_send_timeout      300;
    proxy_read_timeout      300;
    send_timeout            300;
    
    fastcgi_connect_timeout 300;
    fastcgi_send_timeout 300;
    fastcgi_read_timeout 300;
    

    Response

    504 Gateway Time-out
    
    <html>
    
    <body>
        <h1>504 Gateway Time-out</h1>
        The server didn't respond in time.
    </body>
    
    </html>
    

    My observation says that this response 504 Gateway Time-out
    The server didn’t respond in time. ** got from something else like **global nginx
    , not from nginx docker image.

    But I didn’t install nginx in DO droplet.

    Does server’s timeout effected by the GitLab installation?

  • I have tried to run without using load-balencer so server run as http server and nginx configurations works to increase server timeout to 5min. So, think like how to increase or customized load-balancer’s timeout to run server over https by using forwarding rules.

Submit an Answer