Skip to content

Instantly share code, notes, and snippets.

@wiseleyb
Last active February 23, 2016 16:37
Show Gist options
  • Save wiseleyb/a2f78b90c6009e0bab53 to your computer and use it in GitHub Desktop.
Save wiseleyb/a2f78b90c6009e0bab53 to your computer and use it in GitHub Desktop.
Setup a Rails, NGINX, HTTPS dev box

Setup a Rails, NGINX, HTTPS dev box

I was implementing https://layer.com/ on https://zeemee.com and they require https for the callbacks. Since this was new to me and took me a while here's a how to if anyone is looking.

Assumptions

You already have a rails app running on localhost:3000

Static IP Address

You're going to need to get a static IP address for your computer. My ISP is https://sonic.net. They give you one free static IP with your account. In any case - you'll need an IP address and you'll need to configure it.

If you're also on Sonic this is how:

Test it If you've done everything right you should be able to now hit your Rails app at http://your-ip-address:3000

Gotchas

Does your wifi router have a firewall? Mine does. I had to open up ports on my home wifi router. You'll want to open port 80, 443 and 3000. Most wifi routers are available at http://192.168.42.1/

Also - is your Mac's Firewall running? You can turn it off at System Preferences > Security & Privacy > Firewall

Register a domain name

Simple - just go to some place like https://www.namecheap.com/ and register a domain name.

You'll then need to add 2 A records. On Namecheap this is pretty simple. You need to add @ and www that both point to your IP. Remove any A records pointing at placeholders.

Test it If you've done everything right you should be able to hit your app at http://your-domain:3000

NGINX

I used Homebrew to setup NGINX. It's easy peasy:

brew install NGINX
sudo nginx

Test it You should now be able to go to http://localhost:8080 and see a welcome to NGINX message

Change port to :80

vim /usr/local/etc/nginx/nginx.conf

Find

  server {
  	listen 8080;

And change 8080 to 80

Test it

sudo nginx -s stop
sudo nginx

You should now be able to see the NGINX welcome message by going to http://localhost

Point NGINX at your Rails app

vim /usr/local/etc/nginx/nginx.conf

Change location to:

location / {
          proxy_pass http://localhost:3000;
          proxy_set_header Host $host;
          proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
        }

Test it

sudo nginx -s stop
sudo nginx

You should now see you Rails app when you hit http://localhost

HTTPS

This stuff is pretty confusing and a bitch to debug but, this is how I got it working.

Can you email your domain?

You'll need to setup email on your new domain. On Namecheap this is pretty trivial. Add either a wildcard or a admin@your-domain.com. https://www.namecheap.com/support/knowledgebase/article.aspx/308/76/how-can-i-set-up-free-email-forwarding-for-my-domain. Make sure this works... you'll need to be able to get email at admin@your-domain.com

Buy a HTTPS certificate

This is cheap ($10). Again - I used Namecheap to get this https://www.namecheap.com/security/ssl-certificates.aspx

Create a CSR

When you go to setup your HTTPS cert they're going to ask you for a CSR. You can generate this on your Mac in terminal:

openssl x509 -req -days 365 -in domain.csr -signkey domain.key -out domain.crt

Replace domain with your domain name (not required, just easier)

I like to keep all this ssl nonsense in ~/.ssl but, it doesn't really matter.

Make sure, when asked for 'Common Name' that you enter your new domain (my-domain.com)

The your-domain.csr contents is what you'll need when you setup your HTTPS cert.

Once you've done that you'll recieve a verification email from the cert provider. And, after verifying, they'll send you a cert. Mine had a zip file which I extracted into ~/.ssl

Configure NGINX for HTTPS

vim /usr/local/etc/nginx/nginx.conf

Add another server in NGINX (should be commented out already) and change location to something like this:

    # HTTPS server
    #
    server {
        listen       443 ssl;
        server_name  wiseleyb.com;

        ssl_certificate      /Users/wiseleyb/.ssl/wiseleyb_com/wiseleyb_com.crt;
        ssl_certificate_key  /Users/wiseleyb/.ssl/wiseleyb.key;

        ssl_session_cache    shared:SSL:1m;
        ssl_session_timeout  5m;

        ssl_ciphers  HIGH:!aNULL:!MD5;
        ssl_prefer_server_ciphers  on;

        location / {
          proxy_pass http://localhost:3000;
          proxy_set_header Host $host;
          proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
        }
    }

Test it

sudo nginx -s stop
sudo nginx

You should now be able to go to https://your-domain.com and see you app!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment