这是我为解决问题而设置的一个简单的Rails 4项目:
https://github.com/rejacobson/rails4-streamtest
我在/ home/stream设置了一个路由,它应该以1秒的间隔流式传输一行文本5次.
def stream
5.times do |n|
puts "Streaming: #{n}"
response.stream.write "Streaming: #{n+1}"
sleep 1
end
rescue IOError => e
puts 'Connection closed'
ensure
response.stream.close
end
Run Code Online (Sandbox Code Playgroud)
当我使用tcp://运行puma,没有nginx时,流式传输完美无缺.
curl -N http://localhost:3000/home/stream
Run Code Online (Sandbox Code Playgroud)
我得到5行流回来,没问题.
当我引入nginx时,curl将输出第一行但在此之后立即退出.我继续看到来自服务器和日志的puts调用的输出,所以我知道请求仍在5.times循环中处理.
它也不会像用户切断连接时那样抛出IOError异常.
这是我到目前为止所尝试的:
搜索互联网也提供了很少的帮助.
我很茫然,需要一些指导.
这是两个curl调用的输出,有和没有nginx.
~/projects/streamtest ? master ?
ryan mirage ???? curl -i -N http://192.168.1.100:3000/home/stream
HTTP/1.1 200 OK
X-Frame-Options: SAMEORIGIN
X-XSS-Protection: 1; mode=block
X-Content-Type-Options: nosniff
X-UA-Compatible: chrome=1
Cache-Control: no-cache
Content-Type: text/html; charset=utf-8
Set-Cookie: request_method=GET; path=/
X-Request-Id: 9ce86358-4476-404a-97e5-769c16ec7b0c
X-Runtime: 0.978099 …Run Code Online (Sandbox Code Playgroud)