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

相关推荐
蓝田~16 小时前
Nginx集群与相关服务配置说明
运维·nginx
qq_4052686616 小时前
Kali使用nginx部署前后端项目
运维·nginx
程序 代码狂人16 小时前
CentOS7中有趣的yum源(彩蛋)-----第二期
linux·运维·服务器
Y‍waiX‍‍‮‪‎⁠‌‫‎‌‫‬16 小时前
CentOS7(Linux)从系统安装到环境搭建
linux·运维·服务器
是火云哦17 小时前
打包你的开发环境:Docker 从入门到上瘾
运维·docker·容器
我送炭你添花17 小时前
树莓派 3B+ 部署 TR-069 ACS(自动配置服务器)GenieACS 实录
运维·服务器·网络协议
扫地生大鹏17 小时前
阿里云大模型服务器安装SNMP包启发
运维·服务器
今晚打佬虎17 小时前
精准阻断 Docker 容器映射端口:流量路径诊断与 iptables 配置
运维·docker·容器
honsor17 小时前
机房/档案室专用以太网温湿度传感器:智能监控赋能环境安全
运维·网络·物联网·安全
晚风吹长发18 小时前
初步了解Linux中的信号捕捉
linux·运维·服务器·c++·算法·进程·x信号