dongzanxun2790 2014-01-10 23:18
浏览 75
已采纳

Go中的虚拟主机

Is there a way to handle listening for multiple Go web applications on a single port (80, for example). I am aware of ServeMux and the ability to listen for different incoming host names, but in this method they must be handled in the same program, and thus the same binary.

Would the best method be to listen for hostnames on :80 in one binary and then send the requests/response writers to another corresponding binary somewhere else? Would I use "os/exec" for this? How would you pass in the Request and ResponseWriter parameters to this external binary? Thanks in advance!

EDIT:

Is it possible for goroutines of different binary origin to access each others channels? That would be a cool way to do it.

  • 写回答

1条回答 默认 最新

  • dpowhyh70416 2014-01-11 00:16
    关注

    The usual method for doing this would be to use a reverse proxy that directs requests to relevant app servers (usually running on a different port or different machine) based on the host name in the request.

    A common approach is to use Apache for this, but if you want to do it from Go, the ReverseProxy type from the net/http/httputil package should help.

    httputil.NewSingleHostReverseProxy(baseurl) will give you an HTTP handler that proxies requests through to another web site and returns the results. So you could implement your front end via a multiplexing HTTP handler that directs requests to one of a number of ReverseProxy handlers based on the requested host name.

    If you need more complicated routing than NewSingleHostReverseProxy gives you, you can use a custom Director function when creating the proxy handler.

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 高德地图点聚合中Marker的位置无法实时更新
  • ¥15 DIFY API Endpoint 问题。
  • ¥20 sub地址DHCP问题
  • ¥15 delta降尺度计算的一些细节,有偿
  • ¥15 Arduino红外遥控代码有问题
  • ¥15 数值计算离散正交多项式
  • ¥30 数值计算均差系数编程
  • ¥15 redis-full-check比较 两个集群的数据出错
  • ¥15 Matlab编程问题
  • ¥15 训练的多模态特征融合模型准确度很低怎么办