本文介绍: 访问出现504 Gateway Time-out

问题发现

客户系统上传文件的时候,如果上传文件过大,因为系统需要读取excel文件内容,进行处理,所以耗时比较长,导致等待超时

分析

nginx访问出现504 Gateway Time-out,一般是由于程序执行时间过长导致响应超时,例如程序需要执行90秒,而nginx最大响应等待时间为30秒,这样就会出现超时

通常有以下几种情况导致

  1. 程序处理大量数据,导致等待超时
  2. 程序调用外部请求,而外部请求响应超时
  3. 连接数据库失败没有停止,死循环重新连。

出现这种情况,我们可以优化程序,缩短执行时间。假如是文件解析这种本身就耗时比较长的任务,则可以调大nginx超时限制参数,使程序可以正常执行

修改nginx配置

http {
    ...
    fastcgi_connect_timeout 3000;
    fastcgi_send_timeout 3000;
    fastcgi_read_timeout 3000;
	···
}
fastcgi_connect_timeout
fastcgi连接超时时间,默认60秒

fastcgi_send_timeout
nginx 进程fastcgi 进程发送请求过程超时时间,默认值60秒

fastcgi_read_timeout
fastcgi 进程向 nginx 进程发送输出过程超时时间,默认值60秒
server {
    listen 8888;
    location / {
        proxy_pass http://pdfs;
        proxy_connect_timeout   18000;
        proxy_send_timeout      18000;
        proxy_read_timeout      18000;
        proxy_set_header Host 172.10.10.35:8081;
        proxy_set_header X-Forwarded-Scheme  $scheme;
        proxy_set_header X-Forwarded-For $remote_addr;
        proxy_pass   http://172.10.10.35:8083/demo/;
    }
}
  1. proxy_connect_timeout 1800s;#nginx跟后端服务器连接超时时间(代理连接超时)
  2. proxy_send_timeout 1800s;#后端服务器数据回传时间(代理发送超时)
  3. proxy_read_timeout 1800s;#连接成功后,后端服务器响应时间(代理接收超时)

发表回复

您的邮箱地址不会被公开。 必填项已用 * 标注