Caddy VS Nginx,谁领风骚

Caddy 2 is a powerful, enterprise-ready, open source web server with automatic HTTPS written in Go.

— 摘自 Caddy 官网

盘古开发框架 缺省使用 Caddy 来提供 WebServer 能力和 API 网关角色(反向代理)。(当然,直接换成 Nginx 也是可以的)

Caddy 特性

  • 缺省启用HTTP/2 协议,无需任何配置。
  • 缺省全站HTTPS,无需任何配置。(自动申请和续期证书)
  • 简单友好的配置文件,支持在线配置 API。
  • golang 开发,几乎无依赖,部署简单。
  • 充当 API Gateway, 反向代理后端多个 Web 节点。
  • 天生插件架构,社区插件丰富,且自定义插件简单(写个插件顺便蹭一下 golang 的热度)。

Nginx 特性

盘古开发框架 缺省使用 Caddy 而不是 Nginx,并不是说 Nginx 不够优秀。

Caddy 实战

安装

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 //查看运行状态

实战一:Web Server

Web Server 托管静态资源,以部署盘古开发框架项目官网为例。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
}

静态资源文件上传到目录:/var/www/pangu.pulanit.com

域名解析

域名解析地址必须和上述配置文件中的域名保持一致即可。尝试访问:https://pangu.pulanit.com。可见,网站自动开启了HTTPS

实战二:反向代理

反向代理充当网关角色。详见官方文档

你可能感兴趣的:(Caddy VS Nginx,谁领风骚)