使用Rails应用程序和乘客的nginx子域失败

时间:2012-03-15 07:14:51

标签: ruby-on-rails deployment nginx subdomain passenger

我对nginx部署完全陌生,并且在为乘客运行的rails app设置子域时遇到问题。我的app结构就像这样

    -- sss.com (parent domain)
        -- sub.sss.com (subdomain)
    -- zzz.com (which will be redirected to sub.sss.com)

要获得更清晰的视角,请考虑gmail结构

    -- google.com (parent domain)
          - mail.google.com (subdomain)
    -- gmail.com  (which will be redirected to mail.google.com)

请记住,sub.sss.com不仅仅是sss下的一个目录,它完全是一个不同的rails应用程序。

要设置类似的结构,我已经像这样配置了nginx

server {
         listen   80;
         server_name   sss.com *.sss.com;
         rewrite ^(.*) http://sss.com$1 permanent;
    }   

    server {
        listen   80;

        server_name   sss.com;

        passenger_enabled   on;

        access_log   logs/sss.log;
        error_log    logs/sss_error.log; 

        root  /var/www/sss/public;        
    }    
    server {
        listen   80;

        server_name   sub.sss.com;

        passenger_enabled   on;

        access_log   logs/sub.log;
        error_log    logs/sub_error.log;

        root   /var/www/sub/public;        
    } 
    server {
        listen   80;
        server_name   zzz.com;
        rewrite ^(.*) http://sub.sss.com$1 permanent;      
    }      

当我启动nginx时,我收到了此警告消息

nginx: [warn] conflicting server name "sss.com" on 0.0.0.0:80, ignored

尝试访问网址www.sss.com时收到此消息

  

Chrome - 错误310(net :: ERR_TOO_MANY_REDIRECTS):重定向过多。

     

FF - Firefox检测到服务器正在以永远无法完成的方式重定向此地址的请求。

但是当我访问zzz.com时,它成功地重定向到sub.sss.com并出现同样的错误。

似乎它在某种循环中搞砸了。有谁知道如何解决这个问题?

2 个答案:

答案 0 :(得分:1)

在您的第一台服务器中,您也可以在第二台服务器中定义sss.com。你只需要先删除。那样:

server {
     listen   80;
     server_name  *.sss.com;
     rewrite ^(.*) http://sss.com$1 permanent;
}   

server {
    listen   80;

    server_name   sss.com;

    passenger_enabled   on;

    access_log   logs/sss.log;
    error_log    logs/sss_error.log; 

    root  /var/www/sss/public;        
}    
server {
    listen   80;

    server_name   sub.sss.com;

    passenger_enabled   on;

    access_log   logs/sub.log;
    error_log    logs/sub_error.log;

    root   /var/www/sub/public;        
} 
server {
    listen   80;
    server_name   zzz.com;
    rewrite ^(.*) http://sub.sss.com$1 permanent;      
}      

答案 1 :(得分:0)

你有3个域名/子程序员,应该只有3个服务器块而不是你拥有的4个。

尝试......

server {
    # This server block serves sss.com
    listen   80;
    server_name   sss.com;
    passenger_enabled   on;
    access_log   logs/sss.log;
    error_log   logs/sss_error.log; 
    root  /var/www/sss/public;      
}   

server {
    # This server block serves sub.sss.com
    listen   80;
    server_name   sub.sss.com;
    passenger_enabled   on;
    access_log   logs/sub.log;
    error_log   logs/sub_error.log;
    root   /var/www/sub/public;     
} 
server {
    # This server block redirects zzz.com to sub.sss.com
    listen   80;
    server_name   zzz.com;
    rewrite ^ http://sub.sss.com$request_uri? permanent;      
}