Mastering Thruster for Ruby on Rails: A Deep Dive for Beginners
Table of contents
- Introduction to Thruster
- The Need for Thruster in Rails Applications
- Features of Thruster
- Setting Up Thruster in a Rails Project
- Serving Static Files with Thruster
- Using Thruster for TLS Certificate Management
- Integrating Thruster with Puma in a Container
- Benefits of Using Thruster
- Conclusion
- Comparing Thruster and Caddy
Deploying Ruby on Rails applications can be challenging for new developers, especially when it comes to managing server configurations, handling security with TLS certificates, and optimizing performance.
Typically, setting up a production-ready environment requires combining multiple tools, like a web server, proxy server, and SSL manager, which adds complexity.
Thruster, a gem developed by 37signals, aims to simplify this process by integrating essential features like HTTP/2 support, automatic TLS management, and file handling, all while being as "zero-config" as possible.
Introduction to Thruster
Thruster is a Ruby gem designed by 37signals to simplify the deployment of Ruby on Rails applications, particularly for developers who are new to deployment environments and production readiness.
It acts as an HTTP/2 proxy that runs alongside Puma (a popular web server for Rails) and enhances several key areas of application performance, security, and simplicity of deployment.
Thruster allows you to:
Improve performance with HTTP/2, enabling faster load times.
Secure your connections using automatic TLS certificates from Let’s Encrypt.
Manage basic caching of public assets to improve repeat visit performance.
Efficiently serve static files by supporting features like X-Sendfile and file compression.
This article will break down each of these features, explain why they are useful, and show practical examples of how to use them.
The Need for Thruster in Rails Applications
Rails applications often require multiple tools and servers to handle various aspects of a production setup, such as:
An application server (e.g., Puma)
A reverse proxy server (e.g., Nginx) for handling incoming requests more efficiently
A TLS certificate manager for security (e.g., Let's Encrypt Certbot)
Setting up and managing these components can be overwhelming, especially for new developers.
Thruster integrates all of these features, reducing the complexity into a single tool that "wraps around" Puma and helps make a Rails application production-ready without having to worry about the individual services.
It provides a powerful way to create a stable and efficient deployment with minimal configuration.
Features of Thruster
HTTP/2 Support
HTTP/2 is a newer protocol for the web that allows browsers to download all the different parts of a webpage, like images, scripts, and stylesheets, at the same time using a single connection. This is different from HTTP/1.1, which required separate requests for each resource, leading to more waiting time and slower page loading.
Using HTTP/2 through Thruster results in:
Lower latency: Faster delivery of assets and page loads, especially for pages with lots of elements.
Reduced overhead: Fewer repeated connections, which is particularly helpful for servers with limited resources.
How does Thruster provide HTTP/2 support? Thruster, when running alongside Puma, automatically acts as an HTTP/2 reverse proxy. This means that your Rails app can benefit from the faster protocol without additional server-level configuration.
Automatic TLS Certificate Management
TLS (Transport Layer Security) is what provides secure "https" connections. It encrypts the data transferred between your server and users, protecting it from potential attackers. Setting up and renewing TLS certificates manually can be time-consuming and requires domain verification steps.
Thruster automates this process by using Let’s Encrypt, a free certificate authority:
Thruster manages the entire lifecycle of the certificate, including the provisioning, renewal, and revocation.
All you need to do is set an environment variable (
TLS_DOMAIN
), and Thruster will take care of obtaining and managing the TLS certificate for your domain.
HTTP Caching of Public Assets
Caching is a technique used to store copies of files temporarily to make them more readily available on repeat visits. For example, once a user downloads an image from your website, caching allows the browser to store that image locally instead of fetching it from the server each time.
Thruster helps improve performance by:
Enabling basic HTTP caching of public assets, like stylesheets, JavaScript, and images.
Reducing the load on your server and speeding up loading times for returning visitors.
X-Sendfile Support and File Compression
The process of delivering large files can be resource-intensive. X-Sendfile is a feature that allows Puma to offload the task of sending static files (like PDFs or images) directly to the operating system, which is much more efficient.
Additionally, Thruster can compress files before sending them to clients, which reduces the size of files, making them quicker to transmit and improving overall page performance.
Setting Up Thruster in a Rails Project
To start using Thruster in your Rails project, follow these steps:
Add the gem to your project by adding it to your Gemfile:
gem 'thruster'
Install the gem using the following command:
bundle install
Run Thruster:
thruster start
To enable HTTPS, set the TLS_DOMAIN
environment variable:
export TLS_DOMAIN=yourdomain.com
This automatically provisions and renews TLS certificates for your app.
Serving Static Files with Thruster
In this example, let’s explore how Thruster handles static files more efficiently:
# config/application.rb
# Make sure public files are accessible
config.public_file_server.enabled = ENV['RAILS_SERVE_STATIC_FILES'].present?
# Ensure that assets are compressed
config.assets.compress = true
Here, static files in the public
directory (such as images, JavaScript, and CSS) are served directly by Thruster using X-Sendfile, meaning that large files are delivered efficiently. Thruster also compresses the files, improving the speed of their transfer.
Using Thruster for TLS Certificate Management
Secure your Rails app using TLS certificates from Let’s Encrypt:
# Set the TLS domain in your environment
export TLS_DOMAIN="myapp.example.com"
# Start Thruster, which automatically handles Let's Encrypt provisioning
thruster start
When you set the TLS_DOMAIN
, Thruster automatically takes care of getting an HTTPS certificate from Let’s Encrypt and renewing it as needed. This ensures that your website has secure connections at all times without manual intervention.
Integrating Thruster with Puma in a Container
This example shows how to integrate Thruster with Puma in a Docker container for simplified deployment:
# Dockerfile
FROM ruby:3.0
WORKDIR /app
COPY . .
# Install dependencies
RUN bundle install
# Set the environment variable for TLS
ENV TLS_DOMAIN=myapp.example.com
# Use Thruster to manage Puma
CMD ["thruster"]
In this Dockerfile:
Thruster serves as the command (
CMD
) that runs when the container starts.Thruster wraps around the Puma server, eliminating the need for a separate process manager, which is useful when deploying in a containerized environment.
Benefits of Using Thruster
Simplicity: Thruster is designed to be easy to use, without complex configuration files. It automatically applies sensible defaults, ensuring that your app runs efficiently.
Security: Automatic TLS management means you don't have to worry about certificates, while also providing secure connections for your users.
Performance: With HTTP/2 and efficient file handling, Thruster significantly improves the speed and performance of Rails applications.
Reduced Complexity: Managing both HTTP/2 proxying and the Puma process in one place makes it easier to deploy and manage applications, especially in Docker containers.
Conclusion
Thruster is an ideal solution for new developers looking to deploy Ruby on Rails applications efficiently, securely, and with minimal configuration. By integrating critical features like HTTP/2, automated TLS, file caching, and Puma management, Thruster simplifies what is typically a complex production environment.
In this article, we explored practical examples of how Thruster can be used for serving static files efficiently, managing TLS certificates, and deploying a Rails application using Docker. These examples provide insight into how you might use Thruster to simplify your Rails deployment setup.
Thruster's goal is to make Rails deployment accessible and worry-free, so you can focus on building your application instead of handling infrastructure.
Comparing Thruster and Caddy
When considering web servers for Ruby on Rails applications, both Thruster and Caddy offer valuable features, but they cater to different use cases:
Caddy is a powerful web server that excels in serving various applications, providing automatic HTTPS, and handling complex routing rules. It's flexible and well-suited for diverse deployment scenarios, including static sites and microservices.
Thruster, on the other hand, is tailored specifically for Ruby on Rails applications. It seamlessly integrates with Puma, offers automatic TLS certificate management, and provides built-in support for HTTP/2, making it easier for Rails developers to set up and deploy their apps without additional overhead.
Why Choose Thruster over Caddy for Ruby on Rails Applications
Seamless Integration: Thruster is specifically designed to work with the Puma web server, offering a streamlined setup for Rails applications without the need for complex configurations, unlike Caddy, which serves a broader range of applications.
Built-in TLS Management: Thruster automatically manages TLS certificates via Let’s Encrypt, reducing the risk of human error and ensuring secure connections without manual intervention.
Performance Optimization: Thruster’s features, like HTTP/2 support and static file handling, are optimized for Rails, leading to better performance tailored specifically for Rails applications compared to the more generic approach of Caddy.
By choosing Thruster, Ruby on Rails developers can focus on building their applications while enjoying a streamlined deployment process.
Subscribe to my newsletter
Read articles from BestWeb Ventures directly inside your inbox. Subscribe to the newsletter, and don't miss out.
Written by
BestWeb Ventures
BestWeb Ventures
From cutting-edge web and app development, using Ruby on Rails, since 2005 to data-driven digital marketing strategies, we build, operate, and scale your MVP (Minimum Viable Product) for sustainable growth in today's competitive landscape.