After a long time using Nginx and OpenLiteSpeed, recently I’ve been gradually switching to using Caddy as a web server for websites and a reverse proxy for network services in my homelab at home.
Compared to Nginx and Openlitesped, Caddy is much easier to install and use. Just configure 3 command lines similar to the one below to set up a reverse proxy, and automatically set up SSL from Let’s Encrypt.
webserver.markknow.com {
reverse_proxy 192.168.0.50:9000
}
Code language: YAML (yaml)
Today’s article I will quickly introduce Caddy Server and how to install and use it.
1. Introduction to Caddy Server
Caddy Server is an open source web server software developed based on the Golang (Go) programming language. Born in 2015, Caddy has been continuously added with many powerful features, the most notable of which are HTTP / 3.0 support and automatic authentication of Let’s Encrypt SSL certificates.
Caddy Features:
- Easy configuration customization with Caddyfile
- Advanced configuration customization with native JSON config
- Automatic HTTPS authentication
- Multi-feature: web server, reverse proxy, load balancer
- Production-ready
- Hỗ trợ HTTP/1.1, HTTP/2, and HTTP/3
- Easily extend functionality thanks to modular architecture
- Supports multiple operating systems, and is not dependent on dependencies
- And many more features waiting for you to discover
2. Install Caddy using Docker
You can install Caddy in many ways through the instructions here.
I chose to install using Docker so that I can easily remove it when needed without affecting the system.
Create filecompose.yml
mkdir ~/caddy
cd ~/caddy
nano compose.yml
Code language: Bash (bash)
Enter the following content
services:
caddy:
image: caddy:alpine
container_name: caddy
restart: unless-stopped
ports:
- "80:80"
- "443:443"
- "443:443/udp"
volumes:
- ./Caddyfile:/etc/caddy/Caddyfile
- ./html:/var/www/html
- ./caddy_data:/data
- ./caddy_config:/config
Code language: Markdown (markdown)
Create more filesCaddyfile
nano Caddyfile
Code language: Nginx (nginx)
and temporarily enter the content
:80 {
respond "Hello, world!"
}
Code language: JavaScript (javascript)
Activate Caddy
docker compose up -d
Code language: Nginx (nginx)
Open the browser and access the address http://<IP>
. If you see the text, Hello, world!
it means Caddy has been successfully installed and activated.
3. Configure Caddy as Reverse Proxy
To configure Caddy as a Reverse Proxy for Portainer running at the address 192.168.0.50:9000
, I edit the Caddyfile file and add the following 3 lines:
portainer.markknow.com {
reverse_proxy 192.168.0.50:9000
}
Code language: YAML (yaml)
Then restart Caddy with the command
docker compose exec -w /etc/caddy caddy caddy reload
Code language: Bash (bash)
Wait 1-2 minutes for Caddy to complete the SSL certificate authentication step, now I can access Portainer via the addresshttps://portainer.markknow.com
In case Portainer and caddy are on the same server, you can refer to the article below to know how to set up containers on the same docker network and edit Caddyfile as follows
portainer.markknow.com {
reverse_proxy portainer
}
I have also moved this markknow.com website to run on Docker and Reverse Proxy through Caddy. When checking, curl -I https://markknow.com
the information will appearserver: Caddy
HTTP/2 200
accept-ranges: bytes
alt-svc: h3=":443"; ma=2592000
content-type: text/html; charset=utf
etag: "sa2ao42enp"
last-modified: Sat, 09 Mar 2024 03:50:28 GMT
server: Caddy
x-content-type-options: nosniff
x-frame-options: SAMEORIGIN
content-length: 112309
date: Sat, 09 Mar 2024 03:58:40 GMT
Code language: YAML (yaml)
In the following article, I will guide you how to set up a WordPress website running on Docker and Caddy.
Comment Policy: We truly value your comments and appreciate the time you take to share your thoughts and feedback with us.
Note: Comments that are identified as spam or purely promotional will be removed.
To enhance your commenting experience, consider creating a Gravatar account. By adding an avatar and using the same e-mail here, your comments will feature a unique and recognizable avatar, making it easier for other members to identify you.
Please use a valid e-mail address so you can receive notifications when your comments receive replies.