使用Golang开发的简单反向代理工具, 支持HTTP, HTTPS, WebSocket协议.
应用场景为本地开发调试, 安全性和可靠性未测试, 尽量避免在生产环境中使用.
配置文件使用 YAML 格式, 默认路径为 ./config.yml, 可通过 -config 参数指定.
| 参数 | 说明 |
|---|---|
-config <path> | 指定配置文件路径, 默认 ./config.yml |
-version | 显示版本号 |
| 字段 | 类型 | 默认值 | 说明 |
|---|---|---|---|
http.enabled | bool | true | 是否开启 HTTP 监听 |
http.port | int | 80 | HTTP 监听端口 |
http.bind | string | 0.0.0.0 | HTTP 绑定地址 |
https.enabled | bool | false | 是否开启 HTTPS 监听 |
https.port | int | 443 | HTTPS 监听端口 |
https.bind | string | 0.0.0.0 | HTTPS 绑定地址 |
log.level | string | info | 日志级别: debug / info / warn / error |
log.file | string | "" | 日志文件路径, 空值输出到控制台 |
每个站点是一个独立的代理规则, 通过请求的 Host 头进行匹配.
| 字段 | 类型 | 必填 | 默认值 | 说明 |
|---|---|---|---|---|
name | string | 是 | - | 站点唯一标识名称 |
domain | string | 是 | - | 匹配的域名 (不含端口) , 如 www.example.com |
enabled | bool | 否 | true | 是否启用该站点, 设为 false 时跳过加载和校验 |
protocols.http | bool | 否 | true | 是否允许 HTTP 代理 |
protocols.https | bool | 否 | false | 是否允许 HTTPS 代理 |
protocols.ws | bool | 否 | false | 是否允许 WebSocket 代理 |
protocols.wss | bool | 否 | false | 是否允许加密 WebSocket 代理 |
tls.cert_file | string | 条件 | - | TLS 证书 PEM 文件路径 (开启 HTTPS/WSS 时必填) |
tls.key_file | string | 条件 | - | TLS 密钥文件路径 (开启 HTTPS/WSS 时必填) |
tls.verify_cert | bool | 否 | true | 启动时校验证书 CN/SAN 是否包含目标域名, 支持通配符 |
backend.target | string | 是 | - | 后端服务地址, 如 http://127.0.0.1:3000 |
backend.timeout | int | 否 | 30 | 后端请求超时时间 (秒) |
| 字段 | 类型 | 必填 | 说明 |
|---|---|---|---|
headers.set | map | 否 | 向请求注入的自定义头, 支持变量 $remote_addr, $host |
headers.hide | list | 否 | 需要从响应中移除的头名称列表 |
global:
http:
enabled: true
port: 80
bind: "0.0.0.0"
https:
enabled: true
port: 443
bind: "0.0.0.0"
log:
level: "info"
file: ""
sites:
# 示例站点
- name: "my-site"
domain: "www.example.com"
enabled: true
protocols:
http: true
https: true
ws: false
wss: false
tls:
cert_file: "./certs/example.com.pem"
key_file: "./certs/example.com.key"
verify_cert: true
backend:
target: "http://127.0.0.1:3000"
timeout: 30
headers:
set:
X-Proxy-By: "Go-Reverse-Proxy"
X-Forwarded-For: "$remote_addr"
hide:
- "X-Powered-By"
# 编译
go build -o go-reverse-proxy .
# 使用默认配置启动
./go-reverse-proxy
# 指定配置文件启动
./go-reverse-proxy -config /etc/proxy/config.yml
*.example.com 可匹配 www.example.com, api.example.comenabled: false) 不会被校验, 可保留不完整的配置供后续启用