目录
2. proxy_send_timeout
3. proxy_read_timeout
4. Optional: proxy_next_upstream_timeout (if load balancing)
Full Example (Common Setup)
Pro Tips:
首页 运维 nginx 配置NGINX超时

配置NGINX超时

Aug 03, 2025 pm 04:25 PM

proxy_connect_timeout设为5–10秒,确保快速失败;2. proxy_send_timeout设为10–30秒,适应慢速上传;3. proxy_read_timeout匹配应用最长响应时间,避免504错误;4. 若负载均衡,可设proxy_next_upstream_timeout限制重试时长——正确配置这些值能显着减少网关超时、提升用户体验,并需结合实际日志和监控持续调优。

Configuring Nginx Timeouts

When setting up Nginx as a reverse proxy (eg, for Node.js, Python apps, or upstream services), timeout configuration is critical —especially under slow network conditions, long-polling, or when your backend is temporarily unresponsive.

Configuring Nginx Timeouts

If timeouts aren't configured properly, users may see:

  • 504 Gateway Timeout errors
  • Stuck requests
  • Poor user experience during high load or maintenance

Here's how to properly configure Nginx timeouts:

Configuring Nginx Timeouts

1. proxy_connect_timeout

How long Nginx waits to connect to the upstream server .
Default: 60s
Recommended: 5–10s for most apps

 proxy_connect_timeout 10s;

Use shorter values if your backend is on the same network—fail fast if the service is truly down.

Configuring Nginx Timeouts

2. proxy_send_timeout

How long Nginx waits between sending data chunks to the backend .
Default: 60s
Recommended: 10–30s

 proxy_send_timeout 30s;

This isn't total request time—it's the max time between writes. If your app sends data slowly (eg, large file uploads), increase this.


3. proxy_read_timeout

How long Nginx waits for the backend to send a response .
Default: 60s
Recommended: Match your app's longest expected response time

 proxy_read_timeout 60s;

For long-polling APIs or slow reports, set this higher (eg, 300s).
⚠️ If your app takes longer than this, Nginx will kill the connection → 504 error.


4. Optional: proxy_next_upstream_timeout (if load balancing)

Limits how long Nginx tries to find a working upstream server.

 proxy_next_upstream_timeout 5s;

Full Example (Common Setup)

 location / {
    proxy_pass http://localhost:3000;
    proxy_http_version 1.1;
    proxy_set_header Host $host;
    proxy_set_header X-Real-IP $remote_addr;

    proxy_connect_timeout 10s;
    proxy_send_timeout 30s;
    proxy_read_timeout 60s;
}

Pro Tips:

  • ? Test with curl -v or browser dev tools to see if 504s happen after a fixed time (eg, 60s = likely proxy_read_timeout )
  • ? If your backend restarts or deploys often, shorter timeouts (like 10s) help Nginx fail faster and retry
  • ? Monitor 504 errors in Nginx logs ( /var/log/nginx/error.log ) to fine-tune

Timeouts aren't one-size-fits-all—they depend on your app behavior. Start conservative, then adjust based on real usage.

Basically, set them, test them, and don't forget them.

以上是配置NGINX超时的详细内容。更多信息请关注PHP中文网其他相关文章!

本站声明
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系admin@php.cn

热AI工具

Undress AI Tool

Undress AI Tool

免费脱衣服图片

Undresser.AI Undress

Undresser.AI Undress

人工智能驱动的应用程序,用于创建逼真的裸体照片

AI Clothes Remover

AI Clothes Remover

用于从照片中去除衣服的在线人工智能工具。

Clothoff.io

Clothoff.io

AI脱衣机

Video Face Swap

Video Face Swap

使用我们完全免费的人工智能换脸工具轻松在任何视频中换脸!

热工具

记事本++7.3.1

记事本++7.3.1

好用且免费的代码编辑器

SublimeText3汉化版

SublimeText3汉化版

中文版,非常好用

禅工作室 13.0.1

禅工作室 13.0.1

功能强大的PHP集成开发环境

Dreamweaver CS6

Dreamweaver CS6

视觉化网页开发工具

SublimeText3 Mac版

SublimeText3 Mac版

神级代码编辑软件(SublimeText3)

热门话题

PHP教程
1535
276
如何保护NGINX服务器? 如何保护NGINX服务器? Jul 25, 2025 am 01:00 AM

保护Nginx服务器安全的关键措施包括:1.配置HTTPS加密连接,使用Let'sEncrypt免费证书并通过Certbot自动配置,设置强制跳转和合适加密套件,并启用自动续期;2.限制访问权限,通过IP控制和BasicAuth认证保护敏感路径;3.关闭信息泄露,隐藏版本号、禁止目录浏览并自定义错误页面以减少攻击面。

如何阻止特定的用户代理? 如何阻止特定的用户代理? Jul 26, 2025 am 08:20 AM

要屏蔽特定的User-Agent,可在Nginx、Apache或代码(如PHP、Python)中实现。1.在Nginx中,通过if判断$http_user_agent并返回403;2.在Apache中,使用SetEnvIfNoCase和Deny拒绝访问;3.在程序中判断User-Agent并拦截请求。常见需屏蔽的UA包括python-requests、curl、空UA等,选择合适方式可有效减少垃圾流量和安全风险。

如何将NGINX用作简单的HTTP负载平衡器? 如何将NGINX用作简单的HTTP负载平衡器? Jul 21, 2025 am 01:48 AM

如何使用Nginx实现HTTP负载均衡?答案如下:1.使用upstream模块定义后端服务器组并在server或location中通过proxy_pass转发请求;2.支持轮询、加权轮询、最少连接和IP哈希策略;3.可配置down、backup、fail_timeout及max_fails参数增强稳定性;4.修改配置后执行nginx-t检查语法并用nginx-sreload重载生效。例如基本配置结构包含三个后端节点默认采用轮询分发流量,而加权轮询允许按权重分配请求,least_conn将请求发送

如何在server_name中使用通配符或正则表达式? 如何在server_name中使用通配符或正则表达式? Jul 23, 2025 am 01:43 AM

在Nginx中使用server_name匹配多个域名或子域名时,可通过通配符和正则表达式实现。1.使用通配符时,星号仅可用于开头或结尾,并需为完整标签边界,如.example.com可匹配一级子域但不包括根域或多级子域,若需同时匹配根域和一级子域,应写为example.com*.example.com;2.使用正则表达式时需以~开头,如~^\w .(dev|test)$可匹配以.dev或.test结尾的域名,且支持捕获组调用;3.匹配优先级为确切名称>最长通配符前缀>最长通配符后缀&

NGINX工作过程和连接 NGINX工作过程和连接 Jul 27, 2025 am 03:15 AM

设置worker_processes为auto(即CPU核心数)以充分利用多核性能;2.根据系统文件描述符限制和预期流量设置worker_connections(如1024或更高),确保ulimit-n足够大;3.最大并发连接数=worker_processes×worker_connections,合理配置可支持数千至数万连接,避免瓶颈,提升Nginx生产环境性能。

nginx流代理基础知识 nginx流代理基础知识 Jul 26, 2025 am 02:57 AM

NginxStream模块用于四层代理,1.TCP透传代理MySQL需配置listen和proxy_pass指向upstream;2.TLS直通不终止SSL,保持端到端加密;3.UDP代理加udp关键字适用于DNS等场景;4.常用选项包括proxy_timeout、so_keepalive和access_log;5.注意Stream必须位于顶层配置且与HTTP模块端口分离。

配置NGINX超时 配置NGINX超时 Aug 03, 2025 pm 04:25 PM

proxy_connect_timeout设为5–10秒,确保快速失败;2.proxy_send_timeout设为10–30秒,适应慢速上传;3.proxy_read_timeout匹配应用最长响应时间,避免504错误;4.若负载均衡,可设proxy_next_upstream_timeout限制重试时长——正确配置这些值能显着减少网关超时、提升用户体验,并需结合实际日志和监控持续调优。

Server_name指令做什么? Server_name指令做什么? Aug 02, 2025 pm 04:49 PM

Nginx中的server_name指令用于根据客户端发送的Host头选择处理请求的虚拟主机。具体来说:1.server_name通过精确匹配、通配符或正则表达式匹配Host头,决定使用哪个server块;2.未匹配时会回退到默认server块,通常是第一个或显式标记为default_server的块;3.正确配置server_name有助于避免内容重复、提升SEO并增强性能;4.复杂匹配和通配符应谨慎使用,以保持清晰性和效率。因此,合理设置server_name能确保流量正确路由并简化服务器维

See all articles