caddy搭建反向代理
本指南将向您展示如何快速搭建一个生产级的反向代理,无论是否使用 HTTPS。
前提条件:
-
基本的终端/命令行操作技能
-
caddy
命令已在您的系统路径(PATH)中 -
一个正在运行的后端服务,用于代理
本教程假设您有一个后端 HTTP 服务运行在 127.0.0.1:9000
。以下命令适用于 Linux 系统,但相同的原则也适用于其他操作系统。
您可以不使用配置文件来运行一个简单的反向代理,也可以使用配置文件以获得更多的灵活性和控制能力。
命令行
要启动一个从端口 2080 到端口 9000 的明文 HTTP 代理,请运行以下命令:
1 | caddy reverse-proxy --from :2080 --to :9000 |
curl -v 127.0.0.1:2080
1 |
|
:2080reverse_proxy :9000
1 |
|
caddy run
1 |
|
curl -v 127.0.0.1:2080
1 |
|
caddy reverse-proxy --to :9000
1 |
|
curl -v https://localhost
1 |
|
caddy reverse-proxy --from example.com --to :9000
1 |
|
caddy reverse-proxy --from example.com:8443 --to :9000
1 |
|
example.comreverse_proxy :9000
1 | ## 从代理到后端的 HTTPS |
caddy reverse-proxy --from :2080 --to https://localhost:9000
1 |
|
caddy reverse-proxy --from example.com --to https://example.com:9000
1 |
|
caddy reverse-proxy \ –from example.com \ –to https://localhost:9000 \ –change-host-header
默认情况下,Caddy 会将所有 HTTP 头部原封不动地传递,包括 Host
,并且 Caddy 会从 Host
头部中派生 TLS ServerName。--change-host-header
标志会将 Host
头部重置为后端的主机名,以便 TLS 握手能够成功完成。在上面的例子中,它会从 example.com
更改为 localhost:9000
(并且在 TLS 握手中会使用 localhost
)。
