SSE/EventSource 在第一块数据后关闭(Rails 4 + Puma + Nginx) [英] SSE / EventSource closes after first chunk of data (Rails 4 + Puma + Nginx)

查看:23
本文介绍了SSE/EventSource 在第一块数据后关闭(Rails 4 + Puma + Nginx)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我关注了 ,这是我目前的nginx配置:

上游美洲狮{服务器unix:///home/deploy/apps/outy/shared/tmp/sockets/outy-puma.sock;保活16;}服务器 {听 80 default_server 延迟;root/home/deploy/apps/outy/current/public;access_log/home/deploy/apps/outy/current/log/nginx.access.log;error_log/home/deploy/apps/outy/current/log/nginx.error.log 信息;位置 ^~/assets/{gzip_static 开启;最大到期;add_header 缓存控制公共;}try_files $uri/index.html $uri @puma;位置@puma {proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;proxy_set_header 主机 $http_host;proxy_redirect 关闭;proxy_pass http://puma;proxy_http_version 1.1;proxy_set_header 连接"";代理缓冲关闭;proxy_cache 关闭;}error_page 500 502 503 504/500.html;client_max_body_size 10M;keepalive_timeout 10;}

解决方案

我会分享有效的配置.

nginx

上游 app_server {服务器unix:/var/tmp/sockets/puma.sock失败超时=0;}服务器 {听 80 default_server;听 443 ssl;client_max_body_size 8m;server_tokens 关闭;server_name 本地主机;keepalive_timeout 5;地点/{try_files @uri @app;}位置@app {proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;proxy_set_header X-Forwarded-Proto $http_x_forwarded_proto;proxy_set_header 主机 $http_host;proxy_redirect 关闭;proxy_http_version 1.1;chunked_transfer_encoding off;代理缓冲关闭;proxy_pass http://app_server;}}

控制器

class StreamController <动作控制器::基础包括 ActionController::Live你好response.headers["Content-Type"] = "text/event-stream" #;字符集=utf-8"10次​​ {response.stream.write("数据:Hello World!!

")睡觉 1}拯救IO错误放置流 IO 错误"logger.info 流 IO 错误"确保将流关闭"logger.info流已关闭"response.stream.close结尾结尾

卷曲

$ curl -i -N http://localhost/stream/helloHTTP/1.1 200 正常服务器:nginx日期:2015 年 7 月 29 日,星期三 09:15:43 GMT内容类型:文本/事件流传输编码:分块连接:保持连接X-Frame-Options: SAMEORIGINX-XSS-保护:1;模式=块X-Content-Type-Options: nosniff缓存控制:无缓存X 请求 ID:41e80567-d792-4a48-9ec3-c661aa056081X 运行时:0.062282变化:起源数据:你好世界!!数据:你好世界!!数据:你好世界!!数据:你好世界!!数据:你好世界!!数据:你好世界!!数据:你好世界!!数据:你好世界!!数据:你好世界!!数据:你好世界!!

I followed the 401-ActionController-Live Railscast and this Blog Post about Server-Sent-Events to set up something similar in my Rails app. It works perfectly when I open connections to the server when only using puma but with puma + nginx, the connection closes after the first chunk of data is sent.

I also tried following the solutions provided in these questions but they didn't work for me:

This is what I'm getting:

This is How I set up my Server and this is my current nginx configuration:

upstream puma {
  server unix:///home/deploy/apps/outy/shared/tmp/sockets/outy-puma.sock;
  keepalive 16;
}

server {
  listen 80 default_server deferred;

  root /home/deploy/apps/outy/current/public;
  access_log /home/deploy/apps/outy/current/log/nginx.access.log;
  error_log /home/deploy/apps/outy/current/log/nginx.error.log info;

  location ^~ /assets/ {
    gzip_static on;
    expires max;
    add_header Cache-Control public;
  }

  try_files $uri/index.html $uri @puma;
  location @puma {
    proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
    proxy_set_header Host $http_host;
    proxy_redirect off;

    proxy_pass http://puma;

    proxy_http_version 1.1;
    proxy_set_header Connection "";
    proxy_buffering off;
    proxy_cache off;
  }

  error_page 500 502 503 504 /500.html;
  client_max_body_size 10M;
  keepalive_timeout 10;
}

解决方案

I will share configuration that worked.

nginx

upstream app_server {
    server unix:/var/tmp/sockets/puma.sock
    fail_timeout=0;
}

server {
    listen 80 default_server;
    listen 443 ssl;
    client_max_body_size 8m;
    server_tokens off;

    server_name localhost;

    keepalive_timeout 5;

    location / {
        try_files @uri @app;
    }

    location @app {
      proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
      proxy_set_header X-Forwarded-Proto $http_x_forwarded_proto;
      proxy_set_header Host $http_host;
      proxy_redirect off;

      proxy_http_version 1.1;
      chunked_transfer_encoding off;

      proxy_buffering off;

      proxy_pass http://app_server;
    }
}

controller

class StreamController < ActionController::Base
  include ActionController::Live

  def hello
    response.headers["Content-Type"] = "text/event-stream" #; charset=utf-8"

    10.times {
      response.stream.write("data: Hello World!!

")
      sleep 1
    }
  rescue IOError
    puts "Stream IO Error"
    logger.info "Stream IO Error"
  ensure
    puts "Stream closed"
    logger.info "Stream closed"
    response.stream.close
  end
end

curl

$ curl -i -N http://localhost/stream/hello
HTTP/1.1 200 OK
Server: nginx
Date: Wed, 29 Jul 2015 09:15:43 GMT
Content-Type: text/event-stream
Transfer-Encoding: chunked
Connection: keep-alive
X-Frame-Options: SAMEORIGIN
X-XSS-Protection: 1; mode=block
X-Content-Type-Options: nosniff
Cache-Control: no-cache
X-Request-Id: 41e80567-d792-4a48-9ec3-c661aa056081
X-Runtime: 0.062282
Vary: Origin

data: Hello World!!

data: Hello World!!

data: Hello World!!

data: Hello World!!

data: Hello World!!

data: Hello World!!

data: Hello World!!

data: Hello World!!

data: Hello World!!

data: Hello World!!

这篇关于SSE/EventSource 在第一块数据后关闭(Rails 4 + Puma + Nginx)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆