I'm having trouble getting go.net/websocket to work behind nginx. It works if the application is accessed directly but with nginx, I get an EOF error from Receive.
What am I doing wrong?
Nginx version: 1.5.10
This is my nginx configuration.
location /wstest/ {
proxy_pass http://localhost:7415/;
proxy_http_version 1.1;
proxy_set_header Upgrade "websocket";
proxy_set_header Connection "Upgrade";
proxy_buffering off;
}
Go code:
func main() {
http.HandleFunc("/", home)
http.Handle("/sock", websocket.Handler(pingpong))
http.ListenAndServe(":7415", nil)
}
func home(w http.ResponseWriter, r *http.Request) {
homeTmpl.Execute(w, nil)
}
func pingpong(conn *websocket.Conn) {
var msg string
if err := websocket.Message.Receive(conn, &msg); err != nil {
log.Println("Error while receiving message:", err)
return
}
if msg == "ping" {
websocket.Message.Send(conn, "pong")
}
}
var homeTmpl = template.Must(template.New("home").Parse(`
<!doctype html>
<html>
<head>
<title>WS Test</title>
<script>
var path = window.location.pathname;
var wsURL = "ws://" + window.location.host + path.substring(0, path.lastIndexOf('/')) + "/sock";
var ws;
document.addEventListener("DOMContentLoaded", function() {
ws = new WebSocket(wsURL);
ws.onopen = function() {
ws.send("ping");
};
ws.onmessage = function(event) {
document.getElementById("status").innerHTML = "Received: " + String(event.data);
};
})
</script>
</head>
<body>
<span id="status">Pinging...</span>
</body>
</html>`))