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

相关推荐
释怀不想释怀4 分钟前
Docker(安装软件)
运维·docker·容器
网硕互联的小客服8 分钟前
服务器 CPU 温度过高需要进行的物理处理和软件处理有哪些?
运维·服务器
济61719 分钟前
linux(第十三期)--filezilla使用方法(实现ubuntu和windows11文件互传)-- Ubuntu20.04
linux·运维·ubuntu
HIT_Weston20 分钟前
91、【Ubuntu】【Hugo】搭建私人博客:侧边导航栏(五)
linux·运维·ubuntu
阿巴~阿巴~22 分钟前
从不可靠到100%可靠:TCP与网络设计的工程智慧全景解析
运维·服务器·网络·网络协议·tcp/ip·智能路由器
oMcLin23 分钟前
如何在 Rocky Linux 8.6 上配置并调优 Nginx 与 Lua 脚本,提升 API 网关的性能与并发处理能力
linux·nginx·lua
一殊酒32 分钟前
【Figma】Figma自动化
运维·自动化·figma
食咗未39 分钟前
Linux iptables工具的使用
linux·运维·服务器·驱动开发·网络协议·信息与通信
.hopeful.1 小时前
Docker——镜像仓库和镜像
运维·docker·容器
运维小欣1 小时前
博睿数据领航可观测性选型:国际竞品对比与2026企业决策指南
运维