joequery
1/19/2012 - 4:19 PM

Nginx individual site config for multiple rails apps with Unicorn

Nginx individual site config for multiple rails apps with Unicorn

##############################################################
# Upstream must have unique name and unique socket.          #
# The socket must match what is in the app's unicorn.rb file #
##############################################################
upstream railsapp1_server {
  server unix:/tmp/railsapp1.sock fail_timeout=0;
}

##############################
# Rewrite www to non-www     #
##############################
server{
  server_name www.railsapp1.com;
  rewrite ^(.*) http://railsapp1.com$1 permanent;
}

##############################
# Server configs go here     #
##############################
server {
  listen 80; 

  client_max_body_size 4G; 
  server_name railsapp1.com;
  keepalive_timeout 5;

  #########################################################
  # This should go to the public folder of your rails app #
  #########################################################
  root /var/www/railsapp1.com/current/public;

  try_files $uri/index.html $uri.html $uri @app;
  location @app {
    proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
    proxy_set_header Host $http_host;
    proxy_redirect off;

		
    #############################################
    # This should be http://upstream; with the  #
    # upstream specified above.                 #
    #############################################
    proxy_pass http://railsapp1_server;
  }
  error_page 500 502 503 504 /500.html;
  location = /500.html {
    #########################################################
    # This should go to the public folder of your rails app #
    #########################################################
    root /var/www/railsapp1.com/current/public;
  }
}