How to run the puma server with SSL?

1

My Rails 5 application uses WebRTC, and WebRTC (camera, etc.) only works with SSL enabled. I use puma server locally.

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

    
asked by anonymous 03.01.2017 / 11:34

1 answer

1

By default Rails does not provide this feature, you must configure it, generate SSL certificates. Below I detail the procedure.

Self-Signed SSL Certificate

Let's generate the self-signed certificates, I'll put them in the application so that others on the team can benefit from this configuration. Create a certificates directory within the config folder in your application and run the following commands:

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 from from this gist .

Puma

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

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 in Rails. Open the file development.rb and add config.force_ssl = true .

Unfortunately, using the rails server command, you can not start the server with SSL, this happens because rails server does not read the puma.rb settings. Take a look at this issue to better understand the problem. To start the server use the command puma .

Once this is done, your server will run using SSL.

Logger

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

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

logger           = ActiveSupport::Logger.new(STDOUT)
logger.formatter = config.log_formatter
config.logger    = ActiveSupport::TaggedLogging.new(logger)
    
03.01.2017 / 11:49