Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Unable to get go.net/websocket working behind nginx

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>`))
like image 262
Chandra Sekar Avatar asked Feb 28 '14 09:02

Chandra Sekar


People also ask

Does Nginx work with WebSockets?

NGINX supports WebSocket by allowing a tunnel to be set up between both client and back-end servers. NGINX will send the Upgrade request from the client to the back-end server, the Upgrade and Connection headers must be set explicitly. Once this is done, NGINX deals with this as a WebSocket connection.

How do I fix WebSocket connection failed?

Solution 1Check that all the Bot Insight services are running. Check that your firewall settings are configured to accept incoming websocket data. Try to use a different web browser. Restart the Bot Insight Visualization and Bot Insight Scheduler services.

Can you proxy WebSockets?

WebSocket communication can take successfully take place in the presence of forward proxies, providing the client and proxy server have been configured properly to deal with it. This page explains how to configure a Universal Messaging JavaScript client and Apache serving as a forward proxy to permit WebSocket use.

What does Proxy_set_header upgrade $Http_upgrade do?

proxy_set_header Upgrade $http_upgrade; Is actually doing what you want because $http_upgrade comes from the header sent by the client. So if the client doesn't request an upgrade, it doesn't get passed along. Save this answer.


1 Answers

This is the configuration that I'm using, it's working OK:

server {
    listen 0.0.0.0:20007;    
    index index.html;

     root /full/path/to/site;

    # pass the request to the node.js server with the correct headers 
    location / {

      proxy_set_header X-Real-IP $remote_addr;
      proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
      proxy_set_header Host $http_host;
      proxy_set_header X-NginX-Proxy true;

      proxy_pass http://your_app/;
      proxy_redirect off;

      proxy_http_version 1.1;
      proxy_set_header Upgrade $http_upgrade;
      proxy_set_header Connection "upgrade";
    }
 }
like image 54
Angular University Avatar answered Oct 18 '22 21:10

Angular University