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

推荐订阅源

Blog — PlanetScale
Blog — PlanetScale
SecWiki News
SecWiki News
Google DeepMind News
Google DeepMind News
WordPress大学
WordPress大学
小众软件
小众软件
C
CERT Recently Published Vulnerability Notes
Jina AI
Jina AI
N
Netflix TechBlog - Medium
GbyAI
GbyAI
IT之家
IT之家
Apple Machine Learning Research
Apple Machine Learning Research
AWS News Blog
AWS News Blog
G
GRAHAM CLULEY
L
Lohrmann on Cybersecurity
C
Cybersecurity and Infrastructure Security Agency CISA
I
Intezer
T
Tor Project blog
P
Palo Alto Networks Blog
P
Privacy & Cybersecurity Law Blog
P
Privacy International News Feed
Threat Intelligence Blog | Flashpoint
Threat Intelligence Blog | Flashpoint
P
Proofpoint News Feed
T
Tailwind CSS Blog
C
Check Point Blog
Cloudbric
Cloudbric
Y
Y Combinator Blog
The Last Watchdog
The Last Watchdog
Forbes - Security
Forbes - Security
Last Week in AI
Last Week in AI
S
Security Affairs
博客园 - Franky
F
Fortinet All Blogs
量子位
M
MIT News - Artificial intelligence
C
Cisco Blogs
酷 壳 – CoolShell
酷 壳 – CoolShell
Stack Overflow Blog
Stack Overflow Blog
S
Secure Thoughts
V
Visual Studio Blog
AI
AI
美团技术团队
B
Blog RSS Feed
Application and Cybersecurity Blog
Application and Cybersecurity Blog
博客园 - 三生石上(FineUI控件)
阮一峰的网络日志
阮一峰的网络日志
Engineering at Meta
Engineering at Meta
人人都是产品经理
人人都是产品经理
Microsoft Security Blog
Microsoft Security Blog
T
Threatpost
Cyberwarzone
Cyberwarzone

秋水逸冰

丽萨主机:双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 编译版 | 秋水逸冰 Caddy v1.0.5 编译版 | 秋水逸冰 在 Windows 系统里轻松创建自定义服务 | 秋水逸冰 如何在 CentOS 8 下安装 Docker Engine 在 CentOS 7 下构建 Android 编译环境 使用 Docker Buildx 构建多种系统架构镜像 | 秋水逸冰 使用 Docker 快速部署 Shadowsocks-libev + v2ray-plugin 如何使用 FirewallD 开启 IP 白名单 WireGuard 一键安装脚本 | 秋水逸冰 域名那些事儿5 | 秋水逸冰 告别 | 秋水逸冰 Windows Server 2019/2016/2012R2 Datacenter DD 镜像 如何制作 Windows Server 2019 的 DD 镜像 介绍几款 Docker 镜像 | 秋水逸冰 如何启用 Shadowsocks 的多端口 | 秋水逸冰 一键安装KMS服务脚本 | 秋水逸冰 关于免费SSL证书的那些事儿 | 秋水逸冰 使用定时任务cron监视Shadowsocks进程 | 秋水逸冰 LAMP新增Letsencrypt支持 | 秋水逸冰 从编译到二进制安装 | 秋水逸冰 PHP模块一览及简要说明 | 秋水逸冰 使用 GPG 加密 Github Commits 一键安装最新内核并开启 BBR 脚本 | 秋水逸冰 Shadowsocks 一键安装脚本(四合一) | 秋水逸冰 FTP 上传一键脚本ftp_upload.sh | 秋水逸冰 限制 IP 访问 WordPress 登录画面 CentOS 6.10安装Python2.7.18 | 秋水逸冰 一键备份脚本backup.sh | 秋水逸冰 域名那些事儿4 | 秋水逸冰 域名那些事儿3 | 秋水逸冰 LAMP下SSL设置教程 | 秋水逸冰 L2TP/IPSec一键安装脚本 | 秋水逸冰 域名那些事儿2 | 秋水逸冰 一键测试脚本bench.sh | 秋水逸冰 解决pip安装M2Crypto失败的问题 | 秋水逸冰 在CentOS下编译安装GCC | 秋水逸冰 域名那些事儿 | 秋水逸冰 MySQL性能优化的简单说明 | 秋水逸冰 Zend Guard Loader简介及下载链接一览 | 秋水逸冰 Windows和Linux下Whois命令的安装和使用 | 秋水逸冰 LAMP一键安装脚本使用说明 | 秋水逸冰 Windows系统下安装dig命令 | 秋水逸冰 如何在Debian下安装Webmin | 秋水逸冰 Apache下部署SSL教程 | 秋水逸冰 Shadowsocks-go一键安装脚本 | 秋水逸冰 少壮不努力,一生在内地 | 秋水逸冰 Google日语输入法离线安装包 | 秋水逸冰 PHP加速缓存Zend Opcache | 秋水逸冰 CentOS下安装aria2教程 | 秋水逸冰 Linux多线程下载工具Axel | 秋水逸冰 phpMyAdmin导入文件错误的解决办法 | 秋水逸冰 PHP编译安装常见错误总结 | 秋水逸冰 phpMyAdmin从4.1.x升级到4.2.x注意事项 | 秋水逸冰 Debian下shadowsocks-libev一键安装脚本 | 秋水逸冰 CentOS下shadowsocks-libev一键安装脚本 | 秋水逸冰 又一家VPS:Vultr.com | 秋水逸冰 Linux系统中常用操作命令 | 秋水逸冰 Eclipse下搭建Python开发环境 | 秋水逸冰 Shadowsocks Python版一键安装脚本 | 秋水逸冰 Git初学者:msysgit和tortoisegit | 秋水逸冰 CentOS 6下编译PHP出错 | 秋水逸冰 Apache如何开启Gzip压缩 | 秋水逸冰 Linux Shell逻辑运算符和表达式详解 | 秋水逸冰 吐槽腾讯:PC用户也是用户 | 秋水逸冰 Webmin:如何安装Perl模块DBI和DBD-MySQL | 秋水逸冰 CentOS下如何手动添加Swap | 秋水逸冰 安装PHP的PDO MySQL扩展 | 秋水逸冰 Javascript获取URL参数值 | 秋水逸冰 总结我所用过的VPS商家 | 秋水逸冰 Apache索引(目录浏览)的那些事儿 | 秋水逸冰 如何新建最低权限的SSH帐号 | 秋水逸冰
从 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