PDA

View Full Version : nginx + several mongrels


dorkalev
11-26-2006, 11:37 AM
Hi,
I just finished installing 2 Rails websites running on mongrel with Nginx as a load-balancer. The installation is easy (once you understand what you are doing) and the final result works like a charm.

The final Result:
Two Rails websites running on mongrel on a local IP, each in a cluster of two ports.
An Nginx server is serving each website to the outside world.

Site A runs on 127.0.0.1:8000 & 127.0.0.1:8001
Site B runs on 127.0.0.1:7000 & 127.0.0.1:7001

Nginx Serving site A @ http://www.sitea.com (spreading the load between 127.0.0.1 ports 8000 & 8001)

Nginx Serving site B @ http://www.siteb.com (spreading the load between 127.0.0.1 ports 7000 & 7001)

The way:
1. Install Mongrel, Mongrel Cluster and Nginx as explained @ integral thoughts (http://blog.integralimpressions.com/articles/2006/08/30/nginx-the-front-end-solution-for-rails-deployment) (notice the current version of Nginx is newer than 3.6)
2. for site A use: mongrel_rails cluster::configure -p 8000 -e production -a 127.0.0.1 where needed
3. for site A use: mongrel_rails cluster::configure -p 7000 -e production -a 127.0.0.1 where needed
4. run both mongrels
5. In the nginx.conf file write:
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 site-a {
server 127.0.0.1:8000;
server 127.0.0.1:8001;
}

upstream site-b {
server 127.0.0.1:7000;
server 127.0.0.1:7001;
}

gzip on;
gzip_min_length 1100;
gzip_buffers 4 8k;
gzip_types text/plain;

server {
listen 80;
server_name site-b.com;
access_log off;

# / -> first search for local index.html then go to site-b
location ~ ^/$ {
if (-f /index.html){
rewrite (.*) /index.html last;
}
proxy_pass http://site-b;
}

# rail caching: searching first for $action.html local pages
location / {
if (!-f $request_filename.html) {
proxy_pass http://site-b;
}
rewrite (.*) $1.html last;
}

location ~* ^.+\.(jpg|jpeg|gif|png|ico|css|zip|tgz|gz|rar|bz2| doc|xls|exe|pdf|ppt|txt|tar|mid|midi|wav|bmp|rtf|j s$
root /var/www/site-b/public;
}

# resend everything else to site-b
location / {
proxy_pass http://site-b;
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;
}
}

server {
listen 80;
server_name site-b.com;
root /var/www/site-b/public;

access_log off;
rewrite_log on;

# / -> first search for local index.html then go to
location ~ ^/$ {
if (-f /index.html){
rewrite (.*) /index.html last;
}
proxy_pass http://site-b;
}

# rail caching: searching first for $action.html local pages
location / {
if (!-f $request_filename.html) {
proxy_pass http://site-b;
}
rewrite (.*) $1.html last;
}


# serve static files directly
location ~ .html {
root /var/www/site-b/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|j s$
root /var/www/site-b/public;
}

# resend everything else to site-b
location / {
proxy_pass http://site-b;
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;
}

}
}
6. check the nginx.conf file:
nginx -t -c nginx.conf
7. if its fine just run it:
nginx -c nginx.conf
8. enjoy!

Dor.