惯性聚合 高效追踪和阅读你感兴趣的博客、新闻、科技资讯
阅读原文 在惯性聚合中打开

推荐订阅源

IT之家
IT之家
Y
Y Combinator Blog
月光博客
月光博客
Blog — PlanetScale
Blog — PlanetScale
GbyAI
GbyAI
Cyber Security Advisories - MS-ISAC
Cyber Security Advisories - MS-ISAC
博客园 - 三生石上(FineUI控件)
S
SegmentFault 最新的问题
让小产品的独立变现更简单 - ezindie.com
让小产品的独立变现更简单 - ezindie.com
美团技术团队
雷峰网
雷峰网
酷 壳 – CoolShell
酷 壳 – CoolShell
Last Week in AI
Last Week in AI
钛媒体:引领未来商业与生活新知
钛媒体:引领未来商业与生活新知
有赞技术团队
有赞技术团队
博客园 - 司徒正美
V
Visual Studio Blog
小众软件
小众软件
OSCHINA 社区最新新闻
OSCHINA 社区最新新闻
T
Tailwind CSS Blog
Apple Machine Learning Research
Apple Machine Learning Research
奇客Solidot–传递最新科技情报
奇客Solidot–传递最新科技情报
A
About on SuperTechFans
The Cloudflare Blog

秋水逸冰

丽萨主机:双ISP家宽住宅IP VPS/CN2 GIA精品网/G口大带宽 | 秋水逸冰 Linux Kernel 6.18.x 编译版 | 秋水逸冰 思考未来人与 AI 的关系 | 秋水逸冰 3X-UI 面板安装和使用教程 | 秋水逸冰 Xray-UI 面板安装和使用教程 | 秋水逸冰 WCMP (Windows + Caddy + MariaDB + PHP) Nginx for Windows | 秋水逸冰 Aria2 for Windows | 秋水逸冰 Curl for Windows | 秋水逸冰 Wget for Windows | 秋水逸冰 Windows 11 Enterprise LTSC DD 镜像 Windows Server 2025 Datacenter DD 镜像 重新编译 curl 以支持 HTTP3 | 秋水逸冰 tiny10 和 tiny11 23H2 的 Windows DD 镜像 LCMP (Linux + Caddy + MariaDB + PHP) Windows 11 Pro for Workstations 22H2 DD 镜像 Shadowsocks 编译版 by Teddysun | 秋水逸冰 适用于 UEFI 启动的多个 Windows 系统 DD 镜像 Linux Kernel 5.15.x 编译版 | 秋水逸冰 制作适用于 WSL 的任意 Linux 镜像 Windows 11 Pro for Workstations 21H2 DD 镜像 Windows 10 Enterprise LTSC DD 镜像 Linux Kernel 5.14.x 编译版 | 秋水逸冰 Linux Kernel 5.13.x 编译版 | 秋水逸冰 Windows Server 2022 Datacenter DD 镜像 在 Windows 10 上安装 Rocky Linux 系统 Linux Kernel 5.12.x 编译版 | 秋水逸冰 Linux Kernel 5.11.x 编译版 | 秋水逸冰 在 Windows 10 上安装 CentOS 系统 Linux Kernel 5.10.x 编译版 | 秋水逸冰
从 Apache httpd 切换到 Caddy2
秋水逸冰 · 2023-06-11 · via 秋水逸冰

2022 年 6 月 HTTP/3 的 RFC 发布迄今已经有一年了。市面上常用的几款 Web Server,Caddy Web Server 是最早支持 HTTP/3 的,而 Nginx 直到 2023 年 5 月 23 日 1.25.0 版本才开始体验支持。至于 Apache httpd,也不知道要到什么时候才支持 HTTP/3。
之所以打算正式从 Apache httpd 切换到 Caddy,是因为除 Caddy 之外的 Web Server 配置证书以及自动续期还是比较麻烦。众所周知,一般情况下市面上大部分的免费证书有效期只有 90 天,需要在过期之前续签。采用 acme.sh 脚本进行自动续签,然后 nginx reload 或者 httpd reload 有时候不太靠谱,有一定的失败几率。所以最后决定 Caddy 一把梭,将证书问题一劳永逸。
本文在 LCMP (Linux + Caddy + MariaDB + PHP) 环境的基础下,涉及到切换到 Caddy 的一些细节,在此记录一下。

1. 适用于 WordPress 的 Caddy 2 配置

适用于 WordPress 的 Apache httpd 的 .htaccess 基本上不需要转换,是因为 Caddy 2 的 php_fastcgi 指令已经提前做好了一些预设。
php_fastcgi 指令其实是一系列指令的快捷方式。具体解释如下:

route {
	# 为目录请求添加尾部斜线
	@canonicalPath {
		file {path}/index.php
		not path */
	}
	redir @canonicalPath {path}/ 308
	# 如果请求的文件不存在,尝试索引文件
	@indexFiles file {
		try_files {path} {path}/index.php index.php
		split_path .php
	}
	rewrite @indexFiles {http.matchers.file.relative}
	# 将 PHP 文件代理给 FastCGI 应答器
	@phpFiles path *.php
	reverse_proxy @phpFiles  {
		transport fastcgi {
			split .php
		}
	}
}

只需要禁止访问一些 WordPress 的资源即可。比如 xmlrpc.php,以及 wp-content/uploads 下的 php 文件。
下面的配置就是将禁止访问的资源重定向到首页 index.php。

www.example.com {
	header {
		Strict-Transport-Security "max-age=31536000; preload"
		X-Content-Type-Options nosniff
		X-Frame-Options SAMEORIGIN
	}
	# Set this path to your site's directory.
	root * /data/www/yoursiterootfolder
	encode gzip
	@disallowed {
		path /xmlrpc.php
		path /wp-content/uploads/*.php
	}
	rewrite @disallowed /index.php
	# Serve a PHP site through php-fpm
	php_fastcgi unix//run/php-fpm/www.sock
	# Enable the static file server.
	file_server {
		index index.html
	}
	log {
		output file /var/log/caddy/ssl_access.log {
			roll_size 100mb
			roll_keep 3
			roll_keep_for 7d
		}
	}
}

注意替换域名 www.example.com 为你自己的域名,以及网站根目录 /data/www/yoursiterootfolder 为你自定义的路径。同时在此之前,域名的 DNS 也要解析到 Caddy 所在服务器的 IP 地址。

2. 适用于 Typecho 的 Caddy 2 配置

适用于 Typecho 的配置如下:

www.example.com {
	header {
		Strict-Transport-Security "max-age=31536000; preload"
		X-Content-Type-Options nosniff
		X-Frame-Options SAMEORIGIN
	}
	# Set this path to your site's directory.
	root * /data/www/yoursiterootfolder
	encode gzip
	handle_path / {
		try_files {path} {path}/ /index.php/{uri}
	}
	# Serve a PHP site through php-fpm
	php_fastcgi unix//run/php-fpm/www.sock
	# Enable the static file server.
	file_server {
		index index.html
	}
	log {
		output file /var/log/caddy/ssl_access.log {
			roll_size 100mb
			roll_keep 3
			roll_keep_for 7d
		}
	}
}

Typecho 默认是由 index.php 进行静态化路由的,所以try_files {path} {path}/ /index.php/{uri}将网址参数统一由 index.php 重定向。
注意替换域名 www.example.com 为你自己的域名,以及网站根目录 /data/www/yoursiterootfolder 为你自定义的路径。同时在此之前,域名的 DNS 也要解析到 Caddy 所在服务器的 IP 地址。

3. 适用于 YOURLS 的 Caddy 2 配置

适用于 YOURLS 的配置如下:

www.example.com {
	header {
		Strict-Transport-Security "max-age=31536000; preload"
		X-Content-Type-Options nosniff
		X-Frame-Options SAMEORIGIN
	}
	# Set this path to your site's directory.
	root * /data/www/yoursiterootfolder
	encode gzip
	route {
		try_files {path} {path}/index.php /yourls-loader.php
		php_fastcgi unix//run/php-fpm/www.sock {
			split .php
			index index.php
		}
	}
	# Enable the static file server.
	file_server {
		index index.html
	}
	log {
		output file /var/log/caddy/ssl_access.log {
			roll_size 100mb
			roll_keep 3
			roll_keep_for 7d
		}
	}
}

YOURLS 默认是由 yourls-loader.php 进行加载的,所以try_files {path} {path}/index.php /yourls-loader.php将网址参数统一由 yourls-loader.php 尝试重定向。
注意替换域名 www.example.com 为你自己的域名,以及网站根目录 /data/www/yoursiterootfolder 为你自定义的路径。同时在此之前,域名的 DNS 也要解析到 Caddy 所在服务器的 IP 地址。

4. 关于 Caddy 2 发行的证书位置

Caddy 2 最大的优势就是证书一把梭,自动 HTTPS,基本上不让人操心。
其发行的证书默认的存储位置,经过实际使用体验,应该是在如下位置。
Linux 系统

/var/lib/caddy/.local/share/caddy/certificates/acme-v02.api.letsencrypt.org-directory/

Windows 系统

C:\Windows\System32\config\systemprofile\AppData\Roaming\Caddy\certificates\acme-v02.api.letsencrypt.org-directory\

5. 关于 Caddy 2 的 API

Caddy 2 默认可以通过使用地址 localhost:2019RESTAPI 进行 HTTP 访问。除非你知道怎么用,否则还是建议将其禁用。不然 Caddy 2 启动默认会监听 localhost 的 2019 端口。
禁用方法很简单,在 Caddyfile 的全局配置理如下设置。

{
	admin off
}

当然禁用之后,就不能使用 systemctl reload caddy 命令了,因为 Caddy 是通过 API 来加载新配置的。
使用 systemctl restart caddy 命令重启即可。

6. 切换总结

Caddy 的配置文件 Caddyfile 写起来简单,其语法匹配器十分强大,行为明确,比其他 Web Server 的配置少写很多行。
不用操心证书问题,自动 HTTPS 证书申请和续期。妈妈再也不用担心网站的证书过期啦。
至于有人说 Caddy 的缺点就是性能没有 Nginx 高,这个就是仁者见仁智者见智了。
还有个 Caddy 的缺点就是其二进制文件体积比较大,这是 Go 写的软件的通病了,以 v2.6.4 为例,标准模块数量 100 个,占用 46MB。Nginx 是 C 写的,以 v1.25.0 为例,其二进制文件占用 1.6MB,确实小巧。可是这年头,谁还在乎这点硬盘空间呢,你说是吧。

写在最后

请关注我的 Telegram 频道:https://t.me/qiushuiyibing
我会在此不定期发布一些杂七杂八的作品。
同时也欢迎加入交流群:https://t.me/qiushui2018

转载请注明:秋水逸冰 » 从 Apache httpd 切换到 Caddy2