Rails Rails 3.1 Http Streaming 开启方法

huacnlee · 2011年11月20日 · 3838 次阅读

基于 Nginx + Unicorn + Rails 3.1

关于 Http Steaming:

This lets the browser download your stylesheet and javascripts while the server is still generating the response. The result is noticeable faster pages. It’s opt-in and does require support from the web server as well, but the popular combo of nginx and unicorn is ready to take advantage of it.

它可以让浏览器在服务端还在生成 View 的时候,预先下载 Stylesheet 和 Javascript,直接带来的好处可想而知。

开启方式 app/application_controller.rb

class ApplicationController < ActionController::Base
  protect_from_forgery  
  stream # 加这行
end

程序就修改好了,接下来是配置部署环境,首先需要用 Unicorn 来跑 Rails,并且 Unicorn 的配置文件里面 listen 要做修改

listen 5000, :tcp_nopush => false # 要加入后面 tcp_nopush => false 这段

接下来 Nginx 需要修改配置

http {
  # http 区域加入下面两项
  tcp_nopush off; 
  tcp_nodelay on;

  server {
    location / {
      ...
      proxy_buffering    on; # 反向代理处加入这项
      proxy_pass         http://rails_app_backend;
    }
}

测试 Http Streaming 是否已经开启

$ curl -I http://youapp.com
HTTP/1.1 200 OK
Server: nginx/1.1.6
Date: Sat, 19 Nov 2011 15:49:25 GMT
Content-Type: text/html; charset=utf-8
Transfer-Encoding: chunked
Connection: keep-alive
Status: 200 OK

如果返回的信息里面有保函 Transfer-Encoding: chunked 这项,那就说明已经成功了。

暂无回复。
需要 登录 后方可回复, 如果你还没有账号请 注册新账号