Meteor 如何处理多个虚拟主机?

Meteor 如何处理多个虚拟主机?

问题描述:

Meteor 如何处理多个虚拟主机?

How can Meteor handle multiple Virtual Hosts?

www.Some-Client-Domain.com --> www.Our-CName-URL.com --> Meteor 应用程序.

www.Some-Client-Domain.com --> www.Our-CName-URL.com --> Meteor app.

我们需要 Meteor 应用服务于相同的网站/应用,但具有特定于请求的原始 URL (Some-Client-Domain.com) 的数据.

We need the Meteor app to serve the same site/app but with data specific to the original URL requested (Some-Client-Domain.com).

在我们当前的原型中,我们在 Rails 前面有 NGINX,有几种不同的方法可以做到这一点,包括将 NGINX 连接到数据库以定义许多虚拟主机.这很有效,因为如果有新客户端注册,我们可以更新数据库,然后 NGINX 立即知道该虚拟主机,无需任何进一步的 NGINX 配置.

In our current prototype, we have NGINX in front of Rails, and there are a few different ways to do this, including wiring NGINX to the DB for definitions of the MANY Virtual Hosts. This works great, because if a new client signs up, we can update the DB, and then NGINX immediately knows about that Virtual Host without any further NGINX configuration.

如何在 Meteor 中实现这一点?

How would this be accomplished in Meteor?

谢谢!

好吧,如果你暂时忽略 SSL(或者想在以后自己弄清楚 SSL),下面的指南应该有效:

Well, if you just ignore SSL for now (or want to figure out SSL for yourself later on), the below guide should work:

.. 是根据基本 URL 生成具有不同数据库(mongo,通常情况下)的同一应用程序的多个实例.

.. is to spawn multiple instances of the same application with different databases (mongo, the usual case) depending on the base URL.

我们将为虚拟主机使用以下设置:

We are going to use the following settings for the virtual hosts:

  • 站点 #1:www.example1.com
    • 流星端口:3000
    • MongoDB 端点/url:mongodb://localhost:27017/example1
    • 流星端口:3001
    • MongoDB 端点/url:mongodb://localhost:27017/example2
    1. 通过rubygems安装foreman:

    在您的meteor 项目目录中创建一个foreman Procfile 文件.使用上面的数据(不包括项目符号:D):

    Create a foreman Procfile file in your meteor project directory. Using the data above (don't include the bullets :D):

    • web1: ROOT_URL=http://www.example1.com/PORT=3000 MONGO_URL=mongodb://localhost:27017/example1meteor
    • web2: ROOT_URL=http://www.example.com/PORT=3001 MONGO_URL=mongodb://localhost:27017/example2meteor

    -OR- 如果您使用 meteor bundle 版本:

    -OR- if you use the meteor bundle version:

    • web1: ROOT_URL=http://www.example1.com/PORT=3000 MONGO_URL=mongodb://localhost:27017/example1 node bundle/main.js
    • web2: ROOT_URL=http://www.example2.com/PORT=3001 MONGO_URL=mongodb://localhost:27017/example2 node bundle/main.js

    然后您可以直接在同一目录上运行 foreman start(在末尾添加一个 & 以发送到后台).或者您可以通过 foreman export 将其安装为服务/新贵脚本(这可能因其他 linux 发行版而异,请参阅 Foreman 文档:http://ddollar.github.io/foreman/ ):

    You can then run foreman start directly on the same directory (add a & at the end to send to background). or you could install it as a service / upstart script via foreman export (this may vary for other linux distros, please refer to Foreman docs : http://ddollar.github.io/foreman/‎ ):

    • sudo foreman export --appmeteors --user 新贵/etc/init

    准备nginx

    从现在开始,nginx 的配置现在应该非常简单:

    Preparing nginx

    From here on out, the configuration for nginx should now be pretty straightforward:

    server {
      listen 80;
    
      server_name www.example1.com example1.com; 
    
      location / {
        proxy_pass        http://localhost:3000;
        proxy_set_header  X-Real-IP  $remote_addr;
      }
    }
    
    
    server {
      listen 80;
    
      server_name www.example2.com example2.com; 
    
      location / {
        proxy_pass        http://localhost:3001;
        proxy_set_header  X-Real-IP  $remote_addr;
      }
    }
    

    让我知道这是否适合您,尽管您提到您已经使用 SilkJS,但我会将其留在这里给对解决方案感兴趣的其他人.

    Let me know if this works for you, although you mentioned that you already used SilkJS instead, I'll just leave this here for anyone else that's interested on the solution.