Nginx + Unicorn to deploy Rails complete configuration

nginx+Unicorn complete configuration, mainly, nginx.conf and unicorn.rb two configuration file


The 1 configuration nginx file sudo VIM /opt/nginx/conf/nginx.conf

#=== CPU ====
user menxu menxu;
worker_processes 2;
worker_cpu_affinity 00000001 00000010 00000100 00001000 00010000 00100000 01000000 10000000;
worker_rlimit_nofile 204800;


events {
  use epoll;
  worker_connections 204800;
}


# === HTTP ===


http{
  include      mime.types;
  default_type application/octet-stream;


  charset utf-8;


  server_names_hash_bucket_size 128;
  client_header_buffer_size     2k;
  large_client_header_buffers   4 4k;
  client_max_body_size          6m; # A single request data allowed maximum upload size


  sendfile          on;
  tcp_nopush        on;
  tcp_nodelay       on;
  keepalive_timeout 120;
  send_timeout      120;


  gzip              on;
  gzip_buffers      4 32k;
  gzip_min_length   1k;
  gzip_comp_level   2;
  gzip_http_version 1.1;
  gzip_types        text/plain application/x-javascript text/css application/xml;


  # ===== App Server
  upstream app_server{
      server unix:/tmp/unicorn_padrino.sock fail_timeout=0;
  }


  # /web/ruby/blogs
  server{
  listen          80;
  charset         utf-8;
  server_name     menxu.com;
  keepalive_timeout 5;
  
  root            /home/menxu/web/ruby/blogs/public;
  access_log      /home/menxu/web/ruby/blogs/log/nginx_access.log;
  error_log       /home/menxu/web/ruby/blogs/log/nginx_error.log;
  rewrite_log     on;
  
  try_files       $uri/index.html $uri.html $uri @app_server;
  
  location ~* ^/(images|javascripts|stylesheets|img)/{
  access_log      off;
  log_not_found   off;
  expires         max;
  break;
  }
  
  location @app_server {
  proxy_set_header Host $host;
  proxy_set_header X-Forwarded-Host $host;
  proxy_set_header X-Forwarded-Server $host;
  proxy_set_header X-Real-IP        $remote_addr;
  proxy_set_header X-Forwarded-For  $proxy_add_x_forwarded_for;
  proxy_buffering  on;
  proxy_redirect  off;
  
      proxy_pass http://app_server;
  }
  }


}


2 create config/unicorn.rb and configure the unicorn file sudo VIM /home/menxu/web/ruby/blogs/config/unicorn.rb



# -*- encoding: utf-8 -*-
user("menxu","menxu")


root_path = File.expand_path '../', File.dirname(__FILE__)
#log
log_file = root_path + '/log/unicorn.log'
err_log  = root_path + '/log/unicorn_error.log'


# process
pid_file = '/tmp/unicorn_padrino.pid'
old_pid = pid_file + '.oldbin'


#thron
socket_file = '/tmp/unicorn_padrino.sock'


worker_processes 2
working_directory root_path
timeout 30


#listen
listen 8080, tcp_nopush: false
listen socket_file, backlog: 1024


pid pid_file
stderr_path err_log
stdout_path log_file


preload_app true


before_exec do |server|
  ENV['BUNDLE_GEMFILE'] = root_path + '/Gemfile'
end


before_fork do |server, worker|
  if File.exists?(old_pid) && server.pid != old_pid
    begin
      Process.kill('QUIT', File.read(old_pid).to_i)
    rescue Errno::ENOENT, Errno::ESRCH
      puts "Send 'QUIT' signal to unicorn error!"
    end
  end
end






3 write the startup script file sudo vim./blog_server.sh



1>Install wrapper to create a unicorn to run the command with the environment

rvm wrapper ruby-2.0.0-p195@blog2 bootup unicorn


2> Edit the startup script

#! /bin/sh
# rvm wrapper ruby-2.0.0-p195@blog2 bootup unicorn
UNICORN=/home/menxu/.rvm/bin/bootup_unicorn
CONFIG_FILE=/home/menxu/web/ruby/blogs/config/unicorn.rb
APP_HOME=/home/menxu/web/ruby/blogs


case "$1" in
  start)
  $UNICORN -c $CONFIG_FILE -E production -D
  ;;
  stop)
  kill -QUIT 'cat /tmp/unicorn_padrino.pid'
  ;;
  restart|force-reload)
  kill -USR2 'cat /tmp/unicorn_padrino.pid'
  ;;
  *)
  echo "Usage: $SCRIPTNAME{start|top|restart|force-reload" >&2
  exit 3
  ;;
esac





4 start command

sudo ln -s ~/nginx.conf /etc/nginx/conf.d/unicorn.conf # The associated configuration
chmod +x ./one.sh # Execute permissions
./blog_server.sh # Start Unicorn
sudo service nginx start


Posted by Reg at March 15, 2014 - 2:58 AM