go - 如何使用 NewSingleHostReverseProxy() 停止在 Golang 的 ReverseProxy 中显示目标 URL?
问题描述
我正在尝试在 Golang 中使用反向代理,但我无法阻止目标 ip 在浏览器中显示,即。它只是重定向到维基百科(目标)而不是显示做反向代理。谁能告诉我做错了什么?
package main
import (
"net/http"
"net/http/httputil"
"net/url"
"github.com/gorilla/mux"
)
func main() {
target := "https://www.wikipedia.org"
remote, err := url.Parse(target)
if err != nil {
panic(err)
}
proxy := httputil.NewSingleHostReverseProxy(remote)
r := mux.NewRouter()
r.HandleFunc("/forward/{rest:.*}", handler(remote, proxy))
http.ListenAndServe(":8080", r)
}
func handler(ur *url.URL, p *httputil.ReverseProxy) func(http.ResponseWriter, *http.Request) {
return func(w http.ResponseWriter, r *http.Request) {
r.URL.Host = ur.Host
r.URL.Scheme = ur.Scheme
r.Host = ur.Host
r.URL.Path = mux.Vars(r)["rest"]
p.ServeHTTP(w, r)
}
}
解决方案
推荐阅读
- c# - 从 Razor 页面更新数据库行并返回成功/失败的简单 JSON 请求
- r - 使用一个表中的多个列和另一个表中的单个列更新值
- java - hashcode() 会为连接返回相同的哈希值吗?
- php - 使用另一个查询按变量排序?
- apache-spark - 如何从pysark中的文件中匹配/提取多行模式
- laravel - 如何使用 Composer 存储库进行 Laravel 框架开发
- python - 具有密集层的 TensorFlow 激活函数
- python - 连接来自同一物种名称的两个文件的 DNA 序列
- javascript - Styled Components / React - 外部元素的样式
- python - 如何禁用 TKinter 滚动条小部件?