
Caddy 2 is a powerful, enterprise-ready, open source web server with automatic HTTPS written in Go.
— 摘自 Caddy 官網
盤古開發(fā)框架 缺省使用 Caddy 來提供 WebServer 能力和 API 網關角色(反向代理)。(當然,直接換成 Nginx 也是可以的)
Caddy 特性
- 缺省啟用HTTP/2 協(xié)議,無需任何配置。
- 缺省全站HTTPS,無需任何配置。(自動申請和續(xù)期證書)
- 簡單友好的配置文件,支持在線配置 API。
- golang 開發(fā),幾乎無依賴,部署簡單。
- 充當 API Gateway, 反向代理后端多個 Web 節(jié)點。
- 天生插件架構,社區(qū)插件豐富,且自定義插件簡單(寫個插件順便蹭一下 golang 的熱度)。
Nginx 特性
盤古開發(fā)框架 缺省使用 Caddy 而不是 Nginx,并不是說 Nginx 不夠優(yōu)秀。
Caddy 實戰(zhàn)
安裝
Fedora, RHEL/CentOS 8
$ dnf install 'dnf-command(copr)'
$ dnf copr enable @caddy/caddy
$ dnf install caddy
RHEL/CentOS 7
$ yum install yum-plugin-copr
$ yum copr enable @caddy/caddy
$ yum install caddy
Debian, Ubuntu, Raspbian
$ sudo apt install -y debian-keyring debian-archive-keyring apt-transport-https
$ curl -1sLf 'https://dl.cloudsmith.io/public/caddy/stable/gpg.key' | sudo tee /etc/apt/trusted.gpg.d/caddy-stable.asc
$ curl -1sLf 'https://dl.cloudsmith.io/public/caddy/stable/debian.deb.txt' | sudo tee /etc/apt/sources.list.d/caddy-stable.list
$ sudo apt update
$ sudo apt install caddy
常用命令
$ systemctl start[stop|restart] caddy
$ systemctl reload caddy //重新加載配置文件
$ systemctl status caddy //查看運行狀態(tài)
實戰(zhàn)一:Web Server
Web Server 托管靜態(tài)資源,以部署盤古開發(fā)框架項目官網為例。https://pangu.pulanit.com
修改配置文件
# vi /etc/caddy/Caddyfile
pangu.pulanit.com {
# Set this path to your site's directory.
root * /var/www/pangu.pulanit.com
encode gzip
# Enable the static file server.
file_server
}
靜態(tài)資源文件上傳到目錄:/var/www/pangu.pulanit.com。
域名解析
域名解析地址必須和上述配置文件中的域名保持一致即可。嘗試訪問:https://pangu.pulanit.com??梢?,網站自動開啟了HTTPS。
實戰(zhàn)二:反向代理
反向代理充當網關角色。詳見官方文檔。