【问题标题】:Is it possible to run two different Rails apps on the same domain (using a subdirectory)?是否可以在同一个域上运行两个不同的 Rails 应用程序(使用子目录)?
【发布时间】:2012-10-08 16:33:33
【问题描述】:

我有一个在 Rails 3.1 中开发的游戏,我想将它添加到我现有的 Rails 2.3.8 中,而无需升级它。是否可以让这个 3.1 游戏应用程序存在于同一个域中,例如 http://mydomain.com/game?那么任何带有 /game 的 url 都将指向 Rails 3.1 应用程序,而所有其他 url 将指向常规 2.3 应用程序?我将如何使用 nginx 使用子目录来解决这个问题(我宁愿不使用子域并丢失 seo)?

【问题讨论】:

    标签: ruby-on-rails ruby-on-rails-3 routing nginx


    【解决方案1】:

    你可以这样做,但使用像 game.mydomain.com 这样的子域更容易,因为你可以使用 nginx 来处理这个问题。为了区分不同的 ruby​​ 和 rails 版本,请使用 rvm (https://rvm.io/)。

    然后你可以像这样创建一个 nginx 配置:

    upstream mydomain.com {
            server unix:/var/run/thin/mydomain.0.sock;
            server unix:/var/run/thin/mydomain.1.sock;
            server unix:/var/run/thin/mydomain.2.sock;
            server unix:/var/run/thin/mydomain.3.sock;
    }
    upstream game.mydomain.com {
            server unix:/var/run/thin/game.mydomain.0.sock;
            server unix:/var/run/thin/game.mydomain.1.sock;
            server unix:/var/run/thin/game.mydomain.2.sock;
            server unix:/var/run/thin/game.mydomain.3.sock;
    }
    server {
            listen 80;
            server_name mydomain.com;
            access_log /path/to/rails/app/log/access.log;
            error_log /path/to/rails/app/log/error.log;
            root /path/to/rails/app/public;
            index index.html;
            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 off;
    
                    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://mydomain.com;
                            break;
                    }
            }
    }
    server {
            listen 80;
            server_name game.mydomain.com;
            access_log /path/to/rails/app/log/access.log;
            error_log /path/to/rails/app/log/error.log;
            root /path/to/rails/app/public;
            index index.html;
            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 off;
    
                    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://game.mydomain.com;
                            break;
                    }
            }
    }
    

    如果可以接受使用子域而不是子文件夹,那应该为您做。

    如果你真的想使用子目录,你可以使用 nginx locationdirective:

    http://wiki.nginx.org/HttpCoreModule#location

    【讨论】:

    • 将配置保存在一个文件中也很重要,...因为否则配置文件的命名很重要。
    • 感谢您的回答 davidb,但我真的不想将子域用于 seo 目的...
    • 子域不是一般的缺点:mattcutts.com/blog/subdomains-and-subdirectories
    • 但是您丢失了该子域的所有 seo。
    • 你说“seo”是什么意思?页面排名?
    【解决方案2】:

    是的

    在您不想对现有应用程序造成问题的子域上运行它。

    【讨论】:

    • 用子目录怎么办?我宁愿不要失去 SEO。
    • 很抱歉回复晚了,但这不会影响您的 SEO。你想知道关于 SEO 最重要的事情很简单。内容为王,内容越多越好。但你可以在子目录上运行它也取决于你。
    猜你喜欢
    • 2017-05-30
    • 2019-11-05
    • 1970-01-01
    • 2013-01-31
    • 1970-01-01
    • 2020-08-09
    • 1970-01-01
    • 1970-01-01
    • 2014-07-21
    相关资源
    最近更新 更多