In this article, part one of setting up Nginx with a Mongrel cluster, I will walk through how I install and set up a basic Nginx configuration on my web servers.
Please note that for this article I will be working with Ubuntu Gutsy Gibbon on my Slice and Mac OSX Tiger on my localhost. The article also assumes that you have Rubygems installed.
First, if you want to find out what release of Ubuntu you are using, issue the command:
sudo lsb_release -a
Let's now switch to our sudo account.
sudo -s
Enter your root user account password and then we will move on to the next step.
We are now going to install Nginx via Ubuntu's package manager 'aptitude'. Before I had a working version of Nginx via aptitide I would compile it manually, but found that for my needs installing Nginx via the Ubuntu package manager was sufficient. So let's do it now.
apt-get install nginx
Now to find out the what version of Nginx we have just installed (you will see something similar if not the same), we give the following command
nginx -v
You should see somethings similar to the following.
nginx version: nginx/0.5.26
OK, so that was easy enough (I hope you made it). Now let's get on to the actual configuration of Nginx.
Firstly we need to get into your user directory and make a new directory ready for our Nginx configuration and log files.
cd /home/username/ mkdir nginx
We also want to create some subdirectories as follows:
cd /home/username/nginx/ mkdir tmp logs
Next, and probably one of the most important steps, is your Nginx configuration file. I often put this file in the Nginx directory in my user account, such as:
/home/username/nginx/nginx.conf
Here is a sample nginx.conf file. I use this basic setup. It uses 3 mongrels and 6 worker process'. This may not be appropriate for your applications needs. You should experiment for optimal settings. Just be sure to make all paths correct for your environment and setup.
worker_processes 6;
pid /home/username/nginx/tmp/nginx.pid;
# Valid error reporting levels are debug, notice and info
error_log /home/username/nginx/logs/error.log info;
events {
worker_connections 1024;
}
http {
access_log /home/username/nginx/logs/access.log;
fastcgi_temp_path /home/username/nginx/tmp/fcgi_temp;
client_body_temp_path /home/username/nginx/tmp/client_body 1 2;
proxy_temp_path /home/username/nginx/tmp/proxy_temp;
include /etc/nginx/mime.types;
default_type application/octet-stream;
sendfile on;
tcp_nopush on;
keepalive_timeout 65;
tcp_nodelay off;
gzip on;
gzip_min_length 1100;
gzip_buffers 4 8k;
gzip_types text/plain text/html text/xhtml text/css text/js;
#-------------------------------------------------#
# www.yourwebsite.com #
#-------------------------------------------------#
upstream yourwebsitename {
#### Replace these with the ports for your mongrel cluster:
server 127.0.0.1:8000;
server 127.0.0.1:8001;
server 127.0.0.1:8002;
}
#-------------------------------------------------#
# Settings for www.yourwebsite.com #
#-------------------------------------------------#
server {
#### Replace with your nginx port and domain name:
listen 80;
server_name yourwebsite.com www.yourwebsite.com;
#### Replace with the full path to your rails app's public directory:
root /home/username/yourwebsite.com/current/public;
index index.html index.htm;
### maintenance page
if (-f $document_root/system/maintenance.html) {
rewrite ^(.*)$ /system/maintenance.html last;
break;
}
location / {
proxy_set_header X-Real-IP $remote_addr;
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
proxy_set_header Host $http_host;
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
proxy_set_header Host $http_host;
proxy_redirect false;
if (-f $request_filename/index.html) {
rewrite (.*) $1/index.html break;
}
if (-f $request_filename.html) {
rewrite (.*) $1.html break;
}
if (!-f $request_filename) {
## This is your mongrel upstream name
proxy_pass http://yourwebsitename;
break;
}
}
error_page 500 502 503 504 /50x.html;
location = /50x.html {
root /home/username/yourwebsitename/current/public;
}
}
# end of config file
}
We'll that's about all we can do until we get our Mongrel cluster configured and ready to roll, in part 2 of Setting up Nginx with a Mongrel cluster.
Thanks for this write-up. I finally now have my Mongrel cluster up and running.
Maybe you could do an article on monitoring the performance of the mongrels? that would be really cool.
Thanks again.