Pages

Tuesday, 27 May 2014

Configuration Unicorn over nginx

Nginx Using Rails

Introduction

Nginx is an open source reverse proxy server for HTTP,
HTTPS, SMTP, POP3, and IMAP protocols, as well as a load balancer, HTTP cache,
and a web server.

How it works

Nginx works on event driven architecture.
it means that notifications or signals are used to mark the initiation or
completion of a process. Thus, the resources can be used by other process until
a process initiation event is triggered and resource can be allocated and
released dynamically. This leads to the optimized use of memory and CPU. Nginx
doesn’t create a new process for a new request.

Nginx has one master process and several worker
processes. The main purpose of the master process is to read and evaluate
configuration, and maintain worker processes. Worker processes do actual
processing of requests. nginx has focused on high performance, high concurrency
and low memory usage.

Configuration

Unicorn

Write the following code on your local machine config/unicorn.rb

listen "127.0.0.1:8080"

worker_processes 4

preload_app true

user 'vagrant'

root =
"/home/vagrant/app/sample_app/current"

working_directory root

pid
"#{root}/tmp/pids/unicorn.pid"

stderr_path
"#{root}/log/unicorn.log"

stdout_path
"#{root}/log/unicorn.log"

# listen
"/tmp/unicorn.sample_app.sock"

timeout 30

user: set user name of the server.

working_directory: path to the application.

listen: Url which will communicate with
nginx.

Nginx Configuration:

On server machine, open `vi /etc/nginx/nginx.conf` and
paste the below code.