Files
demo/examples/custom_dns_reverse_proxy/README.md
2025-03-14 18:50:49 +00:00

124 lines
2.7 KiB
Markdown
Raw Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

# 自定义DNS反向代理示例
这个示例展示了如何使用 goproxy 创建一个支持自定义 DNS 解析和负载均衡的反向代理服务器。
## 功能特点
- 支持自定义 DNS 解析规则
- 支持多后端服务器负载均衡
- 支持通配符域名解析
- 支持 DNS 缓存
- 支持优雅关闭
- 支持配置文件
## 使用方法
1. 编译示例:
```bash
go build -o custom_dns_proxy main.go
```
2. 运行示例:
```bash
# 使用默认配置
./custom_dns_proxy
# 指定配置文件
./custom_dns_proxy -config config.json
```
## 配置说明
### 命令行参数
- `-addr`: 监听地址,默认为 ":8080"
- `-config`: 配置文件路径,默认为 "config.json"
### 配置文件
配置文件支持 JSON 格式,包含以下主要配置项:
```json
{
"dns": {
"records": {
"api.example.com": [
"192.168.1.1:8080",
"192.168.1.2:8080",
"192.168.1.3:8080"
],
"*.example.com": [
"192.168.1.1:8080",
"192.168.1.2:8080"
]
},
"fallback": true,
"ttl": 300,
"strategy": "round-robin"
},
"proxy": {
"listen_addr": ":8080",
"enable_https": false,
"enable_websocket": true,
"enable_compression": true,
"enable_cors": true,
"preserve_client_ip": true,
"add_x_forwarded_for": true,
"add_x_real_ip": true,
"insecure_skip_verify": false,
"enable_health_check": false,
"health_check_interval": 30,
"health_check_timeout": 5,
"enable_retry": true,
"max_retries": 3,
"retry_backoff": 1,
"max_retry_backoff": 10,
"enable_metrics": true,
"enable_tracing": false,
"websocket_intercept": false,
"dns_cache_ttl": 300,
"enable_cache": true,
"cache_ttl": 300,
"enable_connection_pool": true,
"connection_pool_size": 100,
"idle_timeout": 60,
"request_timeout": 30
}
}
```
## 负载均衡策略
当前示例使用轮询Round Robin负载均衡策略支持以下特性
1. 多后端服务器轮询
2. 自动故障转移
3. 健康检查
4. 连接池管理
## 示例用法
1. 启动代理服务器:
```bash
./custom_dns_proxy -addr :8080
```
2. 使用 curl 测试:
```bash
# 测试 API 访问
curl http://api.example.com:8080/api/v1/users
# 测试通配符域名
curl http://test.example.com:8080/api/v1/users
```
## 注意事项
1. 确保配置文件中的 DNS 记录正确配置
2. 确保后端服务器正常运行
3. 建议在生产环境中启用 HTTPS
4. 根据实际需求调整连接池大小和超时设置