Files
demo/cmd/simple_reverse_proxy/simple_reverse_proxy.go
2025-03-15 10:17:07 +00:00

41 lines
999 B
Go
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.

package main
import (
"fmt"
"log"
"net/http"
"github.com/darkit/goproxy"
"github.com/darkit/goproxy/config"
)
func main() {
// 创建基本配置
cfg := config.DefaultConfig()
// 配置反向代理
cfg.ReverseProxy = true // 启用反向代理模式
cfg.ListenAddr = ":8080" // 监听本地8080端口
cfg.TargetAddr = "http://example.com" // 转发到example.com
// 设置HTTP头部选项
cfg.PreserveClientIP = true // 保留客户端IP
cfg.AddXForwardedFor = true // 添加X-Forwarded-For头
cfg.AddXRealIP = true // 添加X-Real-IP头
// 设置性能选项
cfg.EnableCompression = true // 启用压缩
cfg.EnableCache = true // 启用缓存
// 创建代理实例
proxy := goproxy.New(&goproxy.Options{
Config: cfg,
})
// 启动服务器
fmt.Println("反向代理启动在 :8080转发到 http://example.com")
if err := http.ListenAndServe(":8080", proxy); err != nil {
log.Fatalf("服务器启动失败: %v", err)
}
}