There are three machines and the operating system is centos 6.3 64-bit: 172.16.9.100, install nginx server; the other two 172.16.9.101 and 172.16.9.102 install unicorn as the ROR application server. Ensure that Ruby and rails have been configured on machines 101 and 102. I will not talk about how to install nginx and ror environments here. Many articles have provided a detailed introduction.
Assume that the project/Var/www/demo_projectFolder
Use on 101
Run the gem install unicorn command to install unicorn.
The new file contains the following content. to modify the file, see the Chinese notes in the file./Var/www/demo_project_unicorn.rb,This file is used to configure the production environment of this project.
# Sample verbose configuration file for unicorn (not rack) # This configuration file contains ents containing features of Unicorn # that may not be needed for some applications. see # http://unicorn.bogomips.org/examples/unicorn.conf.minimal.rb# for a much simpler configuration file. # See http://unicorn.bogomips.org/Unicorn/Configurator.html for complete # documentation. # use at least one worker per core if yo U're on a dedicated server, # More will usually help for _ short _ waits on databases/caches. worker_processes 4 # Since unicorn is never exposed to outside clients, it does not need to # run on the standard http port (80 ), there is no reason to start unicorn # as root unless it's from system init scripts. # 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" # help ensure your application will always spawn in the symlinked # "current" directory that Capistrano sets up. working_directory "/var/www/demo_project" # available in 0.94.0 + change to the directory where the project is located # Listen on both a Unix domain socket and a TCP port, # We use a shorter backlog for quicker failover when busylisten "/V Ar/tmp /. unicorn. sock ",: backlog => 64 listen 19527,: tcp_nopush => true # port number, which must be used by nginx # nuke workers after 30 seconds instead of 60 seconds (the default) time out 120 # Feel free to point this anywhere accessible on the filesystempid "/usr/tmp/demo_project/PIDs/Unicorn. PID "# PID file location, which can be set by yourself. Note the permission # by default, the unicorn logger will write to stderr. # Additionally, ome applications/frameworks log Stderr or stdout, # So prevent them from going to/dev/null when daemonized here: stderr_path "/usr/tmp/demo_project/log/Unicorn. stderr. log "# location of the error log. Set it by yourself. Pay attention to the permission stdout_path"/usr/tmp/demo_project/log/Unicorn. stdout. log "# output log location, set by yourself, pay attention to permissions # combine Ruby 2.0.0dev or REE with" preload_app true "for memory savings # http://rubyenterpriseedition.com/faq.html#adapt_apps_for_cowpreload_app truegc. respond_to? (: Copy_on_write_friendly =) and GC. copy_on_write_friendly = true # enable this flag to have unicorn test client connections by writing the # beginning of the HTTP headers before calling the application. this # prevents calling the application for connections that have disconnected # While queued. this is only guaranteed to detect clients on the same # host unicorn runs on, and unlikely to detect disconne CTS even on a # Fast LAN. check_client_connection falsebefore_fork do | Server, worker | # The following is highly recomended for rails + "preload_app true" # as there's no need for the master process to hold a connection defined? (Activerecord: base) and activerecord: Base. Connection. Disconnect! # The following is only recommended for memory/DB-constrained # installations. it is not needed if your system can house # twice as your worker_processes as you have configured. ### 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. th E 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 # 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 processing process is busy. # Sleep 1endafter_fork do | Server, work Er | # per-process listener ports for debugging/admin/migrations # ADDR = "127.0.0.1: #{9293 + worker. NR} "# server. listen (ADDR,: tries =>-1,: delay => 5,: tcp_nopush => true) # The following is * required * For rails + "preload_app true", defined? (Activerecord: base) and activerecord: base. establish_connection # If preload_app is true, then you may also want to check and # restart any other shared sockets/descriptors such as memcached, # And redis. tokyocabinet file handles are safe to reuse # between any number of forked children (assuming your kernel # correctly implements pread ()/pwrite () system CILS) End
Create a shell file named/Var/www/Unicorn. Sh
UNICORN=/usr/local/ruby/bin/unicorn_rails killall -9 unicorn_rails$UNICORN -c /var/www/demo_project_unicorn.rb -D -E production
The first line indicates the installation location of Unicorn. After unicorn is installed successfully, it is in the same folder as executable files such as Ruby and rails. The second line is to disable the unicorn_rails process. The third line starts the project in the production environment according to the configuration after the-C parameter. Run this file to start the unicorn server.
In addition, the configuration of host 102 is the same as that of host 101. The unicorn configuration is complete. Start nginx configuration below
Add the following configuration information to/usr/local/nginx/CONF/nginx. conf:
Upstream demo_project_backend {# connect two ror unicorn servers server 172.16.9.101: 19527; server 172.16.9.102: 19527;} server {Listen 80 default_server; SERVER_NAME _; return 403;} server {Listen 80; SERVER_NAME www. demo_project.com; # access_log logs/host. access. log main; proxy_connect_timeout 500 s; proxy_read_timeout 500 s; proxy_send_timeout 500 s; # forward the request to the two hosts location/{root HTML; index index.html index.htm; proxy_pass http: // demo_project_backend;} # redirect server error pages to the static page/50x.html # error_page 500 502 503 x.html; location =/50x.html {root HTML ;}}