Wednesday, August 1, 2007

Setting nginx to serve multiple rails apps.

Say I need to configure nginx + mongrel clusters to serve multiple rails applications residing on one server, using one domain and using ssl encryption.

I want
http://www.my-domain.com/app1/... to be mapped to application 'app1',
http://www.my-domain.com/app2/... to be mapped to 'app2' and so on.

Well, It took me some time to make this work....

Here are the major steps I took:


Installation

Install mongrel
Install and configure mongrel cluster
Download nginx source & compile (do not forget to use the --with-http_ssl_module option in the configuration phase if you intend to use ssl)


Configurations

First, in each rails application directory, open config/mongrel_cluster.yml file and add the following line:
prefix: /app-name
app-name refers to the part of the url coming right after the domain name.
(www.my-domain.com/app-name/...) It is used to 'map' the url to the right application.

For instance if www.my-domain.com/my-admin/... should be mapped to the application sitting under 'admin-app' directory, then:


# add line to /admin-app/config/mongrel_cluster.yml
prefix: /my-admin


(See full options list here, thanks Alex)


Now to the nginx.conf file.
Generally follow the recommendations of Ezra Zygmuntowicz who is a true expert.
His configuration file however, does not include all the needed ssl stuff, and does not fit to serve multiple applications. So regarding ssl: here is the missing part - follow it carefully to have ssl working right.

To have multiple applications working, one has to define an 'upstream' block for each mongrel cluster and use 'local' block for each rails application.
Here is a nice guide that explains how to serve multiple applications each with it's own domain. The definition of specific cluster per application is kept in my case too, however
I need only one server that contains multiple 'locals'.

Here is an nginx.conf file example:



# user and group to runuser  nadav;
# number of nginx workers - recommendation 1 for ssl
worker_processes 1;

# pid of nginx master process
pid /var/run/nginx.pid;

# Number of worker connections. 1024 is a good default
events {
worker_connections 1024;
}

# start the http module where we config http access.
http {
# pull in mime-types. You can break out your config
# into as many include's as you want to make it cleaner

include /usr/local/nginx/conf/mime.types;

# set a default type for the rare situation that
# nothing matches from the mimie-type include

default_type application/octet-stream;

# configure log format
log_format main '$remote_addr - $remote_user [$time_local] '
'"$request" $request_filename $status $body_bytes_sent "$http_referer" '
'"$http_user_agent" "$http_x_forwarded_for"';


# main access log
access_log /usr/local/nginx/logs/nginx_access.log main;

# main error log
error_log /usr/local/nginx/logs/nginx_error.log debug;

# no sendfile on OSX
sendfile on;

# These are good default values.
tcp_nopush on;
tcp_nodelay off;

# output compression saves bandwidth
gzip on;
gzip_http_version 1.0;
gzip_comp_level 2;
gzip_proxied any;
gzip_types text/plain text/html text/css application/x-javascript text/xml application/xml application/xml+rss text/javascript;


# this is where you define your mongrel clusters.
# you need one of these blocks for each cluster
# and each one needs its own name to refer to it later.

upstream app1_mongrel {
server 127.0.0.1:5000;
server 127.0.0.1:5001;
}

upstream app2_mongrel {
server 127.0.0.1:7000;
server 127.0.0.1:7001;
}



# the server directive is nginx's virtual host directive.

server {

# port to listen on.
# Can also be set to an IP:PORT. Port 443 used for ssl

listen 443;

ssl on;
ssl_certificate /usr/local/nginx/certs/server.crt;
ssl_certificate_key /usr/local/nginx/certs/server.key;

ssl_session_timeout 5m;

ssl_protocols SSLv2 SSLv3 TLSv1;
ssl_ciphers ALL:!ADH:!EXPORT56:RC4+RSA:+HIGH:+MEDIUM:+LOW:+SSLv2:+EXP;
ssl_prefer_server_ciphers on;
keepalive_timeout 70;

# Set the max size for file uploads to 50Mb
client_max_body_size 50M;

# sets the domain[s] that this vhost server requests for
server_name http://www.my-domain.com/;
# doc root
root html;

# vhost specific access log
access_log /usr/local/nginx/logs/nginx.vhost.access.log main;

# this rewrites all the requests to the maintenance.html
# page if it exists in the doc root. This is for capistrano's
# disable web task

if (-f $document_root/system/maintenance.html) {
rewrite ^(.*)$ /system/maintenance.html last;
break;
}

# include locations
include /usr/local/nginx/conf/locals/app1.conf;
include /usr/local/nginx/conf/locals/app2.conf;

error_page 500 502 503 504 /500.html;
location = /500.html {
root /var/www/rails/some_app/public;
}
}
}



I created the 'locals' sub-directory under nginx/conf.
It contains files holding the 'local' block for each application.
Those files are 'included' as you see at the end of the server block.
An example:


# nginx/conf/locals/app1.conf file
location /app1-prefix {
root /path-to-rails-app1/public;


# needed to forward user's IP address to rails
proxy_set_header X-Real-IP $remote_addr;

# needed for HTTPS

proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;

# set X-FORWARDED_PROTO so ssl_requirement plugin works
proxy_set_header X-FORWARDED_PROTO https;
proxy_set_header Host $http_host;
proxy_redirect false;
proxy_max_temp_file_size 0;

# If the file exists as a static file serve it directly without
# running all the other rewite tests on it

if (-f $request_filename) {
break;
}


# check for index.html for directory index
# if its there on the filesystem then rewite
# the url to add /index.html to the end of it
# and then break to send it to the next config rules.

if (-f $request_filename/index.html) {
rewrite (.*) $1/index.html break;
}


# this is the meat of the rails page caching config
# it adds .html to the end of the url and then checks
# the filesystem for that file. If it exists, then we
# rewite the url to have explicit .html on the end
# and then send it on its way to the next config rule.
# if there is no file on the fs then it sets all the
# necessary headers and proxies to our upstream mongrels

if (-f $request_filename.html) {
rewrite (.*) $1.html break;
}

if (!-f $request_filename) {
proxy_pass
http://app1_mongrel/;
break;
}
}



The /app1-prefix (after 'location') should be the same as the one defined in the mongrel_cluster.yml
(Described above).
Good luck!!