How to run the puma server with SSL in development?

Asked

Viewed 401 times

2

My Rails 5 application uses Webrtc, and Webrtc (camera, etc) only works with SSL enabled. I use a local SSL server.

What are the correct procedures to implement and enable SSL support on the puma server?

1 answer

2


By default Rails does not offer this feature, it is necessary to be configured, generate SSL certificates. Below detail the procedure.

Self-signed SSL Certificate

We will generate the signed certificates, I will put in the application so that others on the team benefit from this setting. Create a directory certificates inside the briefcase config in your application and run the commands below:

cd config/certificates

openssl genrsa -des3 -out server.orig.key 2048
openssl rsa -in server.orig.key -out server.key
openssl req -new -key server.key -out server.csr
openssl x509 -req -days 365 -in server.csr -signkey server.key -out server.crt

These instructions were taken of this gist.

Cougar

Open the file puma.rb and add to the end the directive bind with their respective settings:

ssl_key = File.expand_path('../certificates/server.key', __FILE__)
ssl_cert = File.expand_path('../certificates/server.crt', __FILE__)
bind "ssl://127.0.0.1:3000?key=#{ssl_key}&cert=#{ssl_cert}"

Remember to force the SSL connection on Rails. Open the file development.rb and add config.force_ssl = true.

Unfortunately through the command rails server cannot start the server with SSL, this happens because the rails server does not read file settings puma.rb. Take a look at this question to better understand the problem. To start the server use the command puma.

Done this your server will run using SSL.

Logger

Running the server through the command puma will not bring the application logs. To resolve this, open the file development.rb and add the instructions below:

config.log_formatter = ::Logger::Formatter.new

logger           = ActiveSupport::Logger.new(STDOUT)
logger.formatter = config.log_formatter
config.logger    = ActiveSupport::TaggedLogging.new(logger)
  • congratulations, I’m on my way, but would you have the full tutorial to enable https on my Rails api? thanks Bruno!

  • I haven’t, I haven’t played with Rails in a few years.

Browser other questions tagged

You are not signed in. Login or sign up in order to post.