nginx subdomain failure with Rails app and passeng

2019-09-17 14:00发布

问题:

I am totally new to nginx deployment and having problem setting up the subdomain for rails app which is running in passenger. My app structure is like this

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

For more clear perspective, think of the gmail structure

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

And remember sub.sss.com is not just a directory under sss, its completely a different rails app.

To setup a similar structure i have configured nginx like this

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;      
    }      

When i start nginx i got this warning message

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

And got this message when tried to access the url www.sss.com

Chrome - Error 310 (net::ERR_TOO_MANY_REDIRECTS): There were too many redirects.

FF - Firefox has detected that the server is redirecting the request for this address in a way that will never complete.

But when i access zzz.com, it successfully redirects to sub.sss.com with a same error.

Seems its messed up in some kind of loop. anybody got a idea how to solve this?

回答1:

In your first server you define the sss.com like server in the second too. You just need delete from first. like that :

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;      
}      


回答2:

You have 3 domains/subdamians and there should be only 3 server blocks instead of the four you had.

Try ...

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;      
}