Configuring HTTPS for Local Development
Why HTTPS?
Service workers require a secure context (https://
) to function.
While Rails applications typically use http://localhost:3000
, we need HTTPS for PWA development.
Step 1: Generate a Self-Signed SSL Certificate
Run the following command to generate a local SSL certificate:
mkdir -p config/ssl
openssl req -x509 -newkey rsa:2048 -keyout config/ssl/key.pem -out config/ssl/cert.pem -days 365 -nodes -subj "/CN=localhost"
This generates a key and certificate valid for one year.
Step 2: Configure Rails to Use HTTPS
Modify config/puma.rb
to use the certificate:
ssl_bind '127.0.0.1', '3001', {
key: "config/ssl/key.pem",
cert: "config/ssl/cert.pem"
}
Start the server with:
bin/rails s -b 'ssl://127.0.0.1:3001?key=config/ssl/key.pem&cert=config/ssl/cert.pem'
Now, your local Rails app runs at https://127.0.0.1:3001
with HTTPS enabled.