Using Meteor Up with NginX vhosts

Fabian Vilers edited this page Jul 24, 2015 · 21 revisions

Setup of NginX vhost for Meteor

Using Meteor Up to deploy your app.

  • So why do we want to use NginX?
  • Well, just because vhosts is difficult or Idk impossible with just plain node.js if you want to run multiple apps on the same server using the same port, say port 80.
  • But how does it this work?
  • What we're doing with NginX is that we're setting up a proxy to the node.js app that is running somewhere else on the server on some port. NginX is the first to receive all calls to the server and from there decides where to direct the traffic. One can also use this method to improve the way we deliver static content for our meteor app, but for that we need some more configuration... I'll leave that to later. For now this is the best way to host multiple apps on the same server.
  • I should also mention that this is also possible to do using Apache, but you will not get your websockets working using Apache as they don't have a good way to proxy them. So use NginX for now. :)

Configuration for an NginX vhost

/etc/nginx/sites-available/mycustomappname.com.conf
server {
  listen                *:80;

  server_name           mycustomappname.com;

  access_log            /var/log/nginx/app.dev.access.log;
  error_log             /var/log/nginx/app.dev.error.log;

  location / {
    proxy_pass http://127.0.0.1:3000;
    proxy_http_version 1.1;
    proxy_set_header Upgrade $http_upgrade;
    proxy_set_header Connection 'upgrade';
    proxy_set_header X-Forwarded-For $remote_addr;
  }
}

Enable your site by adding a symbolic link to your configuration in sites-enabled.

ln -s /etc/nginx/sites-available/mycustomappname.com.conf /etc/nginx/sites-enabled/mycustomappname.com.conf

mup.json environment settings

"env": {
  "PORT": "3000", // The port you want to bind to on your server.
  "UPSTART_UID": "meteoruser", // The user you want to run meteor as.
  "ROOT_URL": "http://mycustomappname.com"
},

If you don't provide UPSTART_UID mup will try to start your app with root permission.
Then eventually, mup will downgraded your app into a non privileged user (meteoruser)
If you want to start your app with a non privileged user by default use UPSTART_UID env variable

Note1: meteoruser user is already created by mup
Note2: You can't use UPSTART_UID if you are binding to a port lower than 1000

see more discussion on this topic.

Setup Node.js, MongoDB, upstart etc for your app

mup setup

Deploy your app

mup deploy

Dynamic vhost? is it possible?

  • Anyone? I'd love this feature!

Here are some ideas

(RFC) Configuration for Dynamic NginX vhosts

What's missing is a way to map domain names with application port bindings... in the following example. ... I guess this could be mapped in Redis. But we need a way to add all mappings to Redis during setup in this case. - Like consul-template?

/etc/nginx/sites-enabled/dynamic.conf
server {
  listen                *:80;

  server_name           ~^(www\.)?(?<domain>.+)$;

  access_log            /var/log/nginx/app.dev.access.log;
  error_log             /var/log/nginx/app.dev.error.log;

  location / {
    proxy_pass http://127.0.0.1:XXXXX;
    proxy_http_version 1.1;
    proxy_set_header Upgrade $http_upgrade;
    proxy_set_header Connection 'upgrade';
    proxy_set_header X-Forwarded-For $remote_addr;
  }
}