Nginx + unicorn to deploy the rails project. As the name suggests, nginx and unicorn must be installed first.
1. Installation
Install nginx: sudo apt-Get install nginx
Install Unicorn: Gem install unicorn
Ii. Configuration
1. Add a unicorn. RB file under the config folder of your project and add code (sample column: http://unicorn.bogomips.org/examples/unicorn.conf.rb)
Mine is as follows:
1 module Rails 2 class << self 3 def root 4 File.expand_path(__FILE__).split("/")[0..-3].join("/") 5 end 6 end 7 end 8 9 preload_app true10 working_directory Rails.root11 pid "#{Rails.root}/tmp/pids/unicorn.pid"12 stderr_path "#{Rails.root}/log/unicorn.log"13 stdout_path "#{Rails.root}/log/unicorn.log"14 15
16 worker_processes 2
17 timeout 3018 19 GC.respond_to?(:copy_on_write_friendly=) and 20 GC.copy_on_write_friendly = true21 22 before_fork do |server, worker|23 defined?(ActiveRecord::Base) and 24 ActiveRecord::Base.connection.disconnect!25 end26 27 after_fork do |server, worker|28 defined?(ActiveRecord::Base) and 29 ActiveRecord::Base.establish_connection30 end
2 configure nginx
Sudo apt-Get install nginx is installed under/etc/nginx by default.
Create the file project_name.conf in the/etc/nginx/CONF. d directory to add code (example: http://unicorn.bogomips.org/examples/nginx.conf)
Mine is as follows:
1 upstream project_name_backend { 2 server unix:/path_to_project/tmp/sockets/unicorn.project-name.sock fail_timeout=0; 3 } 4 5 server { 6 listen 80; 7 server_name localhost; 8 root /path_to_project/public; 9 10 location ^~ /assets/ {11 gzip_static on;12 expires max;13 add_header Cache-Control public;14 }15 16 try_files $uri/index.html $uri @unicorn;17 location @unicorn {18 proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;19 proxy_set_header Host $http_host;20 proxy_redirect off;21 proxy_pass http://project_name_backend;22 }23 24 error_page 500 502 503 504 /500.html;25 client_max_body_size 4G;26 keepalive_timeout 10;27 }
Note:
1
listen "#{Rails.root}/tmp/sockets/unicorn.project-name.sock", :backlog => 64
upstream project_name_backend { server unix:/path_to_project/tmp/sockets/unicorn.project-name.sock fail_timeout=0; }
These two unicorn. project-name.sock paths must be consistent !!!
2
Upstream project_name_backend andProxy_pass http: // project_name_backend; The project_name_backend here is consistent !!!