Caddy简单使用
wptr33 2025-01-07 16:17 45 浏览
环境: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
- 上一篇:一键释放iOS 64位App潜力
- 下一篇:Base64编码转换工具,搞定编码难题
相关推荐
- redis的八种使用场景
-
前言:redis是我们工作开发中,经常要打交道的,下面对redis的使用场景做总结介绍也是对redis举报的功能做梳理。缓存Redis最常见的用途是作为缓存,用于加速应用程序的响应速度。...
- 基于Redis的3种分布式ID生成策略
-
在分布式系统设计中,全局唯一ID是一个基础而关键的组件。随着业务规模扩大和系统架构向微服务演进,传统的单机自增ID已无法满足需求。高并发、高可用的分布式ID生成方案成为构建可靠分布式系统的必要条件。R...
- 基于OpenWrt系统路由器的模式切换与网页设计
-
摘要:目前商用WiFi路由器已应用到多个领域,商家通过给用户提供一个稳定免费WiFi热点达到吸引客户、提升服务的目标。传统路由器自带的Luci界面提供了工厂模式的Web界面,用户可通过该界面配置路...
- 这篇文章教你看明白 nginx-ingress 控制器
-
主机nginx一般nginx做主机反向代理(网关)有以下配置...
- 如何用redis实现注册中心
-
一句话总结使用Redis实现注册中心:服务注册...
- 爱可可老师24小时热门分享(2020.5.10)
-
No1.看自己以前写的代码是种什么体验?No2.DooM-chip!国外网友SylvainLefebvre自制的无CPU、无操作码、无指令计数器...No3.我认为CS学位可以更好,如...
- Apportable:拯救程序员,IOS一秒变安卓
-
摘要:还在为了跨平台使用cocos2d-x吗,拯救objc程序员的奇葩来了,ApportableSDK:FreeAndroidsupportforcocos2d-iPhone。App...
- JAVA实现超买超卖方案汇总,那个最适合你,一篇文章彻底讲透
-
以下是几种Java实现超买超卖问题的核心解决方案及代码示例,针对高并发场景下的库存扣减问题:方案一:Redis原子操作+Lua脚本(推荐)//使用Redis+Lua保证原子性publicbo...
- 3月26日更新 快速施法自动施法可独立设置
-
2016年3月26日DOTA2有一个79.6MB的更新主要是针对自动施法和快速施法的调整本来内容不多不少朋友都有自动施法和快速施法的困扰英文更新日志一些视觉BUG修复就不翻译了主要翻译自动施...
- Redis 是如何提供服务的
-
在刚刚接触Redis的时候,最想要知道的是一个’setnameJhon’命令到达Redis服务器的时候,它是如何返回’OK’的?里面命令处理的流程如何,具体细节怎么样?你一定有问过自己...
- lua _G、_VERSION使用
-
到这里我们已经把lua基础库中的函数介绍完了,除了函数外基础库中还有两个常量,一个是_G,另一个是_VERSION。_G是基础库本身,指向自己,这个变量很有意思,可以无限引用自己,最后得到的还是自己,...
- China's top diplomat to chair third China-Pacific Island countries foreign ministers' meeting
-
BEIJING,May21(Xinhua)--ChineseForeignMinisterWangYi,alsoamemberofthePoliticalBureau...
- 移动工作交流工具Lua推出Insights数据分析产品
-
Lua是一个适用于各种职业人士的移动交流平台,它在今天推出了一项叫做Insights的全新功能。Insights是一个数据平台,客户可以在上面实时看到员工之间的交流情况,并分析这些情况对公司发展的影响...
- Redis 7新武器:用Redis Stack实现向量搜索的极限压测
-
当传统关系型数据库还在为向量相似度搜索的性能挣扎时,Redis7的RedisStack...
- Nginx/OpenResty详解,Nginx Lua编程,重定向与内部子请求
-
重定向与内部子请求Nginx的rewrite指令不仅可以在Nginx内部的server、location之间进行跳转,还可以进行外部链接的重定向。通过ngx_lua模块的Lua函数除了能实现Nginx...
- 一周热门
-
-
C# 13 和 .NET 9 全知道 :13 使用 ASP.NET Core 构建网站 (1)
-
因果推断Matching方式实现代码 因果推断模型
-
git pull命令使用实例 git pull--rebase
-
git pull 和git fetch 命令分别有什么作用?二者有什么区别?
-
面试官:git pull是哪两个指令的组合?
-
git 执行pull错误如何撤销 git pull fail
-
git fetch 和git pull 的异同 git中fetch和pull的区别
-
git pull 之后本地代码被覆盖 解决方案
-
还可以这样玩?Git基本原理及各种骚操作,涨知识了
-
git命令之pull git.pull
-
- 最近发表
- 标签列表
-
- 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)