nginx接口超时,增加接口超时时间

C:\tools\nginx-1.28.0\conf\nginx.conf文件,报错:2026/01/07 16:49:32 [error] 29844#25096: *8 upstream timed out (10060: A connection attempt failed because the connected party did not properly respond after a period of time, or established connection failed because connected host has failed to respond) while reading response header from upstream, client: 127.0.0.1, server: localhost, request: "POST /api-aa2-agent12/classes/ai-contents/generate/52 HTTP/1.1", upstream: "http://192.168.31.141:1234/classes/ai-contents/generate/52", host: "localhost", referrer: "http://localhost:3001/"

bash 复制代码
# 测试域名
    server {
        listen 80;
        server_name localhost;

        # 全局超时设置(针对 AI 生成等长时间请求)
        proxy_connect_timeout 600s;
        proxy_send_timeout 600s;
        proxy_read_timeout 600s;

        location /api-aa2-agent12/ {

            # 处理 OPTIONS 预检请求
            if ($request_method = 'OPTIONS') {
                add_header 'Access-Control-Allow-Origin' '*' always;
                add_header 'Access-Control-Allow-Methods' 'GET, POST, PUT, DELETE, PATCH, OPTIONS' always;
                add_header 'Access-Control-Allow-Headers' 'Authorization, Content-Type, Accept, Origin, User-Agent, DNT, Cache-Control, X-Mx-ReqToken, X-Requested-With' always;
                add_header 'Access-Control-Max-Age' 1728000 always;
                add_header 'Content-Type' 'text/plain; charset=utf-8' always;
                add_header 'Content-Length' 0 always;
                return 204;
            }

            # 设置允许跨域的域,* 表示允许任何域,也可以设置特定的域
            add_header 'Access-Control-Allow-Origin' '*' always;
            # 允许的方法
            add_header 'Access-Control-Allow-Methods' 'GET, POST, PUT, DELETE, PATCH, OPTIONS' always;
            # 允许的头信息字段
            add_header 'Access-Control-Allow-Headers' 'Authorization, Content-Type, Accept, Origin, User-Agent, DNT, Cache-Control, X-Mx-ReqToken, X-Requested-With' always;
            # 缓存时间
            add_header 'Access-Control-Max-Age' 1728000 always;


            proxy_set_header X-Real-IP $remote_addr;
            proxy_set_header REMOTE-HOST $remote_addr;
            proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
            proxy_set_header X-NginX-Proxy true;

            proxy_set_header Connection '';
            proxy_http_version 1.1;
            chunked_transfer_encoding off;
            proxy_buffering off;
            proxy_cache off;

            # 超时配置(针对 AI 生成等长时间请求,设置为 10 分钟)
            proxy_connect_timeout 600s;
            proxy_send_timeout 600s;
            proxy_read_timeout 600s;

            proxy_pass http://192.168.31.141:1234/;
        }
    }      

主要修改

1. Server 级别的超时配置(lines 118-121)

复制代码
# 全局超时设置(针对 AI 生成等长时间请求)
proxy_connect_timeout 600s;
proxy_send_timeout 600s;
proxy_read_timeout 600s;

2. Location 级别的超时配置(lines 157-160)

复制代码
# 超时配置(针对 AI 生成等长时间请求,设置为 10 分钟)
proxy_connect_timeout 600s;
proxy_send_timeout 600s;
proxy_read_timeout 600s;

配置说明

  • proxy_connect_timeout: 与后端服务器建立连接的超时时间(600 秒 = 10 分钟)
  • proxy_send_timeout: 向后端服务器发送请求的超时时间(600 秒 = 10 分钟)
  • proxy_read_timeout: 从后端服务器读取响应的超时时间(600 秒 = 10 分钟)

这些配置确保 AI 内容生成等长时间请求不会超时。

重新加载 Nginx

请执行以下命令重新加载配置:

复制代码
cd C:\tools\nginx-1.28.0
nginx.exe -t
nginx.exe -s reload

现在 AI 生成接口应该可以正常工作了,即使需要几分钟来生成内容也不会超时!

相关推荐
闲云一鹤2 天前
nginx 快速入门教程 - 写给前端的你
前端·nginx·前端工程化
碳基沙盒2 天前
OpenClaw 多 Agent 配置实战指南
运维
蝎子莱莱爱打怪5 天前
Centos7中一键安装K8s集群以及Rancher安装记录
运维·后端·kubernetes
何中应5 天前
Nginx转发请求错误
前端·后端·nginx
DianSan_ERP6 天前
电商API接口全链路监控:构建坚不可摧的线上运维防线
大数据·运维·网络·人工智能·git·servlet
呉師傅6 天前
火狐浏览器报错配置文件缺失如何解决#操作技巧#
运维·网络·windows·电脑
不是二师兄的八戒6 天前
Linux服务器挂载OSS存储的完整实践指南
linux·运维·服务器
芝士雪豹只抽瑞克五6 天前
Nginx 高性能Web服务器笔记
服务器·nginx
zhangfeng11336 天前
趋动云 如何ssh登录 服务区 项目server
运维·人工智能·ssh