Skip to content

Instantly share code, notes, and snippets.

@bgvo
Last active August 4, 2021 06:15
Show Gist options
  • Save bgvo/34ed30f808c50e11b83bc42cfca7863a to your computer and use it in GitHub Desktop.
Save bgvo/34ed30f808c50e11b83bc42cfca7863a to your computer and use it in GitHub Desktop.
Enable local SSL connections using Puma with Rails

Enable local SSL connections using Puma with Rails

Original source here.

Content

1. Install mkcert

https://github.com/FiloSottile/mkcert

2. Create a certificate

So the first thing you will need to do is to create a certificate for your service you want to add the HTTPS connection. Let's use mkcert to generate it, i'm going to localhost, but you can use whatever name you want

➜  ~ mkcert localhost

For Mac users open the Keychain Access and drag the certificate to the Keychain. For the localhost.pem file you could see some error, but it will end up adding it.

image

Double-click the cert you just added, click the -> Trust

image image

And at the When using this certificate: select the Always Trust option.

image image

3. Edit puma and .env config files

Add the following snippet to config/puma.rb:

if ENV['RACK_ENV'] == 'development'
  localhost_key = "#{File.join('config', 'local-certs', 'localhost-key.pem')}"
  localhost_crt = "#{File.join('config', 'local-certs', 'localhost.pem')}"
  # To be able to use rake etc
  ssl_bind '0.0.0.0', 3000, {
    key: localhost_key,
    cert: localhost_crt,
    verify_mode: 'none'
  }
end

In .env file, set RAILS_HOST=localhost:3001

Now, when you run puma with rails s, you should see how it listening for ssl requests:

=> Booting Puma
=> Rails 6.1.3.1 application starting in development
=> Run `bin/rails server --help` for more startup options
Puma starting in single mode...
* Puma version: 5.2.2 (ruby 3.0.0-p0) ("Fettisdagsbulle")
*  Min threads: 5
*  Max threads: 5
*  Environment: development
*          PID: 1443
* Listening on http://127.0.0.1:3000
* Listening on http://[::1]:3000
* Listening on ssl://0.0.0.0:3001?cert=/Users/borjagvo/Work/templarbit/app-v2/localhost.pem&key=/Users/borjagvo/Work/templarbit/app-v2/localhost-key.pem&verify_mode=none
Use Ctrl-C to stop
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment