部署在Posit Connect的R Plumber API超60秒触发504超时问题求助
Plumber API on Posit Connect Returns 504 Gateway Timeout After 60s (Process Continues Running)
问题概述
在Ubuntu 20.04虚拟机部署的Posit Connect(v2023.03.0)上运行Plumber API时,当API处理流程(如调用外部API获取数据)耗时超过60秒,会返回504 Gateway Time-out - The server didn't respond in time.错误,但API后台进程仍会继续执行并完成后续操作(例如写入数据到Connect Pin)。
已调整Posit Connect的运行时参数:
- Initial Timeout: 600s(默认60s)
- Connection timeout: 3600s
- Idle Timeout: 120s
- Read timeout: 3600s
排除Posit Connect自身配置问题,怀疑是虚拟化平台或环境防火墙层面的超时限制导致此问题。
复现代码
library(plumber) library(pins) ## Configure API #* @apiTitle Testing Plumber Posit Connect Timeout #* @apiVersion 0.0.1 #* @apiDescription Reprex to replicate Plumber API 60s timeout ## Configure API endpoint #* @get /TimeoutTest #* @param Delay The time in seconds which the API should wait before returning a response. #* @response default Returns a dataframe in JSON format TimeoutTest <- function(Delay) { ## Delay API response for the period specified in Delay StartTime = Sys.time() Sys.sleep(Delay) DelayEnd = Sys.time() ## Configure API payload reponse payload <- paste0("Delay: ", Delay, "s, Start Time: ", StartTime, " - End Time: ", DelayEnd) ## configure and write the payload to a Posit Connect Pin board <- board_connect( server = "https://connect.server.com", auth = "manual", key= Sys.getenv("CONNECT_API_KEY") ) pin_write(board = board, payload, name = "API_Timeout_Pin", type = "json") ## return payload as the API GET request response return(payload) }
测试结果:当Delay参数小于60秒时,API正常返回JSON payload并写入Connect Pin;当Delay大于60秒时,返回504错误但仍会完成pin写入操作。
排查与解决方向
- 检查虚拟化平台的反向代理/负载均衡超时:若虚拟机所在的虚拟化环境(如VMware、KVM)内置反向代理或负载均衡器,可能默认设置了60秒超时阈值,需调整对应组件的超时参数(例如Nginx的
proxy_read_timeout、Apache的Timeout)。 - 排查防火墙/安全组规则:确认环境中的防火墙或云安全组是否存在60秒的连接超时限制,修改规则延长超时时间。
- 验证外部前端代理配置:如果Posit Connect前端使用了外部反向代理(如Nginx),即使Connect本身设置了长超时,代理层的60秒限制仍会触发504。需在代理配置中添加超时设置,例如在Nginx的对应
server块中添加:proxy_read_timeout 600s; proxy_connect_timeout 3600s; - 直连Connect端口测试:绕过前端代理,直接访问Posit Connect的服务端口(默认3939),发起超过60秒的请求,验证是否能正常返回,以此确认问题是否来自前端代理或防火墙。
内容的提问来源于stack exchange,提问作者Jaco
相关产品推荐
相关产品推荐

