<p>I am a bit new to Go and understand what a reverse proxy is but when is it appropriate to use it vs a standard request and get the url that way? Specifically, when should I use <a href="https://golang.org/pkg/net/http/httputil/#ReverseProxy" rel="nofollow">https://golang.org/pkg/net/http/httputil/#ReverseProxy</a>?</p>
<p>In my case I am just retrieving a url for a user - no credentials are being passed, no headers being forwarded, etc. (Also, I have only a single server I am requesting resources from so no need for load balancing, etc). Are there any benefits of using a reverse proxy in this case? </p>
<hr/>**评论:**<br/><br/>9nut: <pre><p>The <em>Client</em> type in http package is all that's needed. There is a full example in the http package:
<a href="https://golang.org/pkg/net/http/#example_Get" rel="nofollow">https://golang.org/pkg/net/http/#example_Get</a></p>
<p>For more insight, check the source code for <em>Get</em> function in http package; you'll notice it uses the <em>DefaultClient</em> which is an instance of <em>Client</em></p></pre>carsncode: <pre><p>I think you may misunderstand the purpose of a <a href="https://en.wikipedia.org/wiki/Reverse_proxy" rel="nofollow">reverse proxy</a> - if you're on the client end, you're not talking about a reverse proxy at all, which sits at the server end. You're talking about a normal forward proxy. Load balancing, etc would generally be handled by a reverse proxy on the server side, not by anything on the client side.</p>
<p>If you're receiving HTTP requests and forwarding them somewhere else, then you are acting as a proxy. If requests are initiated some other way (e.g., user runs your program, which makes an HTTP call; or you receive an HTTP request, and in the course of some processing, you must make a call out), then you are not acting as a proxy, you are acting as the client.</p></pre>WikiTextBot: <pre><p><strong>Reverse proxy</strong></p>
<p>In computer networks, a reverse proxy is a type of proxy server that retrieves resources on behalf of a client from one or more servers. These resources are then returned to the client like they originated from the Web server itself. Contrary to a forward proxy, which is an intermediary for its associated clients to contact any server, a reverse proxy is an intermediary for its associated servers to be contacted by any client.</p>
<p>Quite often, popular web servers use reverse-proxying functionality, shielding application frameworks of weaker HTTP capabilities.</p>
<hr/>
<p><sup>[</sup> <a href="https://www.reddit.com/message/compose?to=kittens_from_space" rel="nofollow"><sup>PM</sup></a> <sup>|</sup> <a href="https://reddit.com/message/compose?to=WikiTextBot&message=Excludeme&subject=Excludeme" rel="nofollow"><sup>Exclude</sup> <sup>me</sup></a> <sup>|</sup> <a href="https://np.reddit.com/r/golang/about/banned" rel="nofollow"><sup>Exclude</sup> <sup>from</sup> <sup>subreddit</sup></a> <sup>|</sup> <a href="https://np.reddit.com/r/WikiTextBot/wiki/index" rel="nofollow"><sup>FAQ</sup> <sup>/</sup> <sup>Information</sup></a> <sup>|</sup> <a href="https://github.com/kittenswolf/WikiTextBot" rel="nofollow"><sup>Source</sup></a> <sup>]</sup>
<sup>Downvote</sup> <sup>to</sup> <sup>remove</sup> <sup>|</sup> <sup>v0.24</sup></p></pre>adamtanner: <pre><p>What are you returning to the user for the URL requested on their behalf? Are you generating the request to the downstream server or passing along the request you received?</p>
<p>If you're passing the unmodified/slightly modified request you received directly to the downstream server and returning the unmodified/slightly modified response you probably want a reverse proxy. If it's something else then probably not.</p></pre>notsoobadusername: <pre><p>It's just a .jpg image. Returning the image and nothing else.</p></pre>adamtanner: <pre><p>Okay, then it's probably sufficient to do something like the following in your handler:</p>
<pre><code>res, err := http.Get("http://example.com/image.jpg")
if err != nil { ... }
defer res.Body.Close()
io.Copy(rw, res.Body)
</code></pre></pre>mcouturier: <pre><p>I would still use a reverse proxy.</p>
<p>You get the expiring headers, content type and such. You'll also get the return code for free in case of a 404...</p>
<p>Unless you want a completely new set of headers.</p></pre>a_k_w: <pre><p>reverse proxy can be used as a load balancer, cache, validator/hardener, gateway.</p></pre>
这是一个分享于 的资源,其中的信息可能已经有所发展或是发生改变。
入群交流(和以上内容无关):加入Go大咖交流群,或添加微信:liuxiaoyan-s 备注:入群;或加QQ群:692541889
- 请尽量让自己的回复能够对别人有帮助
- 支持 Markdown 格式, **粗体**、~~删除线~~、
`单行代码`
- 支持 @ 本站用户;支持表情(输入 : 提示),见 Emoji cheat sheet
- 图片支持拖拽、截图粘贴等方式上传