Caddy简单使用
nanshan 2025-01-18 22:52 11 浏览 0 评论
环境:CentOS 7系统
Caddy官网:https://caddyserver.com/
1、安装
1.1、方式1:下载预编译的文件
# 1、下载
wget https://github.com/caddyserver/caddy/releases/download/v2.8.4/caddy_2.8.4_linux_amd64.tar.gz
# 2、解压
tar -xf caddy_2.8.4_linux_amd64.tar.gz
# 3、移动文件到/usr/local/bin/
mv caddy /usr/local/bin/
# 4、查看版本
caddy version
# 5、命令帮助
# 启动:caddy start 或者 caddy run
# 重启:caddy reload
# 停止:caddy stop
caddy
# 6、启动
# 6.1、前台运行
caddy run
# 或者
# 6.2、后台运行
# caddy start --config caddy.json
# caddy run --config nginx.conf --adapter nginx
# caddy run --config caddy.file --adapter caddyfile
caddy start
1.2、方式2:使用源码编译安装
安装go:
# 1、下载
# 下载地址:https://go.dev/dl/
wget https://go.dev/dl/go1.23.3.linux-amd64.tar.gz
# 2、解压
tar -xf go1.23.3.linux-amd64.tar.gz -C /usr/local/
# 3、添加环境变量
# 添加到环境变量,编辑/root/.bash_profile文件,内容如下:
GO_PATH=/usr/local/go/bin
PATH=$PATH:$HOME/bin:$GO_PATH
export PATH
# 4、使环境变量生效
source /root/.bash_profile
# 5、查看版本
go version
# 6、配置goproxy
go env -w GOPROXY=https://goproxy.cn,direct
安装Caddy:
# 1、下载
git clone "https://github.com/caddyserver/caddy.git"
# 2、切换目录
cd caddy/cmd/caddy/
# 3、构建
go build
# 4、复制文件到/usr/local/bin/
cp caddy /usr/local/bin/
# 5、其他
caddy
# 6、启动
# 6.1、前台运行
caddy run
# 或者
# 6.2、后台运行
# caddy start --config caddy.json
# caddy run --config nginx.conf --adapter nginx
# caddy run --config caddy.file --adapter caddyfile
caddy start
2、命令说明
# 命令
caddy
# 显示内容如下
Caddy is an extensible server platform written in Go.
At its core, Caddy merely manages configuration. Modules are plugged
in statically at compile-time to provide useful functionality. Caddy's
standard distribution includes common modules to serve HTTP, TLS,
and PKI applications, including the automation of certificates.
To run Caddy, use:
- 'caddy run' to run Caddy in the foreground (recommended).
- 'caddy start' to start Caddy in the background; only do this
if you will be keeping the terminal window open until you run
'caddy stop' to close the server.
When Caddy is started, it opens a locally-bound administrative socket
to which configuration can be POSTed via a restful HTTP API (see
https://caddyserver.com/docs/api).
Caddy's native configuration format is JSON. However, config adapters
can be used to convert other config formats to JSON when Caddy receives
its configuration. The Caddyfile is a built-in config adapter that is
popular for hand-written configurations due to its straightforward
syntax (see https://caddyserver.com/docs/caddyfile). Many third-party
adapters are available (see https://caddyserver.com/docs/config-adapters).
Use 'caddy adapt' to see how a config translates to JSON.
For convenience, the CLI can act as an HTTP client to give Caddy its
initial configuration for you. If a file named Caddyfile is in the
current working directory, it will do this automatically. Otherwise,
you can use the --config flag to specify the path to a config file.
Some special-purpose subcommands build and load a configuration file
for you directly from command line input; for example:
- caddy file-server
- caddy reverse-proxy
- caddy respond
These commands disable the administration endpoint because their
configuration is specified solely on the command line.
In general, the most common way to run Caddy is simply:
$ caddy run
Or, with a configuration file:
$ caddy run --config caddy.json
If running interactively in a terminal, running Caddy in the
background may be more convenient:
$ caddy start
...
$ caddy stop
This allows you to run other commands while Caddy stays running.
Be sure to stop Caddy before you close the terminal!
Depending on the system, Caddy may need permission to bind to low
ports. One way to do this on Linux is to use setcap:
$ sudo setcap cap_net_bind_service=+ep $(which caddy)
Remember to run that command again after replacing the binary.
See the Caddy website for tutorials, configuration structure,
syntax, and module documentation: https://caddyserver.com/docs/
Custom Caddy builds are available on the Caddy download page at:
https://caddyserver.com/download
The xcaddy command can be used to build Caddy from source with or
without additional plugins: https://github.com/caddyserver/xcaddy
Where possible, Caddy should be installed using officially-supported
package installers: https://caddyserver.com/docs/install
Instructions for running Caddy in production are also available:
https://caddyserver.com/docs/running
Usage:
caddy [command]
Examples:
$ caddy run
$ caddy run --config caddy.json
$ caddy reload --config caddy.json
$ caddy stop
Available Commands:
adapt Adapts a configuration to Caddy's native JSON
add-package Adds Caddy packages (EXPERIMENTAL)
build-info Prints information about this build
completion Generate completion script
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 Help about any command
list-modules Lists the installed Caddy modules
manpage Generates the manual pages for Caddy commands
reload Changes the config of the running Caddy instance
remove-package Removes Caddy packages (EXPERIMENTAL)
respond Simple, hard-coded HTTP responses for development and testing
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
storage Commands for working with Caddy's storage (EXPERIMENTAL)
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
Flags:
-h, --help help for caddy
-v, --version version for caddy
Use "caddy [command] --help" for more information about a command.
Full documentation is available at:
https://caddyserver.com/docs/command-line
3、配置
3.1、方式1:命令行
详见:https://caddyserver.com/docs/command-line
caddy file-server(静态文件服务器):
# 参考:https://caddyserver.com/docs/command-line#caddy-file-server
# 命令
caddy file-server
[-r, --root <path>]
[--listen <addr>]
[-d, --domain <example.com>]
[-b, --browse]
[--reveal-symlinks]
[-t, --templates]
[--access-log]
[-v, --debug]
[--no-compress]
[-p, --precompressed]
# 示例
caddy file-server -r /usr/local/nginx/html -b
caddy respond(http服务器):
# 参考:https://caddyserver.com/docs/command-line#caddy-respond
# 命令
caddy respond
[-s, --status <code>]
[-H, --header "<Field>: <value>"]
[-b, --body <content>]
[-l, --listen <addr>]
[-v, --debug]
[--access-log]
[<status|body>]
# 示例
caddy respond --listen :2000-2004 "I'm server {{.N}} on port {{.Port}}"
caddy reverse-proxy(http反向代理):
# 参考:https://caddyserver.com/docs/command-line#caddy-reverse-proxy
# 命令
caddy reverse-proxy
[-f, --from <addr>]
(-t, --to <addr>)
[-H, --header-up "<Field>: <value>"]
[-d, --header-down "<Field>: <value>"]
[-c, --change-host-header]
[-r, --disable-redirects]
[-i, --internal-certs]
[-v, --debug]
[--access-log]
[--insecure]
# 示例
# ----------from----------:
caddy respond --listen :2024 "I'm server {{.N}} on port {{.Port}}"
# ----------to----------:
# 当前浏览器访问8080端口,反向代理到2024端口
caddy reverse-proxy -t :2024 -f :8080
3.2、方式2:JSON文件
详见:https://caddyserver.com/docs/json/
1)编写caddy.json文件
{
"apps": {
"http": {
"servers": {
"example": {
"listen": [":2015"],
"routes": [
{
"handle": [{
"handler": "static_response",
"body": "Hello, world!"
}]
}
]
}
}
}
}
}
2)加载配置
# 参考:https://caddyserver.com/docs/getting-started
# 1、上传文件
curl localhost:2019/load -H "Content-Type: application/json" -d @caddy.json
# 2、查看配置
curl localhost:2019/config/
3)访问站点
curl localhost:2015
3.3、方式3:Caddyfile文件
详见:https://caddyserver.com/docs/quick-starts/caddyfile
1)编写Caddyfile文件
:5566 {
# 指定root为nginx的html目录
root * /usr/local/nginx/html
file_server
log {
output file /var/log/access.log
}
handle /halo/ {
respond "hello world"
}
handle_path /local-nginx/* {
# 代理本地nginx服务
reverse_proxy localhost
}
handle_path /local-php/* {
# 访问php项目
root * /usr/local/nginx/html
php_fastcgi localhost:9000
file_server
}
handle /jump-csdn/ {
# 重定向
redir http://www.csdn.net/
}
handle_path /proxy-csdn/* {
# 反向代理
reverse_proxy https://www.csdn.net
}
}
:6060 {
reverse_proxy localhost
}
:7070 {
redir http://www.baidu.com
}
2)加载配置
方式1:使用caddy start启动时,同时加载配置的情况
# 1、启动并加载配置
caddy start --config Caddyfile --adapter caddyfile
# 2、查看配置
curl localhost:2019/config/
注:如果不确定是否已启动caddy服务,使用ps aux | grep caddy命令查看。如果存在,使用caddy stop命令或者使用kill 进程id来停止服务。
方式2:caddy服务已经启动,使用curl命令上传文件的情况
# 参考:https://caddyserver.com/docs/quick-starts/caddyfile
# 1、上传文件
curl localhost:2019/load -H "Content-Type: text/caddyfile" --data-binary @Caddyfile
# 2、重启
caddy reload
# 3、查看配置
curl localhost:2019/config/
方式3:caddy服务已经启动,使用caddy adapt加载配置的情况
# 参考:https://caddyserver.com/docs/getting-started
# 1、加载配置
# 假设Caddyfile文件在/path/to目录下
# caddy adapt --config /path/to/Caddyfile
# 如果caddy adapt命令不指定--config参数,默认加载当前路径下的Caddyfile文件
caddy adapt
# 2、重启
caddy reload
# 3、查看配置
curl localhost:2019/config/
3)访问站点
# 浏览器访问以下地址:
# 其中:localhost替换成当前的 ip 或 域名
# localhost:5566/
# localhost:5566/halo/
# localhost:5566/local-nginx/
# localhost:5566/local-php/
# localhost:5566/jump-csdn/
# localhost:5566/proxy-csdn/
# localhost:6060/
# localhost:7070/
3.4、方式4:Nginx文件
详见:https://caddyserver.com/docs/config-adapters
1)安装xcaddy
# 参考:https://github.com/caddyserver/xcaddy#install
# 1、下载
wget https://github.com/caddyserver/xcaddy/releases/download/v0.4.4/xcaddy_0.4.4_linux_amd64.tar.gz
# 2、解压
tar -xf xcaddy_0.4.4_linux_amd64.tar.gz
# 3、移动文件到/usr/local/bin/
mv xcaddy /usr/local/bin/
# 4、查看版本
xcaddy version
2)安装nginx-adapter
# 参考:https://github.com/caddyserver/nginx-adapter
# 1、安装
xcaddy build --with github.com/caddyserver/nginx-adapter
# 2、移动文件到/usr/local/bin/
mv caddy /usr/local/bin/
3)编写Nginx配置文件
# 相关文档:
# https://github.com/caddyserver/nginx-adapter/
# https://github.com/caddyserver/nginx-adapter/blob/master/testdata/example1.conf
# https://github.com/caddyserver/nginx-adapter/blob/master/testdata/example2.conf
# 以下为nginx.conf配置内容:
http {
server {
listen 8080;
location / {
proxy_pass http://127.0.0.1:2024;
}
}
}
其中2024端口:
# 执行命令
caddy respond --listen :2024 "I'm server {{.N}} on port {{.Port}}"
4)检查配置文件
caddy validate -c nginx.conf -a nginx
5)加载配置
# 方式1:启动并加载配置
# caddy run --config nginx.conf --adapter nginx
# caddy start --config nginx.conf --adapter nginx
caddy start --config nginx.conf --adapter nginx
# 或者
# 方式2:caddy服务已经启动,使用curl命令上传文件
# 2.1、上传文件:
# curl localhost:2019/load -H "Content-Type: text/nginx" --data-binary @nginx.conf
# 2.2、重启:
# caddy reload
# 2.3、查看配置
# curl localhost:2019/config/
6)访问站点
# 浏览器访问以下地址:
# 其中:localhost替换成当前的 ip 或 域名
# localhost:8080/
4、参考
- https://caddyserver.com/
- https://caddyserver.com/docs/getting-started
- https://github.com/caddyserver/caddy/tree/master/caddytest/integration/caddyfile_adapt
相关推荐
- 教你一个解决手机卡顿的方法(10秒解决手机卡顿问题)
-
我们的手机天天刷头条,看视频,用了一阶段时间以后,就时不时的发生卡顿现象。昨天我的手机就发现了这个问题。友友们,你们遇到过这样的问题吗?你们都是怎样解决的?我看了一眼我的粉丝情况,头条君给我分析的很精...
- 手机视频缓存清理,3步彻底清空,告别卡顿
-
在我们使用手机观看视频的过程中,经常会产生大量的缓存垃圾,这些垃圾文件不仅占用了手机的存储空间,还可能导致手机卡顿和运行缓慢。然而,你知道如何彻底清空手机的视频缓存,让手机恢复流畅的使用体验吗?在本文...
- 关手机这个开关,轻松提升流畅度!
-
关闭手机这个开关,跟新买的一样流畅。手机不要再清理垃圾了,只要关闭这个开关,手机就会和新买的差不多,丝滑流畅不卡顿。其实抖音里就隐藏着一个小开关,每天刷过的视频都会保存在手机里,如果一直不清理,手机就...
- 如何清理今日头条和西瓜视频的内存,让手机流畅不卡顿?
-
对于老年人而言,今日头条和西瓜视频能带来丰富的资讯与娱乐。然而,随着使用时间的增加,这些应用会占用大量手机内存,致使手机运行卡顿。那该如何解决呢?接下来,我将用最简单易懂的方式教老年人清理今日头条和西...
- 视频在线如何转换格式?好用不卡顿的三种转换办法
-
转换视频格式目前来说已经是很熟练的操作了,但是还有些用户可能还是不知道,小编今天就特意给大家带来一些小众才知道的转换教程,让新手也能快速的上手去转换视频格式,以后获取到视频就不怕内容丢失了,视频的格式...
- 如何把视频慢放处理?这几个慢放方法记得收藏
-
如何把视频慢放处理?如果你想让视频慢放,可能是因为你想放慢一些精彩的瞬间,或者你想制作一个慢动作视频。在这篇文章中,我们将介绍一些调速方法,这些方法可以有效地调整视频速度,一起来学习一下吧。方法一:使...
- 如何清理看过的视频,释放垃圾,让手机更流畅?
-
现在谁的手机上没几个短视频平台,无聊时就会刷别人的视频。可您知道吗?我们看过的内容都会被自动保存在手机里,而且很耗内存。如果长时间不释放,手机就会出现各种问题,其中最突出的就是反应慢。相信很多老年人的...
- 手机掉帧是怎么回事?刷视频的时候经常掉帧卡顿
-
手机掉帧是指在运行应用或视频时,画面出现卡顿、不流畅的现象,通常由硬件性能不足、软件优化不佳、内存占用过高、网络问题或设备过热等因素引起。尤其是在刷视频时,掉帧问题可能更为明显,以下是具体原因及解决方...
- 拍视频画面卡顿不流畅,原来是相机设置错误 #短视频拍摄
-
拍摄视频时,应该选择哪种快门速度?许多新手朋友可能会认为,快门速度越高,画面就越清晰,实则不然。因为拍摄视频时,需要考虑一个问题,即动态模糊。例如,如果设置为24帧/秒,那么每秒钟会拍摄24张图片。如...
- 手机卡顿最大原因#视频太卡怎么变流畅
-
抖音这几个开关是手机卡顿的最大原因。你是不是也会经常遇到刷视频的时候,打开一个视频之后老半天还在那转着圈圈,总觉得手机没有之前流畅了。这就说明你的手机占用的内存太多了,导致手机卡顿,使用不流畅。使用手...
- 为啥你家的玩游戏和刷视频经常性的会卡,那是你不懂这些小妙招
-
本内容来源于@什么值得买APP,观点仅代表作者本人|作者:暴走的黄小猪说到网速有不少的值友都有一个共同点,那就是“卡”,那是你根本没体验过啥叫真正的网速啊,全屋零四条网络报表也花不了几个钱你们的方法...
- 电脑看视频卡顿有什么解决方法?(电脑看视频画面卡顿是什么原因)
-
电脑看视频卡顿的原因可能多种多样,包括硬件性能不足、网络问题、软件设置不当等。以下是一些常见的解决方法,帮助你改善视频播放的流畅度:一、硬件方面1.检查硬件性能:如果电脑配置较低,尤其是CPU、内存或...
- 手机Wi-Fi满格但视频卡顿,你需要这样解决
-
累了一天的打工人回家拿出手机准备玩玩游戏,看看电影时,发现网络异常卡顿,但手机又显示Wi-Fi信号满格,当咱们遇到此类问题时,这些动作能让网络恢复正常,方法如下。一、重启路由器和光猫很多家庭在安装好路...
- 视频越刷越卡?原来是路由器开启了这个功能,关闭方法来了
-
应该很多小伙伴都有过类似的经历,就是在家里长时间刷视频或者看剧的时候,网速好像会越来越慢,视频总是要加载。手机本身可能是一部分原因,但路由器也会影响,你知道吗?当我们在刷视频的,路由器会悄悄地开启大量...
- 一招解决视频卡顿的问题,改变发布渠道后,结果香了
-
最近一段时间拍了很多美景视频,编辑发布到头条后,有时一直显示在缓冲,播放不了,有时打开断断续续的,老是卡顿。导致的后果是:要么展现量很低,要么阅读量寥寥无几,这让我非常苦恼。所以再发布作品时,我只好文...
你 发表评论:
欢迎- 一周热门
-
-
如何在安装前及安装后修改黑群晖的Mac地址和Sn系列号
-
极空间如何无损移机,新Z4 Pro又有哪些升级?极空间Z4 Pro深度体验
-
爱折腾的特斯拉车主必看!手把手教你TESLAMATE的备份和恢复
-
10个免费文件中转服务站,分享文件简单方便,你知道几个?
-
[常用工具] OpenCV_contrib库在windows下编译使用指南
-
日本海上自卫队的军衔制度(日本海上自卫队的军衔制度是什么)
-
Ubuntu系统Daphne + Nginx + supervisor部署Django项目
-
WindowsServer2022|配置NTP服务器的命令
-
【系统配置】信创终端挂载NAS共享全攻略:一步到位!
-
UOS服务器操作系统防火墙设置(uos20关闭防火墙)
-
- 最近发表
- 标签列表
-
- linux 查询端口号 (58)
- docker映射容器目录到宿主机 (66)
- 杀端口 (60)
- yum更换阿里源 (62)
- internet explorer 增强的安全配置已启用 (65)
- linux自动挂载 (56)
- 禁用selinux (55)
- sysv-rc-conf (69)
- ubuntu防火墙状态查看 (64)
- windows server 2022激活密钥 (56)
- 无法与服务器建立安全连接是什么意思 (74)
- 443/80端口被占用怎么解决 (56)
- ping无法访问目标主机怎么解决 (58)
- fdatasync (59)
- 405 not allowed (56)
- 免备案虚拟主机zxhost (55)
- linux根据pid查看进程 (60)
- dhcp工具 (62)
- mysql 1045 (57)
- 宝塔远程工具 (56)
- ssh服务器拒绝了密码 请再试一次 (56)
- ubuntu卸载docker (56)
- linux查看nginx状态 (63)
- tomcat 乱码 (76)
- 2008r2激活序列号 (65)