How to run rails as HTTPS?

Hi all,

Please let me know how to run rails as secure when rails s command execute.

Thanks learning

You can do it in many ways:

Using Ngrok

First, install Ngrok: Ngrok Download

Set up your config file config/environments/development.rb:

Rails.application.configure do
  config.hosts << ".ngrok.io" # <-- Add this line
  # ...
end

Then run bin/rails s in a terminal.

After that, open another terminal and run ngrok http 3000 you will see something like this output:

Tunnel Status                 online
Version                       2.0/2.0
Web Interface                 http://127.0.0.1:4040
Forwarding                    http://92832de0.ngrok.io -> localhost:3000
Forwarding                    https://92832de0.ngrok.io -> localhost:3000

Connnections                  ttl     opn     rt1     rt5     p50     p90
                              0       0       0.00    0.00    0.00    0.00

In this case now you can access to your webapp with this URL https://92832de0.ngrok.io, Ngrok will give another address though, That’s it.

Using Puma-dev

If you mind to use an external service you can also install this awesome tool: puma-dev.

hope it helps.

1 Like

Very helpful, thank you

Is Nginx also same way to handle this?

Very helpful, thank you

I will take it as if you don’t know where to start. Most Rails applications are run behind a reverse proxy like NGINX. This reverse proxy might also run behind a load balancer.

Now the SSL layer might be implemented as if going from the client (browser) to your load balancer or reverse proxy (both have different implications). Then the reverse proxy communicates with an application server like Puma running Rails without an SSL layer since there are (likely) on the same host.

At least this is the most common production setup. You could expose Rails directly, but NGINX protects you from slow clients and serves static assets really fast.

So the question becomes a different one. How to implement an SSL layer in your web server (like NGINX) or load balancer (can be NGINX as well)? There is a lot of resources online for that. In the case of NGINX it could look like:

server {
  listen 443 ssl;
  server_name deploymentfromscratch.com;

  ssl_certificate /path/to/cert.crt;
  ssl_certificate_key /path/to/privkey.key;
}

There is more to this (secure headers and ciphers), but the above should work as a minimum.

To get the certificates you can use Let’s Encrypt and their cerbot tool or – if this is a private project – self-sign a certificate.

It’s a lot of moving parts for one comment. If you have more specific question, I am happy to help you. I am also just finishing a book on Rails application deployment where I talk about all of this in detail (and many other useful things!).

5 Likes

For myself, years ago, Rails was really frustrating to learn how to deploy to your own infrastructure. All of the tutorials, beginner and advanced simply say “deploy to Heroku”, or, make it someone else’s problem. It’s been a sore spot for myself, and others I’ve taught to. The fact is, lots of apps don’t make sense to deploy publicly for a variety of reasons. A toy app that is for myself, or, an internal app that uses internal resources. There’s a handful of concepts that aren’t obvious if you’ve done things like PHP or .net in the past. Thanks for working on that book— I’m a big fan of every effort that tries to teach people how to run their own stuff. While “it depends” is understandably the usual response, there’s not a lot of effort to help people navigate the “it depends” on their own.

I’m just curious, any potential for a physical release of that book by chance? I want it. And I want it on my bookshelf. :wink:

3 Likes

Thanks for that! I absolutely want to hold that book at some point in my hands! But to be honest, it might be a while for a printed version, because I really want to polish it and then maybe rethink some parts for print (not sure if it’s not too big now).

As for the general progress, I am finishing the Rails demo (git-push deploy, systems socket activation for graceful restarts, railsc.sh to drop you to Rails console, Action Cable, self-sign certs for your IP address until you have a domain then Let’s Encrypt,…a lot of stuff!), then I will continue to finish chapters on containers.

2 Likes

Hello, Okay so here’s what I found on how to run Rails via HTTPS. According to pluralsight (I have tested and it works), you need to follow three steps as follows: a) obtaining SSL certificate, b) configuring the web server, c) configuring ruby on rails for https. Each of these steps are broken down. Regards, EduHelpHub