SSL for local Rails development

My goal was to test jch.app serviceworkers with different devices on the same network. While localhost is an exception allowed for serviceworkers, all other origins require a no-warning https connection. This meant the certificate must be signed with a system trusted CA.

Fortunately, mkcert does exactly that. Some additional fiddling was needed to configure puma with command line options to reference the certs and listen for SSL connections. No additional gems, or configuration changes were necessary. Tested on macOS 15.6.1, puma 6.6.0, mkcert 1.4.4, and rails 8.0.2.

# Run from rails root
# Create locally trusted certificate https://github.com/FiloSottile/mkcert
$ mkcert -install

# Used `sudo scutil --set LocalHostName` to set local hostname to `roboplan.local`
$ mkcert roboplan.local "*.roboplan.local" roboplan.local localhost 127.0.0.1 ::1

# Rename to avoid shell escaping later
$ mkdir -p config/certs
$ mv roboplan.local+5-key.pem config/certs/roboplan.local-key.pem
$ mv roboplan.local+5.pem config/certs/roboplan.local.pem

# Added in bin/dev
$ bin/rails server -b 'ssl://0.0.0.0?key=config/certs/roboplan.local-key.pem&cert=config/certs/roboplan.local.pem'

Details

Puma and Falcon support self-signed certificates with localhost gem, but the defaults did not add a system trusted CA causing certificate warnings that made serviceworkers unavailable.

Additional notes:

Service workers are only available in secure contexts: this means that their document is served over HTTPS, although browsers also treat http://localhost as a secure context, to facilitate local development. MDN Service Worker API

Sources