Caddy是一个由Go语言编写的Web服务器软件,可以自动实现HTTPS部署。

为什么使用Caddy

自己的博客想要使用https,懒得去更新证书,Caddy可以快速解决我的问题。

而且相比nginxCaddy部署起来更简单,就一个二进制文件;

Caddyfile配置更简单,很多都是默认配置,只要根据自己的情况,添加需要的参数即可。

功能丰富,但是很多我暂时用不到,用到再通过插件的方式添加即可。

对于一个博客应用来说完全够用了。

安装Caddy

CentOS上可以直接使用yum安装的,也可以下载对应平台的二进制进行安装。

安装命令:

yum install yum-plugin-copr -y
yum copr enable @caddy/caddy -y
yum install caddy -y

配置

配置文件默认是在/etc/caddy/Caddyfile,配置文件只要修改好自己的域名,如果需要https直接填写上http://xxx就可以了,root执行自己的网站目录即可。

我的网站是hugo生成的静态站,直接配置目录。

如果是php可以直接转发的php_fastcgi


# The Caddyfile is an easy way to configure your Caddy web server.
#
# Unless the file starts with a global options block, the first
# uncommented line is always the address of your site.
#
# To use your own domain name (with automatic HTTPS), first make
# sure your domain's A/AAAA DNS records are properly pointed to
# this machine's public IP, then replace ":80" below with your
# domain name.

www.opcai.top https://www.opcai.top http://opcai.top https://opcai.top {
        
        # Set this path to your site's directory.
        root * /data/webroot

        # Enable the static file server.
        file_server
        
        # Another common task is to set up a reverse proxy:
        # reverse_proxy /back/* localhost:8080

        # Or serve a PHP site through php-fpm:
        # php_fastcgi localhost:9000
}

# Refer to the Caddy docs for more information:
# https://caddyserver.com/docs/caddyfile

服务管理

systemctl enable --now caddy  # 启动并设置开机启动
systemctl reload caddy # 重载配置

总结

Caddy还有很多强大的功能,慢慢探索。