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.
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.
×
Found the main source of this issue of timeout.
DigitalOcean Load balancer connections have a keep-alive time of 60 seconds. As shown in following docs.
https://www.digitalocean.com/docs/networking/load-balancers/#limits
Can we change the keep-alive time to 300 seconds?
Or give any alternative for load balancer.