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 生成接口应该可以正常工作了,即使需要几分钟来生成内容也不会超时!

相关推荐
皮卡蛋炒饭.10 分钟前
Ext系列文件系统
运维
齐齐大魔王15 分钟前
linux-核心工具
linux·运维·服务器
醇氧15 分钟前
Linux 系统的启动过程
linux·运维·服务器
IMPYLH16 分钟前
Linux 的 dircolors 命令
linux·运维·服务器·数据库
齐齐大魔王19 分钟前
linux-基础操作
linux·运维·服务器
一个有温度的技术博主25 分钟前
网安实验系列二:服务器信息收集
运维·服务器
bwz999@88.com1 小时前
ubuntu24.04更换国内源
linux·运维·服务器
腾科IT教育1 小时前
红帽认证考试全国考点信息新发布,便捷参考
运维·服务器·红帽认证·linux考试·rhcsa考试
历程里程碑1 小时前
Protobuf 环境搭建:Windows 与 Linux 系统安装教程
linux·运维·数据结构·windows·线性代数·算法·矩阵
软件资深者1 小时前
OpenClaw 本地安装 vs 网页版龙虾:全方位对比 + 2026 最新一键安装客户端(新手零门槛搭建专属 AI 助理)
运维·人工智能·自动化·飞书·数字员工·openclaw·龙虾