http.Redirect won't direct to port 8080?

agolangf · · 665 次点击    
这是一个分享于 的资源,其中的信息可能已经有所发展或是发生改变。
<p>I&#39;m setting up https and want to reroute all http traffic to https:</p> <pre><code>func main() { http.HandleFunc(&#34;/&#34;, homeHandler) http.Handle(&#34;/ws&#34;, websocket.Handler(EmailEntryServer)) fmt.Println(&#34;Serving to :8080 and :8081&#34;) go panic(http.ListenAndServeTLS(&#34;:8080&#34;, &#34;cert.pem&#34;, &#34;key.pem&#34;, nil)) panic(http.ListenAndServe(&#34;:8081&#34;, http.HandlerFunc( func (w http.ResponseWriter, req *http.Request) { http.Redirect(w, req, &#34;https://127.0.0.1:8080&#34;+req.RequestURI, http.StatusMovedPermanently) } ))) } </code></pre> <ol> <li>my http requests to 8081 are successfully redirected - BUT only to <code>https://localhost</code> , without port 8080. </li> <li>In production, how can I &#34;ignore&#34; the port when handling http requests and just forward every request to https? Thanks!</li> </ol> <hr/>**评论:**<br/><br/>xrstf: <pre><blockquote> <p>&#34;If it doesn&#39;t load through curl, it&#39;s broken.&#34;</p> <p>Some very wise person.</p> </blockquote> <p>Can you show us the output of <code>curl -v http://localhost:8081/idontexist</code>?</p></pre>julianxxxx: <pre><pre><code>* Trying ::1... * TCP_NODELAY set * Connection failed * connect to ::1 port 8081 failed: Connection refused * Trying 127.0.0.1... * TCP_NODELAY set * Connection failed * connect to 127.0.0.1 port 8081 failed: Connection refused * Failed to connect to localhost port 8081: Connection refused * Closing connection 0 curl: (7) Failed to connect to localhost port 8081: Connection refused` </code></pre> <p>That&#39;s output. I can see that something is redirecting, because If I test with <code>localhost:8080</code> it redirects to <code>https://127.0.0.1</code>...</p></pre>xrstf: <pre><blockquote> <p>my http requests to 8081 are successfully redirected - BUT only to https://localhost , without port 8080.</p> </blockquote> <p>vs.</p> <blockquote> <p>localhost port 8081: Connection refused</p> </blockquote> <p>Sounds like some things changed between your initial code sample and your curl test. If I compile your sample (without the TLS listener, because I had no cert at hand), I get</p> <pre><code>$ curl -v http://localhost:8081/foo * timeout on name lookup is not supported * Hostname was NOT found in DNS cache * Trying 127.0.0.1... * Connected to localhost (127.0.0.1) port 8081 (#0) &gt; GET /foo HTTP/1.1 &gt; User-Agent: curl/7.39.0 &gt; Host: localhost:8081 &gt; Accept: */* &gt; &lt; HTTP/1.1 301 Moved Permanently &lt; Location: https://127.0.0.1:8080/foo &lt; Date: Mon, 18 Sep 2017 19:46:36 GMT &lt; Content-Length: 61 &lt; Content-Type: text/html; charset=utf-8 &lt; &lt;a href=&#34;https://127.0.0.1:8080/foo&#34;&gt;Moved Permanently&lt;/a&gt;. </code></pre></pre>julianxxxx: <pre><p>I&#39;m getting the same output as you now. What&#39;s very strange is that my curl request gets picked up from the redirect function and prints the host connecting (just a test), if I do the same and type <code>http://localhost:8081</code> in the address bar, the request doesn&#39;t even respond! Any ideas why?</p></pre>shovelpost: <pre><p>Try this:</p> <pre><code>func redirectHTTP(w http.ResponseWriter, r *http.Request) { w.Header().Set(&#34;Connection&#34;, &#34;close&#34;) u := r.URL u.Host = r.Host u.Scheme = &#34;https&#34; http.Redirect(w, r, u.String(), http.StatusMovedPermanently) } </code></pre></pre>nhooyr: <pre><blockquote> <p>u := r.URL</p> </blockquote> <p>Shouldn&#39;t that be</p> <pre><code>u := *r.URL </code></pre></pre>ChristophBerger: <pre><p>There is no need to dereference <code>r</code> first. The dot notation <a href="https://golang.org/ref/spec#Selectors" rel="nofollow">works transparently with pointers, too</a>. That is, <code>r.URL</code> is a shorthand for <code>(*r).URL</code>.</p></pre>nhooyr: <pre><p>I&#39;m not dereferencing the r, I&#39;m dereferencing r.URL.</p> <p>See <a href="https://play.golang.org/p/POTLkz0Afk" rel="nofollow">https://play.golang.org/p/POTLkz0Afk</a> </p></pre>ChristophBerger: <pre><p>Ok, but whichever pointer gets dereferenced in this context, the result is the same as using <code>r.URL</code>: <a href="https://play.golang.org/p/hXDD4Dj5CN" rel="nofollow">https://play.golang.org/p/hXDD4Dj5CN</a></p></pre>nhooyr: <pre><p>While that&#39;s true, see <a href="https://golang.org/pkg/net/http/#Handler" rel="nofollow">https://golang.org/pkg/net/http/#Handler</a></p> <blockquote> <p>Except for reading the body, handlers should not modify the provided Request.</p> </blockquote></pre>ChristophBerger: <pre><p>Oh, ok, now I know what you were after in your original comment! Sorry, I was on the wrong track all the time. You are right, the request header should remain unchanged.</p></pre>

入群交流(和以上内容无关):加入Go大咖交流群,或添加微信:liuxiaoyan-s 备注:入群;或加QQ群:692541889

665 次点击  
加入收藏 微博
0 回复
暂无回复
添加一条新回复 (您需要 登录 后才能回复 没有账号 ?)
  • 请尽量让自己的回复能够对别人有帮助
  • 支持 Markdown 格式, **粗体**、~~删除线~~、`单行代码`
  • 支持 @ 本站用户;支持表情(输入 : 提示),见 Emoji cheat sheet
  • 图片支持拖拽、截图粘贴等方式上传