dti3914 2017-08-18 05:59
浏览 37

在go中构建完整的HTTP反向代理需要什么?

A naive reverse proxy is like this:

package main

import (
    "net/http"
    "net/http/httputil"
    "net/url"
    "fmt"
)

func main() { 
    // New functionality written in Go
    http.HandleFunc("/new", func(w http.ResponseWriter, r *http.Request) {
        fmt.Fprint(w, "New function")
    })

    // Anything we don't do in Go, we pass to the old platform
    u, _ := url.Parse("http://www.google.com/")
    http.Handle("/", httputil.NewSingleHostReverseProxy(u))

    // Start the server
    http.ListenAndServe(":8080", nil)
}

But, this is incomplete. Depending on the website you might not get anything useful back. Some do https redirect. Some complain about direct ip access. I suspect virtual hosts don't work? not sure.

What does a true reverse proxy do that makes it complete?

  • 写回答

1条回答 默认 最新

  • dongzhan1948 2017-08-18 06:59
    关注

    The simplest way to implement a reverse HTTP proxy in Go is with the httputil.ReverseProxy type in the standard library.

    This gives you the flexibility to set a Director function which can modify the incoming requests, and a Transport to possibly modify requests and/or responses on-the-fly.

    It should be able to handle the vast majority of reverse proxy situations. I use it with great success in a project of mine.

    评论

报告相同问题?

悬赏问题

  • ¥100 set_link_state
  • ¥15 虚幻5 UE美术毛发渲染
  • ¥15 CVRP 图论 物流运输优化
  • ¥15 Tableau online 嵌入ppt失败
  • ¥100 支付宝网页转账系统不识别账号
  • ¥15 基于单片机的靶位控制系统
  • ¥15 真我手机蓝牙传输进度消息被关闭了,怎么打开?(关键词-消息通知)
  • ¥15 装 pytorch 的时候出了好多问题,遇到这种情况怎么处理?
  • ¥20 IOS游览器某宝手机网页版自动立即购买JavaScript脚本
  • ¥15 手机接入宽带网线,如何释放宽带全部速度