rust_http_proxy 支持灵活配置反向代理路由规则,正向代理和反向代理均支持 websocket。
使用 --location-config-file 配置反向代理规则:
# 针对特定域名
api.example.com:
- location: /api
basic_auth_users: # 可选;不配置 basic_auth_path_prefixes 时保护整个 /api
- alice:alice_password
basic_auth_path_prefixes: # 可选;相对当前 location 的路径前缀
- /private
upstream:
url_base: "https://backend.internal.com/api/" # 上游 scheme 和路径
connect_to: "10.0.0.8:8443" # 可选:DNS/TCP 连接目标
tls_server_name: "backend.internal.com" # 可选:TLS SNI 和证书校验名
authority: "#{host}" # 可选:H1 Host / H2 :authority
version: "AUTO" # HTTP 版本: H1/H2/AUTO
headers: # 可选:修改发送给上游的请求头
X-Custom-Header: "custom_value"反向代理 location 的 basic_auth_users 和 basic_auth_path_prefixes 语义与静态文件托管一致。认证使用客户端请求的 Authorization: Basic ...;对于命中受保护路径的请求,该请求头在认证后会被移除,不会透传给 upstream。未启用认证或未命中受保护路径时,保持原有请求头透传行为。
路由和认证前会解码路径中的百分号编码非保留字符,例如 /api/%70rivate 按 /api/private 处理。./.. 路径段、重复分隔符、反斜线和编码后的分隔符等歧义路径返回 400,不会连接 upstream。查询参数保持原样。/https://host/path 便捷代理中的协议分隔符保留。
配置的 location 应使用规范路径(例如 /api/,而非 /%61pi/ 或 /./api/),保留的百分号编码统一使用大写十六进制,否则启动失败。请求路径中的小写十六进制编码会自动规范化。认证用户列表中任何格式错误的条目也会导致启动失败。
上游 URL 通过替换 url_base 的路径构建:移除请求的 location 前缀,将剩余路径与查询参数追加到基础路径,必要时补齐开头的 /。请求后缀不会改变配置的主机、端口或 TLS SNI。例如 /api/item?q=1 配合 location: /api/ 和 url_base: https://backend.example/base/,转发到 https://backend.example/base/item?q=1。
| 参数 | 说明 | 默认值/可选值 |
|---|---|---|
url_base |
上游 scheme、默认主机与基础路径 | 任意有效 URL |
connect_to |
DNS/TCP 实际连接目标;必须是静态域名或 IP | url_base 的 authority(自动补 80/443) |
tls_server_name |
TLS SNI 与证书校验名;必须是静态 DNS 名或 IP | url_base 的 host |
authority |
HTTP 虚拟主机:H1 写入 Host,H2 写入 :authority |
url_base 的 authority,支持 #{host} |
version |
HTTP 协议版本 | H1、H2、AUTO(默认) |
headers |
覆盖/添加发送给上游的请求头 | 键值对,支持 #{host} |
H1 和 H2 使用彼此独立且严格限定协议的客户端。HTTPS upstream 的 AUTO 沿用入口请求版本后再分流,明文 HTTP upstream 则使用 H1;若对 http:// upstream 显式指定 H2,使用的是 h2c prior knowledge,上游必须直接接受明文 HTTP/2。H2 不发送普通 Host,但会把 authority 写入 :authority,因此仍支持独立于连接目标和 TLS SNI 的虚拟主机。为兼容旧配置,未设置 authority 时,headers.Host 会作为 authority 使用。
connect_to 和 tls_server_name 不接受 #{host},避免外部请求的 Host 控制代理的 DNS/TCP 或 TLS 目标。需要动态虚拟主机时只配置 authority: "#{host}"(或兼容写法 headers.Host: "#{host}");连接目标与证书校验名应保持静态。
#{host} 会保留原请求的非默认端口,但会省略与原请求 scheme 匹配的默认端口(HTTP 的 80、HTTPS 的 443)。H2 使用解析后的值生成 :authority,不会额外发送普通 Host 请求头。
在国内无法访问 GitHub 时,可启用 GitHub 代理功能。通过在原始 URL 前添加 https://YOUR_DOMAIN 访问:
# 启动时添加参数
rust_http_proxy -p 7788 --enable-github-proxy支持代理的 GitHub 域名:
raw.githubusercontent.comgithub.comgist.githubusercontent.comgist.github.comrelease-assets.githubusercontent.comobjects.githubusercontent.com
使用示例:
# 原始地址
https://raw.githubusercontent.com/user/repo/main/file.txt
# 代理后地址
https://YOUR_DOMAIN/https://raw.githubusercontent.com/user/repo/main/file.txt通过 --append-upstream-url 快速配置反向代理:
rust_http_proxy -p 7788 --append-upstream-url=https://cdnjs.cloudflare.com访问方式:
https://YOUR_DOMAIN/https://cdnjs.cloudflare.com/ajax/libs/jquery/3.6.0/jquery.min.js
等价于以下 YAML 配置:
default_host:
- location: /https://cdnjs.cloudflare.com
upstream:
url_base: https://cdnjs.cloudflare.com
version: AUTO