01.01.07
nginx: the front end solution for rails deployment?
by Chris Abad
This article is a simple tutorial for getting nginx and mongrel_clusters up and running for your rails applications. It’s based off the great intro article by
Ezra Zygmuntowicz
. I used his installation steps and configuration file. Please take a look at his articles for further details.
nginx
(pronounced: “engine x”) is an HTTP -server that is under active development by
Igor Sysoev
. It runs on any nix platform, including OSX (which we’ll be using in this tutorial), Solaris, Linux, and FreeBSD. For Rails applications, it’s meant to be used to serve your static and cached files inside your rails application. The rest is handled by a mongrel_cluster. Think of it as a replacement for either of the two current popular solutions, Apache 2.2 w/mod_proxy_balancer, or Lighttpd. Both sit in front of that mongrel_cluster and serve static content.
Setup:
Install mongrel:
sudo gem install mongrel
Install mongrel_cluster:
sudo gem install mongrel_cluster
Install nginx:
curl -O http://sysoev.ru/nginx/nginx-0.3.60.tar.gz tar -xzvf nginx-0.3.60.tar.gz cd nginx-0.3.60 ./configure --sbin-path=/usr/local/sbin --with-http_ssl_module make sudo make install sudo chmod +x /usr/local/sbin/nginx
Nginx will install to /usr/local/nginx and in there you will find the conf directory containing the nginx.conf file, the logs directory with the error and access logs, as well as the PID for the nginx process.
Once you’ve got everything installed, create a new Rails application wherever you’d like.
rails demo
Then, create a mongrel_cluster for your application. I’m going to use ports 8000 & 8001, but you can use whatever you’d like. I’ve also set the environment to production, but again, you can set that to whatever you’d like. This will create a config file located at demo/config/mongrel_cluster.yml. You can create as many mongrel instances as you would like.
mongrel_rails cluster::configure -p 8000 -e production -a 127.0.0.1
Start the mongrel cluster
mongrel_rails cluster::start
This can be stopped or restarted just as easily by typing:
mongrel_rails cluster::stop mongrel_rails cluster::restart
So now your app should have it’s mongrel cluster running fine. You can test by loading up either http://localhost:8000/ or http://localhost:8001/.
Now go ahead and open up the nginx.conf file located at /usr/local/nginx/conf/nginx.conf in TextMate or your favorite editor. There is a default conf file there, but it wouldn’t hurt to back up the old one.
Paste in the following code or download a .conf file with the same info
here
:
worker_processes 2; error_log logs/error.log notice; pid logs/nginx.pid; events { worker_connections 1024; } http { include conf/mime.types; default_type application/octet-stream; # no sendfile on OSX uncomment #this if your on linux or bsd #sendfile on; tcp_nopush on; keepalive_timeout 65; tcp_nodelay on; upstream mongrel { server 127.0.0.1:8000; server 127.0.0.1:8001; } gzip on; gzip_min_length 1100; gzip_buffers 4 8k; gzip_types text/plain; server { listen 80; #server_name localhost; root /path_to/rails/public; access_log off; rewrite_log on; # / -> first search for local index.html then go to mongrel location ~ ^/$ { if (-f /index.html){ rewrite (.*) /index.html last; } proxy_pass http://mongrel; } # rail caching: searching first for $action.html local pages location / { if (!-f $request_filename.html) { proxy_pass http://mongrel; } rewrite (.*) $1.html last; } # serve static files directly location ~ .html { root /path_to/rails/public; } location ~* ^.+\.(jpg|jpeg|gif|png|ico|css|zip|tgz|gz|rar|bz2|doc|xls|exe|pdf|ppt|txt|tar|mid|midi|wav|bmp|rtf|js|mov)$ { root /path_to/rails/public; } # resend everything else to mongrel location / { proxy_pass http://mongrel; proxy_redirect off; proxy_set_header Host $host; proxy_set_header X-Real-IP $remote_addr; proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for; } } }
Set /path_to/rails/public to whatever the path to your rails app is.
Once you’ve gotten your nginx.conf file saved, fire up nginx and let it rip. You start nginx by calling that executable that was created when you built from source earlier. In the example above it would be:
sudo nginx
Sidenote:
make sure Apache is turned off, it’s most likely been turned on in Personal Web Sharing under System Preferences → Sharing. Load up http://localhost, and you should see a blank Rails app from there.
Nginx is managed by sending UNIX signals to that process, similar to apache. For more info on that, visit
Zhesto’s notes
.
Nginx has a lot of buzz right now about being a potential solution for being the lightweight front end for mongrel_clusters. Whether or not that happens remains to be seen, but hopefully now you can help make that decision.
All credit for this article goes to
Ezra Zygmuntowicz
and
Alexy Kovyrin
for posting enough info to get started, as well as
Bradley Taylor
for the mongrel_cluster plugin. Please check out those blogs for more information.
Comments
There are no comments.
Leave a Comment