百度360必应搜狗淘宝本站头条
当前位置:网站首页 > 技术文章 > 正文

Caddy简单使用

nanshan 2025-01-18 22:52 16 浏览 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

相关推荐

0722-6.2.0-如何在RedHat7.2使用rpm安装CDH(无CM)

文档编写目的在前面的文档中,介绍了在有CM和无CM两种情况下使用rpm方式安装CDH5.10.0,本文档将介绍如何在无CM的情况下使用rpm方式安装CDH6.2.0,与之前安装C5进行对比。环境介绍:...

ARM64 平台基于 openEuler + iSula 环境部署 Kubernetes

为什么要在arm64平台上部署Kubernetes,而且还是鲲鹏920的架构。说来话长。。。此处省略5000字。介绍下系统信息;o架构:鲲鹏920(Kunpeng920)oOS:ope...

生产环境starrocks 3.1存算一体集群部署

集群规划FE:节点主要负责元数据管理、客户端连接管理、查询计划和查询调度。>3节点。BE:节点负责数据存储和SQL执行。>3节点。CN:无存储功能能的BE。环境准备CPU检查JDK...

在CentOS上添加swap虚拟内存并设置优先级

现如今很多云服务器都会自己配置好虚拟内存,当然也有很多没有配置虚拟内存的,虚拟内存可以让我们的低配服务器使用更多的内存,可以减少很多硬件成本,比如我们运行很多服务的时候,内存常常会满,当配置了虚拟内存...

国产深度(deepin)操作系统优化指南

1.升级内核随着deepin版本的更新,会自动升级系统内核,但是我们依旧可以通过命令行手动升级内核,以获取更好的性能和更多的硬件支持。具体操作:-添加PPAs使用以下命令添加PPAs:```...

postgresql-15.4 多节点主从(读写分离)

1、下载软件[root@TX-CN-PostgreSQL01-252software]#wgethttps://ftp.postgresql.org/pub/source/v15.4/postg...

Docker 容器 Java 服务内存与 GC 优化实施方案

一、设置Docker容器内存限制(生产环境建议)1.查看宿主机可用内存bashfree-h#示例输出(假设宿主机剩余16GB可用内存)#Mem:64G...

虚拟内存设置、解决linux内存不够问题

虚拟内存设置(解决linux内存不够情况)背景介绍  Memory指机器物理内存,读写速度低于CPU一个量级,但是高于磁盘不止一个量级。所以,程序和数据如果在内存的话,会有非常快的读写速度。但是,内存...

Elasticsearch性能调优(5):服务器配置选择

在选择elasticsearch服务器时,要尽可能地选择与当前业务量相匹配的服务器。如果服务器配置太低,则意味着需要更多的节点来满足需求,一个集群的节点太多时会增加集群管理的成本。如果服务器配置太高,...

Es如何落地

一、配置准备节点类型CPU内存硬盘网络机器数操作系统data节点16C64G2000G本地SSD所有es同一可用区3(ecs)Centos7master节点2C8G200G云SSD所有es同一可用区...

针对Linux内存管理知识学习总结

现在的服务器大部分都是运行在Linux上面的,所以,作为一个程序员有必要简单地了解一下系统是如何运行的。对于内存部分需要知道:地址映射内存管理的方式缺页异常先来看一些基本的知识,在进程看来,内存分为内...

MySQL进阶之性能优化

概述MySQL的性能优化,包括了服务器硬件优化、操作系统的优化、MySQL数据库配置优化、数据库表设计的优化、SQL语句优化等5个方面的优化。在进行优化之前,需要先掌握性能分析的思路和方法,找出问题,...

Linux Cgroups(Control Groups)原理

LinuxCgroups(ControlGroups)是内核提供的资源分配、限制和监控机制,通过层级化进程分组实现资源的精细化控制。以下从核心原理、操作示例和版本演进三方面详细分析:一、核心原理与...

linux 常用性能优化参数及理解

1.优化内核相关参数配置文件/etc/sysctl.conf配置方法直接将参数添加进文件每条一行.sysctl-a可以查看默认配置sysctl-p执行并检测是否有错误例如设置错了参数:[roo...

如何在 Linux 中使用 Sysctl 命令?

sysctl是一个用于配置和查询Linux内核参数的命令行工具。它通过与/proc/sys虚拟文件系统交互,允许用户在运行时动态修改内核参数。这些参数控制着系统的各种行为,包括网络设置、文件...

取消回复欢迎 发表评论: