【问题标题】:502 bad gateway after 30 seconds30 秒后出现 502 错误网关
【发布时间】:2020-04-13 08:26:32
【问题描述】:

我网站上的一个页面需要在服务器上进行长时间的计算(约 2 分钟)。如果我在 localhost 上运行该网站,它可以正常工作。但在生产时约 30 秒。这是我的 nginx conf 的 http 部分:

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 {
    }

}

我尝试添加:

fastcgi_read_timeout 300;
proxy_read_timeout 300;

最后(在“服务器”之后)但它什么也没做。

【问题讨论】:

  • 您问题中的配置不完整。使用nginx -T(大写T)查看整个配置。

标签: nginx gunicorn bad-gateway


【解决方案1】:

如果您收到 502 Bad Gateway 错误,这意味着您的应用程序服务器(我猜它的 Unicorn 根据您的标签)正在发送超时而不是 Nginx,您应该在生产服务器中的 unicorn.rb 文件中增加超时。

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

如果是 Python Green Unicorn,请执行以下操作:

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

【讨论】:

  • 您好,非常感谢您的时间和精力。我还没有机会测试此解决方案,但我会尽快为您提供机会。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2016-06-23
  • 1970-01-01
  • 2020-06-18
  • 2012-08-04
  • 2016-08-19
相关资源
最近更新 更多