Caddy简单使用
wptr33 2025-01-07 16:17 66 浏览
环境: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 start1.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 start2、命令说明
# 命令
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-line3、配置
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 -bcaddy 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 :80803.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:20153.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 version2)安装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 nginx5)加载配置
# 方式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
- 上一篇:一键释放iOS 64位App潜力
- 下一篇:Base64编码转换工具,搞定编码难题
相关推荐
- oracle数据导入导出_oracle数据导入导出工具
-
关于oracle的数据导入导出,这个功能的使用场景,一般是换服务环境,把原先的oracle数据导入到另外一台oracle数据库,或者导出备份使用。只不过oracle的导入导出命令不好记忆,稍稍有点复杂...
- 继续学习Python中的while true/break语句
-
上次讲到if语句的用法,大家在微信公众号问了小编很多问题,那么小编在这几种解决一下,1.else和elif是子模块,不能单独使用2.一个if语句中可以包括很多个elif语句,但结尾只能有一个...
- python continue和break的区别_python中break语句和continue语句的区别
-
python中循环语句经常会使用continue和break,那么这2者的区别是?continue是跳出本次循环,进行下一次循环;break是跳出整个循环;例如:...
- 简单学Python——关键字6——break和continue
-
Python退出循环,有break语句和continue语句两种实现方式。break语句和continue语句的区别:break语句作用是终止循环。continue语句作用是跳出本轮循环,继续下一次循...
- 2-1,0基础学Python之 break退出循环、 continue继续循环 多重循
-
用for循环或者while循环时,如果要在循环体内直接退出循环,可以使用break语句。比如计算1至100的整数和,我们用while来实现:sum=0x=1whileTrue...
- Python 中 break 和 continue 傻傻分不清
-
大家好啊,我是大田。...
- python中的流程控制语句:continue、break 和 return使用方法
-
Python中,continue、break和return是控制流程的关键语句,用于在循环或函数中提前退出或跳过某些操作。它们的用途和区别如下:1.continue(跳过当前循环的剩余部分,进...
- L017:continue和break - 教程文案
-
continue和break在Python中,continue和break是用于控制循环(如for和while)执行流程的关键字,它们的作用如下:1.continue:跳过当前迭代,...
- 作为前端开发者,你都经历过怎样的面试?
-
已经裸辞1个月了,最近开始投简历找工作,遇到各种各样的面试,今天分享一下。其实在职的时候也做过面试官,面试官时,感觉自己问的问题很难区分候选人的能力,最好的办法就是看看候选人的github上的代码仓库...
- 面试被问 const 是否不可变?这样回答才显功底
-
作为前端开发者,我在学习ES6特性时,总被const的"善变"搞得一头雾水——为什么用const声明的数组还能push元素?为什么基本类型赋值就会报错?直到翻遍MDN文档、对着内存图反...
- 2023金九银十必看前端面试题!2w字精品!
-
导文2023金九银十必看前端面试题!金九银十黄金期来了想要跳槽的小伙伴快来看啊CSS1.请解释CSS的盒模型是什么,并描述其组成部分。...
- 前端面试总结_前端面试题整理
-
记得当时大二的时候,看到实验室的学长学姐忙于各种春招,有些收获了大厂offer,有些还在苦苦面试,其实那时候的心里还蛮忐忑的,不知道自己大三的时候会是什么样的一个水平,所以从19年的寒假放完,大二下学...
- 由浅入深,66条JavaScript面试知识点(七)
-
作者:JakeZhang转发链接:https://juejin.im/post/5ef8377f6fb9a07e693a6061目录...
- 2024前端面试真题之—VUE篇_前端面试题vue2020及答案
-
添加图片注释,不超过140字(可选)...
- 今年最常见的前端面试题,你会做几道?
-
在面试或招聘前端开发人员时,期望、现实和需求之间总是存在着巨大差距。面试其实是一个交流想法的地方,挑战人们的思考方式,并客观地分析给定的问题。可以通过面试了解人们如何做出决策,了解一个人对技术和解决问...
- 一周热门
- 最近发表
-
- oracle数据导入导出_oracle数据导入导出工具
- 继续学习Python中的while true/break语句
- python continue和break的区别_python中break语句和continue语句的区别
- 简单学Python——关键字6——break和continue
- 2-1,0基础学Python之 break退出循环、 continue继续循环 多重循
- Python 中 break 和 continue 傻傻分不清
- python中的流程控制语句:continue、break 和 return使用方法
- L017:continue和break - 教程文案
- 作为前端开发者,你都经历过怎样的面试?
- 面试被问 const 是否不可变?这样回答才显功底
- 标签列表
-
- git pull (33)
- git fetch (35)
- mysql insert (35)
- mysql distinct (37)
- concat_ws (36)
- java continue (36)
- jenkins官网 (37)
- mysql 子查询 (37)
- python元组 (33)
- mybatis 分页 (35)
- vba split (37)
- redis watch (34)
- python list sort (37)
- nvarchar2 (34)
- mysql not null (36)
- hmset (35)
- python telnet (35)
- python readlines() 方法 (36)
- munmap (35)
- docker network create (35)
- redis 集合 (37)
- python sftp (37)
- setpriority (34)
- c语言 switch (34)
- git commit (34)
