<p>I'm setting up https and want to reroute all http traffic to https:</p>
<pre><code>func main() {
http.HandleFunc("/", homeHandler)
http.Handle("/ws", websocket.Handler(EmailEntryServer))
fmt.Println("Serving to :8080 and :8081")
go panic(http.ListenAndServeTLS(":8080", "cert.pem", "key.pem", nil))
panic(http.ListenAndServe(":8081", http.HandlerFunc(
func (w http.ResponseWriter, req *http.Request) {
http.Redirect(w, req, "https://127.0.0.1:8080"+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 "ignore" the port when handling http requests and just forward every request to https? Thanks!</li>
</ol>
<hr/>**评论:**<br/><br/>xrstf: <pre><blockquote>
<p>"If it doesn't load through curl, it's broken."</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'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)
> GET /foo HTTP/1.1
> User-Agent: curl/7.39.0
> Host: localhost:8081
> Accept: */*
>
< HTTP/1.1 301 Moved Permanently
< Location: https://127.0.0.1:8080/foo
< Date: Mon, 18 Sep 2017 19:46:36 GMT
< Content-Length: 61
< Content-Type: text/html; charset=utf-8
<
<a href="https://127.0.0.1:8080/foo">Moved Permanently</a>.
</code></pre></pre>julianxxxx: <pre><p>I'm getting the same output as you now. What'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'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("Connection", "close")
u := r.URL
u.Host = r.Host
u.Scheme = "https"
http.Redirect(w, r, u.String(), http.StatusMovedPermanently)
}
</code></pre></pre>nhooyr: <pre><blockquote>
<p>u := r.URL</p>
</blockquote>
<p>Shouldn'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'm not dereferencing the r, I'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'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
0 回复
- 请尽量让自己的回复能够对别人有帮助
- 支持 Markdown 格式, **粗体**、~~删除线~~、
`单行代码`
- 支持 @ 本站用户;支持表情(输入 : 提示),见 Emoji cheat sheet
- 图片支持拖拽、截图粘贴等方式上传