Skip to content

Instantly share code, notes, and snippets.

@keikubo
Created March 20, 2012 01:38
Show Gist options
  • Star 48 You must be signed in to star a gist
  • Fork 20 You must be signed in to fork a gist
  • Save keikubo/2129714 to your computer and use it in GitHub Desktop.
Save keikubo/2129714 to your computer and use it in GitHub Desktop.
Nginx + Unicorn for Rails on Rackhub

Nginx + Unicorn for Rails on Rackhub

Description:

This script enables you to launch your Rails application in production environment (port:80) with Nginx and Unicorn.

Installation:

Please make sure that your Gemfile in your rails application includes unicorn.

gem 'unicorn'

Next, check whether you already set $RAILS_ROOT as follows.

echo $RAILS_ROOT

If you do not set it yet, please do so like below.

export RAILS_ROOT=$HOME/sample_app

Please make sure to replace sample_app to your actual Rails project path.

Then, just execute the following command.

curl -s -L https://gist.github.com/raw/2129714/install.sh | bash

Now you may successfully check your Rails application in your browser.

Contact

© 2012 Kei Kubo keikubo@fluxflex.com

Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except in compliance with the License. You may obtain a copy of the License at

www.apache.org/license/LICENSE-2.0

Unless required by applicable law or agreed to in writing, software distributed under the License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the License for the specific language governing permissions and limitations under the License.

#!/usr/bin/env bash
DIR=nginx-unicorn
SCRIPT_HOME=~/$DIR
cd
git clone git://gist.github.com/2129714.git $DIR
cd $SCRIPT_HOME
sed "s#RAILS_ROOT#$RAILS_ROOT#g" $SCRIPT_HOME/nginx.conf > /tmp/nginx.conf
sudo cp /tmp/nginx.conf $SCRIPT_HOME/nginx.conf
sudo ln -s $SCRIPT_HOME/nginx.conf /usr/local/nginx/conf.d/
export SCRIPT_HOME=$SCRIPT_HOME
sudo rm -rf /usr/local/nginx/conf.d/default
$SCRIPT_HOME/restart_nginx_and_unicorn.sh
rm -rf /tmp/nginx.conf
upstream backend {
server unix:/tmp/.unicorn.sock.0;
server unix:/tmp/.unicorn.sock.1;
}
log_format default_log '$host $remote_addr [$time_local] "$request" $status $request_length "$http_referer" "$http_user_agent" $request_time';
server {
listen 80;
server_name _; # all accept
access_log /var/log/nginx/access.log default_log;
location ~ ^/assets/ {
root RAILS_ROOT/public;
gzip_static on; # to serve pre-gzipped version
expires 1y;
add_header Cache-Control public;
add_header ETag "";
break;
}
location / {
proxy_set_header HOST $host;
proxy_set_header X-Real-IP $remote_addr;
proxy_set_header X-Forwarded-For $remote_addr;
proxy_set_header X-Forwarded-Proto $http_x_forwarded_proto;
proxy_pass http://backend;
proxy_redirect off;
}
}
sudo nginx -s stop
pid=`cat $RAILS_ROOT/unicorn.pid`
kill -QUIT $pid
sleep 3
sudo nginx
unicorn -D -d -E production -c $SCRIPT_HOME/unicorn.rb
rails_root = ENV["RAILS_ROOT"]
worker_processes 4
working_directory rails_root # available in 0.94.0+
listen "/tmp/.unicorn.sock.0", :backlog => 64
listen "/tmp/.unicorn.sock.1", :backlog => 64
timeout 30
pid rails_root + "/unicorn.pid"
stderr_path rails_root + "/log/unicorn.stderr.log"
stdout_path rails_root + "/log/unicorn.stdout.log"
preload_app true
GC.respond_to?(:copy_on_write_friendly=) and
GC.copy_on_write_friendly = true
before_fork do |server, worker|
defined?(ActiveRecord::Base) and
ActiveRecord::Base.connection.disconnect!
end
after_fork do |server, worker|
defined?(ActiveRecord::Base) and
ActiveRecord::Base.establish_connection
end
@vikastv
Copy link

vikastv commented Jun 3, 2015

what is the configuration for multiple apps on single ip ? if so how can i do it ..

@sahilsk
Copy link

sahilsk commented Oct 20, 2015

sorry for replying so late. I missed it somehow. For you answer: Just keep adding more server blocks with different server_name and port, if required.
You can have multiple apps using single ip and port using nginx named based hosting.

server {
    listen 80;
    server_name first.myapp.com; # all accept
    access_log /var/log/nginx/first.access.log default_log;

    location ~ ^/assets/ {
        root RAILS_ROOT_FIRST/public;
        gzip_static on; # to serve pre-gzipped version
        expires 1y;
        add_header Cache-Control public;
        add_header ETag "";
        break;
    }

    location / {
        proxy_set_header HOST $host;
        proxy_set_header X-Real-IP $remote_addr;
        proxy_set_header X-Forwarded-For $remote_addr;
        proxy_set_header X-Forwarded-Proto $http_x_forwarded_proto;
        proxy_pass http://backend1;
        proxy_redirect off;
    }
}
server {
    listen 80;
    server_name second.myapp.com; # all accept
    access_log /var/log/nginx/second.access.log default_log;

    location ~ ^/assets/ {
        root RAILS_ROOT_SECOND/public;
        gzip_static on; # to serve pre-gzipped version
        expires 1y;
        add_header Cache-Control public;
        add_header ETag "";
        break;
    }

    location / {
        proxy_set_header HOST $host;
        proxy_set_header X-Real-IP $remote_addr;
        proxy_set_header X-Forwarded-For $remote_addr;
        proxy_set_header X-Forwarded-Proto $http_x_forwarded_proto;
        proxy_pass http://backend2;
        proxy_redirect off;
    }
}

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment