现如今,企业web服务器,可以说nginx,无人不知无人不晓,哪个不怕死的,要来替换nginx呢?——caddy。长江后浪推前浪,作为IT行业人士,请不要大惊小怪,这是必然的,而且速度会很快,所以,我们必须不断学习,走在技术变革的前沿。
caddy是什么?
caddy是一个强大的、企业级、开放源代码的服务器,使用GO语言编写,可以自动实现https加密数据传输。
caddy的特点与优势?
-
默认时自动支持HTTPS
-
ZeroSSL 和 Let's Encrypt实现公共名
-
CA包括完整的内部名和ip信息
-
可以与集群中的其他Caddy配合
-
当服务因为TLS/OCSP/其他证书,出问题时,依然能正常运行
-
能支持万亿级别的请求和管理百万级别TLS证书请求
-
能支持十万级站点的扩展部署
-
默认支持HTTP/1.1, HTTP/2, HTTP/3
-
可以轻松使用Caddyfile进行配置
-
使用json文件实现强大的配置能力
-
可以使用JSON API接口进行动态配置
-
环境运行,没有外部依赖
-
用go语言编写,拥有广告的内存安全
学习caddy
caddy安装
# centos7
yum install yum-plugin-copr -y
yum copr enable @caddy/caddy -y
yum install caddy -y
[root@centos7 ~]# caddy
Caddy is an extensible server platform.
usage:
caddy <command> [<args...>]
commands:
adapt Adapts a configuration to Caddy's native JSON
add-package Adds Caddy packages (EXPERIMENTAL)
build-info Prints information about this build
environ Prints the environment
file-server Spins up a production-ready file server
fmt Formats a Caddyfile
hash-password Hashes a password and writes base64
help Shows help for a Caddy subcommand
list-modules Lists the installed Caddy modules
reload Changes the config of the running Caddy instance
remove-package Removes Caddy packages (EXPERIMENTAL)
reverse-proxy A quick and production-ready reverse proxy
run Starts the Caddy process and blocks indefinitely
start Starts the Caddy process in the background and then returns
stop Gracefully stops a started Caddy process
trust Installs a CA certificate into local trust stores
untrust Untrusts a locally-trusted CA certificate
upgrade Upgrade Caddy (EXPERIMENTAL)
validate Tests whether a configuration file is valid
version Prints the version
Use 'caddy help <command>' for more information about a command.
Full documentation is available at:
https://caddyserver.com/docs/command-line
启动caddy
-
caddy run 启动一个非守护的caddy进程,按ctrl+c停止;
[root@centos7 ~]# caddy run --help
Usage of run:
-adapter string
Name of config adapter to apply 指定适配的配置文件
-config string
Configuration file 指定config文件
-envfile string
Environment file to load 指定家族的环境变量
-environ
Print environment 打印环境变量
-pidfile string
Path of file to which to write process ID 存放PID的文件
-pingback string
Echo confirmation bytes to this address on success 成功时的回显地址
-resume
Use saved config, if any (and prefer over --config file)保存配置,使--config的参数无效
-watch
Watch config file for changes and reload it automatically发现配置变更时,自动重新加载
caddy run -config /path/caddy.json -watch
这个命令,就是在启动时,加载了一个config文件,使用了watch,那么,在之后过程中,一旦这个配置文件被修改,caddy进程会自动加载,变更信息。-
caddy start
启动caddy为守护进程,caddy stop
停止。
[root@centos7 ~]# caddy start --help
Usage of start:
-adapter string
Name of config adapter to apply
-config string
Configuration file
-envfile string
Environment file to load
-pidfile string
Path of file to which to write process ID
-watch
Reload changed config file automatically
run
基本一样。配置文件
{
"apps": {
"http": {
"servers": {
"example": {
"listen": [":2015"],
"routes": [
{
"handle": [{
"handler": "static_response",
"body": "Hello, Allen!"
}]
}
]
}
}
}
}
}
curl localhost:2019/load -H "Content-Type: application/json" -d @caddy.json
JSON配置文件模板
{
"admin": {
"disabled": false,
"listen": "", // 绑定的端口,默认2019
"enforce_origin": false,
"origins": [""],
"config": {
"persist": false,
"load": {•••}
},
"identity": {
"identifiers": [""],
"issuers": [{•••}]
},
"remote": {
"listen": "",
"access_control": [{
"public_keys": [""],
"permissions": [{
"paths": [""],
"methods": [""]
}]
}]
}
},
"logging": {
"sink": {
"writer": {•••}
},
"logs": {
"": {
"writer": {•••},
"encoder": {•••},
"level": "",
"sampling": {
"interval": 0,
"first": 0,
"thereafter": 0
},
"include": [""],
"exclude": [""]
}
}
},
"storage": {•••},
"apps": {•••}
}
localhost:80 {
respond "xxxxx"
}
www.domain.com:2016 {
respond "xxxxx"
}
# 快速启动一个文件服务
caddy file-server
# 使用一个https服务访问文件服务
caddy file-server --domain www.examp.com
# 设置代理
caddy reverse-proxy --from examp.com --to localhost:9010
# 配置Caddyfile文件
reverse_proxy 10.0.0.1:9010 10.0.0.2:9010 10.0.0.3:9010 {
lb_policy random_choose 2
health_path /ok
health_interval 10s
}
接口、自动化、互联网大厂真题
简历模板......统统都有
快扫码领取吧~
注:本文柠檬班老师原创,转载需注明来源
文章评论