30th Sep, 2007

nginx+mongrel cluster配置指南

准备工作

安装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;

评论

[...] Ruby on Rails: LetRails » nginx+mongrel cluster配置指南 (tags: nginx mongrel cluster RubyonRails) [...]

使用nginx+mongrel在post的时候会出现ActionController::InvalidAuthenticityToken

而不使用nginx,直接mongrel_rails start -a 127.0.0.1 -p 8000则完全正常.

是不是因为nginx作为代理服务器转发给mongrel后被rails认为是站外提交的?

留条评论?

Your response:

Categories