使用Nginx和Mongrel Cluster部署Rails应用

Posted by ashchan on January 8th, 2008 filed in 部署与优化

本文介绍在Linux上以Nginx为前端、Mongrel Cluster为后端部署Rails应用的方法及步骤。

平台:Ubuntu 7.10 (Gutsy Gibbon)
Nginx简介Nginx是由俄罗斯人Igor Sysoev开发的一款HTTP服务器。它有以下特点:
轻量小巧(相对于Apache等)
快速高效
模块化

Rails开发者应该对Mongrel和Mongrel Cluster很熟悉了,这里不再重复介绍。
安装Mongrel和Mongrel Cluster

1
2
 
sudo gem install mongrel --include-dependencies

注:提示选择要安装的gem版本时,选择最新的ruby版本。

1
sudo gem install mongrel_cluster --include-dependencies

安装Nginx

1
sudo apt-get install nginx

Rails(应用)站点目录结构及配置
我们将Rails站点统一放置于个人用户目录的apps子目录下,每个站点的目录结构如下:

1
2
3
4
5
6
app名称
    releases
        20071225
        20080101
    current
    shared

其中,releases目录下是以每一次发布站点的日期为名称的Rails应用目录,current则是一个soft link,链接至releases下当前运行的发布目录。这为站点的持续发布带来很大方便。

注:以下假定要发布的站点名称为example,域名为example.com,当前用户为james。

创建发布目录

1
2
3
4
5
cd /home/james/appsmkdir examplemkdir example/releases
</code>
取得站点代码(以svn为例)
<pre lang="rails" line="1">
svn co http://somecodebase/example/trunk example/releases/20080101

建立链接

1
ln -s example/releases/20080101 example/current

我们将使用www-data账户来运行Nginx和example应用,所以改一下站点的所有权:

1
sudo chown -R www-data:www-data example/current

建立站点的mongrel cluster配置

1
2
cd example/current/config
sudo mongrel_rails cluster::configure -e production -p 8000 -N 2 -c /home/james/apps/example/current -a 127.0.0.1 --user www-data --group www-data

参数p指定站点使用的起始端口号,参数N指定使用的端口数,参数c指定站点目录。命令执行后,会在config目录下生成一个配置文件,文件名为mongrel_cluster.yml。站点已经配置好。下面在系统一级配置Mongrel。

配置Mongrel
配置自动运行

1
sudo ln -s /usr/lib/ruby/gems/1.8/gems/mongrel_cluster-1.0.2/resources/mongrel_cluster /etc/init.d/mongrel_cluster

注:根据安装的Mongrel Cluster的版本的不同,上述路径可能需作相应修改。

1
sudo chmod +x /etc/init.d/mongrel_clustersudo update-rc.d mongrel_cluster defaults

包含站点mongrel配置

1
sudo mkdir /etc/mongrel_clustersudo ln -s /home/james/example/current/config/mongrel_cluster.yml /etc/mongrel_cluster/example.yml

启动Mongrel Cluster测试站点运行状况

1
sudo /etc/init.d/mongrel_cluster start

该命令手动了Mongrel Cluster,然后用curl来看一下我们的站点example有没有被带起来:

1
curl -L http//127.0.0.1:8000

如果返回了example的首页内容,说明配置无误,接下来可以配置Nginx将站点暴露出去了。

配置Nginx

Nginx的主配置文件为/etc/nginx/nginx.conf。为了方便部署多个虚拟站点,我们将每个可用站点的配置文件置于/etc/nginx/sites-available下,并且将已经发布使用的网站的配置文件链接至/etc/nginx/sites-enabled目录下。example的站点配置文件(/etc/nginx/sites-available/example.conf)内容为:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
upstream example{
 
        server 127.0.0.1:8000;
 
        server 127.0.0.1:8001;
 
}
 
server{
 
        listen       80;
 
        root /home/james/apps/example/current/public;
 
        index index.html index.htm;
 
        server_name example.com www.example.com;
 
        client_max_body_size 50M;
 
        access_log  /var/log/nginx/localhost.access.log;
 
        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_redirect false;
 
                proxy_max_temp_file_size 0;
 
                if (-f $request_filename) {
 
                        break;
 
                }
 
                if (-f $request_filename/index.html){
 
                        rewrite (.*) $1/index.html break;
 
                }
 
                if (-f $request_filename.html) {
 
                        rewrite (.*) $1.html break;
 
                }
 
                if (!-f $request_filename) {
 
                        proxy_pass http://example;
 
                        break;
 
                }
 
        }
 
        error_page   500 502 503 504  /500.html;
 
        location = /500.html {
 
                root  /home/james/apps/example/current/public;
 
        }
 
}

这个配置文件是一个典型的针对Rails应用的Nginx配置文件。不需关注里面所有内容,只要注意以下几点:

upstream节点设置负载到哪几个后端,本例中为127.0.0.1的8000和8001端口(与Mongrel Cluster的设置相对应。);

proxy_pass http://example;这一行中的”example”,设置为upstream后面设定的名称;

server下的root属性,设置为Rails站点的public目录;

server下的server_name属性,设置为站点的域名。

将这一配置链接至sites-enabled目录下:sudo ln -s /etc/nginx/sites-available/example.conf /etc/nginx/sites-enabled/example.conf如果Nginx默认不加载sites-enabled目录下的配置,则需要修改/etc/nginx/nginx.conf,在http节点下,加上这一条:include /etc/nginx/sites-enabled/*;

注:确保nginx.conf里的user属性值为www-data。

启动Nginx

1
sudo /etc/init.d/nginx start

如果一切顺利,并且域名已经解析至当前服务器,则使用http://example.com即可访问example站点。

总结

本文介绍了使用Nginx作为前端,使用Mongrel Cluster作为后端配置部署Rails应用的方法。该方法有以下优点:

以Mongrel Cluster为后端,可以根据站点负载随时调整mongrel实例的数目,甚至可以将站点均衡至不同服务器的mongrel实例以实现水平扩展;

以Nginx作为前端(代理),速度快,占系统资源少。如果站点有静态页面或页面缓存,Nginx可以直接将请求的静态页面或页面缓存返回而无需经过Rails,进一步提升性能;

Rails站点的目录结构设计,简化了发布站点新版本的过程,也方便了随时将站点rollback至某一历史版本(只要三步:停止mongrel cluster,将current链接至目标版本,启动mongrel cluster);要配置为capistrano自动部署也非常方便;

Nginx的配置方式,简化了增加、去除、停用站点的配置过程;

Related posts:

  1. Nginx boot script 目的:让nginx随服务器启动 操作系统: Linux localhost.localdomain 2.6.9-42.ELsmp #1 SMP Wed Jul 12...

Related posts brought to you by Yet Another Related Posts Plugin.


One Response to “使用Nginx和Mongrel Cluster部署Rails应用”

  1. GILBERT Says:


    CheapTabletsOnline.com. Canadian Health&Care.Best quality drugs.Special Internet Prices.No prescription online pharmacy. Low price drugs. Order pills online

    Buy:Zithromax.Viagra Soft Tabs.VPXL.Levitra.Cialis.Soma.Tramadol.Cialis Super Active+.Propecia.Viagra.Cialis Professional.Viagra Super Force.Cialis Soft Tabs.Super Active ED Pack.Viagra Super Active+.Maxaman.Viagra Professional….

Leave a Comment