小编Luk*_*yer的帖子

通过Nginx发送EventSource/Server-Sent事件

在服务器端使用带有stream块的Sinatra .

get '/stream', :provides => 'text/event-stream' do
  stream :keep_open do |out|
    connections << out
    out.callback { connections.delete(out) }
  end
end
Run Code Online (Sandbox Code Playgroud)

在客户端:

var es = new EventSource('/stream');
es.onmessage = function(e) { $('#chat').append(e.data + "\n") };
Run Code Online (Sandbox Code Playgroud)

当我直接使用应用程序,通过http://localhost:9292/,一切都很完美.连接是持久的,所有消息都传递给所有客户端.

但是当它通过Nginx时http://chat.dev,连接被丢弃并且重新连接每隔一秒左右触发一次.

Nginx设置对我来说没问题:

upstream chat_dev_upstream {
  server 127.0.0.1:9292;
}

server {
  listen       80;
  server_name  chat.dev;

  location / {
    proxy_pass http://chat_dev_upstream;
    proxy_buffering off;
    proxy_cache off;
    proxy_set_header Host $host;
  }
}
Run Code Online (Sandbox Code Playgroud)

尝试keepalive 1024upstream部分以及proxy_set_header Connection keep-alive;在 …

ruby nginx sinatra

66
推荐指数
4
解决办法
2万
查看次数

标签 统计

nginx ×1

ruby ×1

sinatra ×1