Skip to content

Instantly share code, notes, and snippets.

@diegobarros0701
Created November 5, 2017 00:40
Show Gist options
  • Save diegobarros0701/d50535dfc0c7476a048ae4cfeacd61a7 to your computer and use it in GitHub Desktop.
Save diegobarros0701/d50535dfc0c7476a048ae4cfeacd61a7 to your computer and use it in GitHub Desktop.
RoR with Nginx and Unicorn

Nginx configuration

Add the following code in nginx.conf, inside http block

upstream rails {
  # Path to Unicorn socket file
  server unix:/home/username/project_name/shared/sockets/uni$
}

Unicorn configuration

Install the gem

gem install unicorn

And then create unicorn.rb in your project inside config directory and put the content below

# set path to the application
app_dir = File.expand_path("../..", __FILE__)
shared_dir = "#{app_dir}/shared"
working_directory app_dir

# Set unicorn options
worker_processes 2
preload_app true
timeout 30

# Path for the Unicorn socket
listen "#{shared_dir}/sockets/unicorn.sock", :backlog => 64

# Set path for logging
stderr_path "#{shared_dir}/log/unicorn.stderr.log"
stdout_path "#{shared_dir}/log/unicorn.stdout.log"

# Set proccess id path
pid "#{shared_dir}/pids/unicorn.pid"

Sites-available and sites-enabled

Create inside sites-available on nginx a file with the content below

server {
listen 80;
server_name localhost;

root /home/deploy/gerenciador_estudos;

try_files $uri/index.html $uri @rails;

location @rails {
   proxy_pass http://rails;
   proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
    proxy_set_header Host $http_host;
   proxy_redirect off;
}

error_page 500 502 503 504 /500.html;
client_max_body_size 4G;
keepalive_timeout 10;
}

After that, create a symbolic link to sites-enable. Example

ln -s /etc/nginx/sites-available/project_name /etc/nginx/sites-enabled/project_name

Managing Unicorn

To start Unicorn go inside your project and execute:
For development environment

 sudo unicorn -c config/unicorn.rb -E development -D

Or, for production

 sudo unicorn -c config/unicorn.rb -E production -D

To stop, execute:

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