tiger-sms ChatGPT 账号/ChatGPT 代注册 OpenAI API 代充值

Nginx 504 Gateway Timeout 错误的解决方法

dajiaka OpenAI API key

今天老王在调一个 API 服务时,响应时间超过 60s 就报错 Nginx 504 Gateway Timeout,本文分享下错误原因和解决方法。

一、Nginx 504 Gateway Timeout 问题原因

Nginx 504 Gateway Timeout 错误通常表示后端服务器在规定时间内没有响应前端服务器(如 Nginx)的请求。

老王自己的情况是:Nginx 作为前端服务器,反向代理到 Docker 里的一个服务,Nginx 默认的最长等待时间是 60s,超时就报错 504,因此才会出现调用 API 时,一超过 60s 就直接报错返回。

二、Nginx 504 Gateway Timeout 解决方法

我们可以通过修改 Nginx 配置文件来增加超时时间。

在 Nginx 配置文件(通常是 /etc/nginx/nginx.conf 或者 /etc/nginx/sites-available/default,如果是宝塔安装的 Nginx,则位置在 /www/server/nginx/conf/proxy.conf)中,添加或修改以下参数:

http {
    ...
    proxy_connect_timeout       600;
    proxy_send_timeout          600;
    proxy_read_timeout          600;
    send_timeout                600;
    ...
}

这些参数的含义分别是:

  • proxy_connect_timeout:Nginx 与后端服务器建立连接的超时时间。
  • proxy_send_timeout:Nginx 向后端服务器发送请求的超时时间。
  • proxy_read_timeout:Nginx 等待后端服务器响应的超时时间。
  • send_timeout:Nginx 向客户端发送响应的超时时间。

修改好后,重启 Nginx,504 问题解决。

赞(0)
关注我们
未经允许不得转载:老王博客 » Nginx 504 Gateway Timeout 错误的解决方法