原创作者: LetRails   阅读:4768次   评论:1条   更新时间:2011-05-26    
本文讲述如何在Linux/Unix平台上面搭建Nginx+Mongrel Cluster实现Rails高负载的应用。

安装PCRE库
$ ftp ftp.csx.cam.ac.uk
username: anonymous
> cd pub/software/programming/pcre/
> get pcre-7.4.tar.bz2

> quit
$ tar -jxvf pcre-7.4.tar.bz2
$ cd pcre-7.4
$ ./configure
$ make


不需要安装它,只是编译nginx时需要用到而已。

安装nginx
$ wget http://sysoev.ru/nginx/nginx-0.5.32.tar.gz

$ tar -zxvf nginx-0.5.32.tar.gz
$ cd nginx-0.5.32
$ ./configure –with-pcre=../pcre-7.4
$ make
$ sudo make install


配置nginx
修改/usr/local/nginx/conf/nginx.conf:

user  someuser;
worker_processes  1;

error_log logs/error.log  notice;

pid logs/nginx.pid;

events {
    worker_connections  1024;
}

http {
    include       conf/mime.types;
    default_type  application/octet-stream;

    access_log  logs/access.log;

    sendfile        on;
    tcp_nopush     on;

    keepalive_timeout  65;
    tcp_nodelay        on;

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

    upstream mongrel {
        server 127.0.0.1:8100;
        server 127.0.0.1:8101;
        server 127.0.0.1:8102;
        server 127.0.0.1:8103;
    }

    server {
        listen       80;
        server_name  your.server.com;
        location / {
            proxy_pass http://mongrel;
        }

        root /home/your/app/path;

        access_log  off;
        rewrite_log on;

        location ~ ^/$ {
            if (-f /index.html){
                rewrite (.*) /index.html last;
            }
            proxy_pass  http://mongrel;
            proxy_set_header   Host             $host;
        }

        location / {
            if (!-f $request_filename.html) {
                proxy_pass  http://mongrel;
            }
            rewrite (.*) $1.html last;
        }

        location ~ .html {
            root /home/your/app/path;
        }

        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 /home/your/app/path;
        }
        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;
        }
    }
}


启动 nginx
$ sudo /usr/local/nginx/sbin/nginx
配置启动 mongrel cluster

$ cd /home/your/app/path
$ sudo mongrel_rails cluster::configure -e production \
-p 8100 -N 4 -c /home/your/app/path -a 127.0.0.1 \
–user mongrel –group mongrel
$ sudo mongrel_rails cluster::start


配置 HTTP 认证
添加以下行到 nginx.conf:

location  /  {
    auth_basic            "Restricted";
    auth_basic_user_file  conf/htpasswd;
}


要生成htpasswd文件, 使用Apache附带的htpasswd命令:

$ sudo htpasswd -bc conf/htpasswd user pass

关于htpasswd命令的使用,可使用’htpasswd -h’察看帮助。
清理工作

$ rm -rf pcre-7.4
$ rm -rf nginx-0.5.32

更新:如果遇到redirect_to重定向问题,请检查你的nginx版本,老版本使用$http_host变量,因此应该对proxy_set_header做相应修改:

    proxy_set_header   Host             $host;

版权声明
本文原创作者LetRails,版权声明:署名-非商业性使用-相同方式共享
评论 共 1 条 请登录后发表评论
1 楼 magicgod 2009-03-14 22:43
nginx还是跟thin搭配好一点

发表评论

您还没有登录,请您登录后再发表评论

文章信息

  • robbin在2008-12-06创建
  • robbin在2011-05-26更新
  • 标签: nginx, mongrel, cluster
Global site tag (gtag.js) - Google Analytics