update
This commit is contained in:
40
cmd/simple_reverse_proxy/simple_reverse_proxy.go
Normal file
40
cmd/simple_reverse_proxy/simple_reverse_proxy.go
Normal file
@@ -0,0 +1,40 @@
|
||||
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)
|
||||
}
|
||||
}
|
Reference in New Issue
Block a user