= Nginx / Unicorn

== Words of Warning

I have decided to upload the configuration of my Nginx/Unicorn setup. Please
be aware that:

- I'm using Archlinux and, therefore, the directory hierarchy may change (even for other Linux distros such as Ubuntu).
- I'll assume that your Rails app is located at /home/mssola/myapp, change it at your own needs.
- I'm not saying that NGinx/Unicorn is the best combo ever. Be sure to understand what is NGinx and Unicorn before going any further.

== Setting up NGinx

My Nginx configuration may seem tricky but it's quite clean. My point is that
you just want to have a basic configuration on your /etc/nginx/conf directory
and then enable sites by linking more configuration (specific for each app)
inside the /etc/nginx/sites-enabled directory (not created by default). Here
it goes:

  # Setting up the basic HTTP configuration

  $ sudo cp -f nginx.http.conf /etc/nginx/conf/nginx.conf
  $ sudo mkdir /etc/nginx/sites-enabled

  # Copy the nginx.server.conf to your Rails application config directory.
  # After doing this, you have to edit this file and change the root with
  # the public path of your Rails application (in our case /home/mssola/myapp/public).
  # You also want to change the upstream unicorn block by replacing the 'my_app_name'
  # with the name of your app.

  $ cp nginx.server.conf /home/mssola/myapp/config/nginx.conf
  $ vim /home/mssola/myapp/config/nginx.conf

  # Finally create a soft link pointing to the specific configuration file for
  # this new site we want to enable.

  $ sudo ln -s /home/mssola/myapp/config/nginx.conf /etc/nginx/sites-enabled/my_app_name

After going through the steps shown above, you should be able to start the
nginx daemon successfully. If you try to access to localhost you'll see the
500 page because Unicorn is not configured yet, but at least it proves that
NGinx is properly running.

== Setting up Unicorn

My Unicorn configuration assumes that you want as many worker processes as
CPU cores you have (using a linux-specific command). In order to change this and
more, please configure the unicorn.rb file. In this file you also have to
change the working directory (WD constant) with the path of your Rails
application (/home/mssola/myapp in our case). Don't forget to replace all
the 'my_app_name' with the name of your app. After editing this file, you
have to edit the unicorn_init.sh script and change the APP_ROOT variable
with the path of your Rails application (/home/mssola/myapp again :) ). I
usually place both files to the config directory of my Rails
application. However it's a good practice to copy the script file to the
daemons directory in order to start unicorn at startup (assuming that you
also start the nginx daemon at startup). With this script you can start,
stop, upgrade, etc. Unicorn. In fact this script is quite similar to
the example provided by the Unicorn folks.

== What's next?

Please be sure to understand what's going on the configurators. In order to
fully understand it, check out the awesome documentation for the NGinx and
Unicorn projects. Furthermore, there're more interesting unicorn configurators
on the Unicorn repository, showing more possible scenarios and use cases.

I hope this is helpful :)
