最近做微信相关开发, 需要部署一个服务器给微信应用做后台。

  项目后端用 ruby on rails, 前端用 angularjs。服务器部署选择 nginx反向代理, rainbows起服务。(本来想用passenger的师兄不让。。。)

 

  rainbows的配置文件放在rails的config目录下面, 修改GEM添加相应包就OK了。

  rainbows 的配置文件:

# rainbows config Rainbows! do use :ThreadPool worker_connections 4 end # paths and things wd = File.expand_path('../../', __FILE__) tmp_path = File.join(wd, 'log') Dir.mkdir(tmp_path) unless File.exist?(tmp_path) socket_path = File.join(tmp_path, 'rainbows.sock') pid_path = File.join(tmp_path, 'rainbows.pid') err_path = File.join(tmp_path, 'rainbows.error.log') out_path = File.join(tmp_path, 'rainbows.out.log') # Use at least one worker per core if you're on a dedicated server, # more will usually help for _short_ waits on databases/caches. worker_processes 4 # 创建多少个进程 # If running the master process as root and the workers as an unprivileged # user, do this to switch euid/egid in the workers (also chowns logs): # user "unprivileged_user", "unprivileged_group" # tell it where to be working_directory wd # listen on both a Unix domain socket and a TCP port, # we use a shorter backlog for quicker failover when busy listen 40000, :tcp_nopush => true # nuke workers after 30 seconds instead of 60 seconds (the default) timeout 30 # feel free to point this anywhere accessible on the filesystem pid pid_path

# 注意这里设置要监听的sock文件路径, rainbows 就是利用这个文件和nginx通信的,
# 从而完成对nginx 反向代理过来的的请求相应的
# nginx 配置文件 upstream 也要做相应的配置:
# upstream app {    
#   # Path to Unicorn SOCK file, as defined previousl    

 
#   server unix:/下面listen设置的socket_path fail_timeout=1s; 
 
# }
# 这种通信方式, 是linux 进程间通信的一种方式, 本地socket
  listen socket_path
# By default, the Unicorn logger will write to stderr.
# Additionally, ome applications/frameworks log to stderr or stdout,
# so prevent them from going to /dev/null when daemonized here:
stderr_path err_path
stdout_path out_path

preload_app true

before_fork do |server, worker|
  # # This allows a new master process to incrementally
  # # phase out the old master process with SIGTTOU to avoid a
  # # thundering herd (especially in the "preload_app false" case)
  # # when doing a transparent upgrade.  The last worker spawned
  # # will then kill off the old master process with a SIGQUIT.
  old_pid = "#{server.config[:pid]}.oldbin"

  if old_pid != server.pid
    begin
      sig = (worker.nr + 1) >= server.worker_processes ? :QUIT : :TTOU
      Process.kill(sig, File.read(old_pid).to_i)
    rescue Errno::ENOENT, Errno::ESRCH
    end
  end
  #
  # Throttle the master from forking too quickly by sleeping.  Due
  # to the implementation of standard Unix signal handlers, this
  # helps (but does not completely) prevent identical, repeated signals
  # from being lost when the receiving process is busy.
  # sleep 1
end

after_fork do |server, worker|
end

 

然后是nginx 的配置文件:#定义要反向代理到的服务器的位置

}
server { listen 80; server_name localhost; # 这里需要同步成对应rails应用的public目录 root /root/my_app/public;
try_files $uri/index.html $uri @app; location @app { proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for; proxy_set_header Host $http_host; proxy_redirect off;
  
     #代理到前面up_stream 定义的位置    proxy_pass http:
//app; } error_page 500 502 503 504 /500.html; client_max_body_size 4G; keepalive_timeout 10; }

 

 

 

参考资料(抄袭原文):

https://www.digitalocean.com/community/tutorials/how-to-deploy-rails-apps-using-unicorn-and-nginx-on-centos-6-5