Setting Up Local SSL Certificates for Rails with Caddy


Developing web applications locally with HTTPS can be tricky, but Caddy makes it seamless. Caddy is a modern web server with automatic HTTPS built-in. In this guide, we'll cover how to use Caddy to:
โ
Serve a local Rails app over HTTPS
โ
Use a custom domain name for local development
โ
Set up reverse proxying with automatic SSL certificates
๐ Why Use Caddy for Local SSL?
While you can use tools like mkcert to create local certificates, Caddy simplifies the process by:
Automatically generating and trusting certificates
Handling reverse proxying and HTTPS termination
Requiring minimal configuration with its
Caddyfile
๐ Step 1: Install Caddy
You can install Caddy easily via the instructions on the official website. On macOS with Homebrew:
brew install caddy
๐๏ธ Step 2: Set Up Your Rails Server
Start your Rails server on port 3000 (the default development port):
rails server
Make sure it's running at http://localhost:3000
.
๐ฅ๏ธ Step 3: Configure a Custom Local Domain
To use a custom domain like myapp.local
, you need to modify your hosts file:
Update /etc/hosts
:
sudo nano /etc/hosts
Add the following line:
127.0.0.1 myapp.local
๐๏ธ Step 4: Create a Caddyfile
Create a Caddyfile
in your project root:
touch Caddyfile
Add the following content:
myapp.local {
reverse_proxy localhost:3000
}
๐ Explanation:
myapp.local
: The domain you set up in/etc/hosts
.reverse_proxy localhost:3000
: Proxies HTTPS requests to your Rails server running on port 3000.
โ๏ธ Step 5: Run Caddy
Start Caddy with your configuration:
caddy run
Caddy will automatically issue a local SSL certificate for myapp.local
and serve your Rails app over HTTPS.
๐ Visit https://myapp.local
to see your Rails app secured with HTTPS!
๐ Step 6 (Optional): Trust the Local Certificate
Some browsers or systems might not trust the automatically generated certificate. To trust it system-wide:
bashCopyEditcaddy trust
This command installs the generated root certificate into your system's trust store.
๐๏ธ Alternative: One-Liner Reverse Proxy
If you don't want to use a Caddyfile
, you can run:
caddy reverse-proxy --from myapp.local --to :3000
This achieves the same result but is more suitable for quick tests.
๐งช Testing the Setup
Make sure Rails is running:
rails server
Start Caddy with your Caddyfile:
caddy run
Visit
https://myapp.local
in your browser.
๐ You should now see your Rails app served over HTTPS with no certificate warnings!
๐ Conclusion
Using Caddy for local SSL in your Rails development environment provides a smooth, production-like setup. With automatic HTTPS and reverse proxying, you can focus on development without dealing with the complexities of certificate generation.
Subscribe to my newsletter
Read articles from Tony Duong directly inside your inbox. Subscribe to the newsletter, and don't miss out.
Written by

Tony Duong
Tony Duong
I love writing beautiful code โจ