502 bad gateway after 30 seconds
Asked Answered
T

2

5

One of the pages on my website requires a long computation on the server(~2 minutes). If I run the website on localhost it works fine. But in production when ~30 seconds. Here's my http section of the nginx conf:

http {
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;
tcp_nodelay         on;
keepalive_timeout   120;
types_hash_max_size 2048;

include             /etc/nginx/mime.types;
default_type        application/octet-stream;

# Load modular configuration files from the /etc/nginx/conf.d directory.
# See http://nginx.org/en/docs/ngx_core_module.html#include
# for more information.
include /etc/nginx/conf.d/*.conf;
server {
    listen       80 default_server;
    listen       [::]:80 default_server;
    server_name  _;
    root         /usr/share/nginx/html;

    # Load configuration files for the default server block.
    include /etc/nginx/default.d/*.conf;

    location / {
            proxy_read_timeout 300;
            proxy_connect_timeout 300;
    }

    error_page 404 /404.html;
        location = /40x.html {
    }

    error_page 500 502 503 504 /50x.html;
        location = /50x.html {
    }

}

I tried adding:

fastcgi_read_timeout 300;
proxy_read_timeout 300;

at the end(after "server") but it didn't do anything.

Tolu answered 13/4, 2020 at 8:26 Comment(1)
The configuration in your question is incomplete. Use nginx -T (uppercase T) to view the entire configuration.Ideally
P
5

If you get 502 Bad Gateway error it means your Application server(i guess its Unicorn according to your tags) is sending the timeout and not Nginx, you should increase the timeout in your unicorn.rb file in your production server.

worker_processes 2
listen "/tmp/xxx.socket"
##equal to your proxy read timeout in the Nginx config.
timeout 300 
pid "/tmp/unicorn.xxx.pid"

In case of Python Green Unicorn please do the following:

NUM_WORKERS=3
TIMEOUT=300

exec gunicorn ${DJANGO_WSGI_MODULE}:application \
--name $NAME \
--workers $NUM_WORKERS \
--timeout $TIMEOUT \
--log-level=debug \
--bind=x.x.x.x \
--pid=$PIDFILE
Paediatrics answered 13/4, 2020 at 9:54 Comment(1)
Hi, thank you so much for your time and effort. I haven't had the chance to test this solution yet but I will update you as soon as I get the opportunity.Tolu
N
2

Fixed this by increasing timeout for Gunicorn. Used the --timeout option when starting Gunicorn.

gunicorn -b 0.0.0.0:8000 --timeout 60 your_app:app

You can increase this in cmd or in service. Here it is 60 seconds

Got the idea from @matanco answer

Narvik answered 11/10, 2023 at 16:44 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.