Hack: Naming Hosts and Ports with Nginx

At Cribspot, we had a bunch of different ember apps and one single rails API that fed all of them. Here is a breakdown of all of our apps:

  • manage
  • home
  • owner
  • search

They were all separate projects in different repos. Ember with ember server would run on port 4200 for every project.

We started by adding to the config for each app so that they would not conflict. We had:

  • api: port 3000
  • manage: port 3001
  • home: port 3002
  • owner: port 3003
  • search: port 3004

That worked well but it was hard to remember which port for each app. So let’s make it easier with Nginx.

Open up your sites-enabled/default.conf and add this:

server {
    listen 80;
    server_name api;
    location / {
        proxy_pass http://127.0.0.1:3000;
    }
}

server {
    listen 80;
    server_name manage;
    location / {
        proxy_pass http://127.0.0.1:3001;
    }
}

Also, make sure to add to your /etc/hosts:

127.0.0.1 api
127.0.0.1 manage
127.0.0.1 home
127.0.0.1 owner
127.0.0.1 search

Now you can visit, http://home/ or http://manage/ or whatever and life is easier!