How can Meteor handle multiple Virtual Hosts?

泪湿孤枕 提交于 2019-12-04 10:58:04

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

The basic idea

.. 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:

  • Site #1 : www.example1.com
    • Meteor port: 3000
    • MongoDB endpoint/url: mongodb://localhost:27017/example1
  • Site #2 : www.example2.com
    • Meteor port: 3001
    • MongoDB endpoint/url: mongodb://localhost:27017/example2

Preparing the meteor instances

  1. Install foreman via rubygems:

  2. 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/example1 meteor
    • web2: ROOT_URL=http://www.example.com/ PORT=3001 MONGO_URL=mongodb://localhost:27017/example2 meteor
  3. -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
  4. 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 --app meteors --user <meteor files owner> upstart /etc/init

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

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.

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!