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

相关推荐
开开心心就好19 小时前
发票合并打印工具,多页布局设置实时预览
linux·运维·服务器·windows·pdf·harmonyos·1024程序员节
火车叼位19 小时前
脚本伪装:让 Python 与 Node.js 像原生 Shell 命令一样运行
运维·javascript·python
鹏北海19 小时前
micro-app 微前端项目部署指南
前端·nginx·微服务
迎仔20 小时前
06-存储设备运维进阶:算力中心的存储管家
运维
?re?ta?rd?ed?20 小时前
linux中的调度策略
linux·运维·服务器
全栈工程师修炼指南20 小时前
Nginx | stream content 阶段:TCP 协议四层反向代理浅析与实践
运维·网络·网络协议·tcp/ip·nginx
hweiyu0020 小时前
Linux 命令:tr
linux·运维·服务器
Trouvaille ~20 小时前
【Linux】应用层协议设计实战(一):自定义协议与网络计算器
linux·运维·服务器·网络·c++·http·应用层协议
allway220 小时前
基于华为taishan200服务器、arm架构kunpeng920 cpu的虚拟化实战
linux·运维·服务器
CSCN新手听安20 小时前
【linux】高级IO,I/O多路转接之poll,接口和原理讲解,poll版本的TCP服务器
linux·运维·服务器·c++·计算机网络·高级io·poll