The basic solution for local domains

Romain Champourlier
JobTeaser Engineering
2 min readMay 3, 2016

As you can see in my answer on this StackOverflow question, I have already tried hard to setup an easy-to-work-with development environment, using local domains (e.g. my-project.dev), all this with HTTPS support.

Some good time with Invoker

Lately I was fond of Invoker which worked pretty well for me (with my pull request), handling all of my current requirements.

…but not anymore

Until some days ago. It stopped working. Uninstalled it, reinstalled it, nothing changed. I think it may be related with my install of Docker Native for Mac, which may have tempered with my firewall settings, something Invoker seems to be using too.

Tried and left Hotel

I looked into another solution, Hotel, which is nice but handles the service loading. Which I don’t want, because I generally need to run the service in a terminal to debug with breakpoints. Because I’m a developer.

Returned to the basics

I almost started a new project to write another (yes, another) solution, but I decided it was not worth it. I don’t need to use and update local domains so often that it requires something so smooth as Pow, Invoker, or Hotel. So I just did it with the basics: nginx and /etc/hosts.

In short

(For MacOS X users only, sorry other OSes)

Install brew

brew install nginx

Edit /usr/local/etc/nginx to set your server locations

server {
listen 80;
server_name *.my-project.dev;
location / {
proxy_pass http://localhost:9292;
proxy_set_header Host $host;
proxy_set_header X-Forwarded-For $remote_addr;
proxy_set_header X-Forwarded-Proto http;
}
}
server {
listen 443 ssl;
server_name localhost;
ssl_certificate server.crt;
ssl_certificate_key server.key;
location / {
proxy_pass http://localhost:9292;
proxy_set_header Host $host;
proxy_set_header X-Forwarded-For $remote_addr;
proxy_set_header X-Forwarded-Proto https;
}
}

Bonus: here’s a config file for Phoenix, including websocket proxying for live-reload.

Create a self-signed certificate

You can follow this excellent guide from Heroku, but if you’re in a hurry:

cd /usr/local/etc/nginx/openssl genrsa -des3 -passout pass:x -out server.pass.key 2048
openssl rsa -passin pass:x -in server.pass.key -out server.key
rm server.pass.key
openssl req -new -key server.key -out server.csr
openssl x509 -req -days 365 -in server.csr -signkey server.key -out server.crt

Finally, edit your /etc/hosts

For example (matching the nginx conf above):

# local domains
127.0.0.1 www.my-project.dev

Bonus: aliases to start/stop nginx

(Kind of hacky, but that’s a fast’n’dirty solution anyway)

alias nginx-start=”sudo nginx”alias nginx-status=”ps auxw|grep ‘nginx: master’|grep -v grep”alias nginx-pid=”ps auxw|grep \”nginx: master\”|grep -v grep|awk ‘{print \$2}’”alias nginx-stop=”ps auxw|grep \”nginx: master\”|grep -v grep|awk ‘{print \$2}’|xargs sudo kill”

Enjoy!

--

--

Romain Champourlier
JobTeaser Engineering

Open-minded and empathic (hopefully) human, trying to reduce GHG emissions with software. Previously CTO/VPEng @ JobTeaser (French HR-tech scale-up)