NGINX Websocket代理立即关闭

时间:2018-05-02 13:57:52

标签: nginx websocket

我无法找到解决此问题的方法。其他帖子谈论几分钟后断开连接。在握手期间第一次回复后,我的配置立即断开连接。超时似乎不起作用。错误日志告诉我什么。

为什么连接没有打开?

nginx.conf

user  nginx;
#user root;

error_log  /var/log/nginx/error.log warn;
pid        /var/run/nginx.pid;

worker_processes  2;

events {
    worker_connections  1024;
    multi_accept off;
}


http {
    include   /etc/nginx/mime.types;
    default_type  application/octet-stream;

    server_names_hash_bucket_size 64;

    client_max_body_size 64m;

    log_format  main  '$remote_addr - $remote_user [$time_local] "$request" '
                      '$status $body_bytes_sent "$http_referer" '
                      '"$http_user_agent" "$http_x_forwarded_for"';

    access_log  /var/log/nginx/access.log main buffer=16k;

    sendfile        on;
    tcp_nopush      on;
    tcp_nodelay     on;

    keepalive_timeout  65;
    keepalive_requests 100;

    #gzip  on;

        disable_symlinks off;
        proxy_intercept_errors on;
        #fastcgi_intercept_errors on;

    #############################
    # NICOLAS WEBSOCKET SUPPORT #
    #############################


    upstream websocket {
        server 10.100.14.8:9026;
    }

    server {
        listen 80;

        location /mqtt {
            proxy_pass http://websocket/api/v2/mqtt;
            proxy_http_version 1.1;
            proxy_set_header Upgrade websocket;
            proxy_set_header Connection Upgrade;
            proxy_connect_timeout 5s;
            proxy_read_timeout 20s;
            proxy_send_timeout 20s;
            proxy_buffers 8 32k;
            proxy_buffer_size 64k;
            proxy_redirect off;
            more_set_headers "Sec-WebSocket-Protocol: $http_Sec_WebSocket_Protocol";
            #add_header Sec-WebSocket-Protocol $http_Sec_WebSocket_Protocol;
            #proxy_set_header Sec-WebSocket-Protocol $http_Sec_WebSocket_Protocol;
        }
    }

    #############################


    include /etc/nginx/conf.d/*.conf;
    include /etc/nginx/conf.d/<deleted>;
}

Paho客户端测试页面/ html

<html>
    <head>
        <title>Mqtt client</title>
        <script src="https://cdnjs.cloudflare.com/ajax/libs/paho-mqtt/1.0.1/mqttws31.js" type="text/javascript"></script>
        <script src="https://cdnjs.cloudflare.com/ajax/libs/fingerprintjs2/1.8.0/fingerprint2.min.js" type="text/javascript"></script>
        <script type = "text/javascript" language = "javascript">
            var mqtt;
            var reconnectTimeout = 2000;

            var host = "10.100.14.10"; 
            var port = 80;
            var path = "/mqtt";

            /* Handling Mqtt stuff */
            function onConnect() {
                console.log("Connected ");
            }

            function onFailure(message) {
                console.log("Connection Attempt to Host " + host + "Failed");
                console.log("Error message: " + JSON.stringify(message));
                setTimeout(MQTTconnect, reconnectTimeout);
            }

            function onMessageArrived(msg) {
                out_msg = msg.payloadString;
                out_dest = msg.destinationName;

                console.log(out_msg);
                calculateResult(out_msg);
            }

            function MQTTconnect(fingerprint) {

                console.log("connecting to " + host + " " + port);
                mqtt = new Paho.MQTT.Client(host, port, path, fingerprint);

                var options = {
                    timeout: 3,
                    onSuccess: onConnect,
                    onFailure: onFailure
                };

                mqtt.onMessageArrived = onMessageArrived;
                mqtt.connect(options); //connect
            }

            function subscribe(path) {

                mqtt.subscribe(path);
                console.log("Subscribed to: " + path);

            }

            /* interpret result */
            function calculateResult(msg) {
                obj = JSON.parse(msg);
                console.log(obj);

                document.getElementById("flexi").innerHTML = "<div style='color: green;'>" + obj.hello + "</div>";
                alert("done");
            }

            function load() {

                var that = this;
                new Fingerprint2().get(function (result, components) {
                    console.log(result); 
                    console.log(components); 
                    that.MQTTconnect(result);
                });

            }

            function doYourStuff() {
                var rrn = document.getElementById("rrn").value;
                var path = "/test";
                subscribe(path);
                //callBackend(rrn);
            }

            function simulateResult() {
                var value = document.getElementById("rrn").value;
                var path = "/flexichecker/" + value;

                message = new Paho.MQTT.Message('{ "hello": "world" }');
                message.destinationName = path;
                mqtt.send(message);
            }

        </script>
    </head>
    <body>
        <h1>MQTT websocket</h1>
        <div id="flexi">
            <input id="rrn" name="rrn" type="text" />
            <button onclick="doYourStuff()">Check</button> 
            <button onclick="simulateResult()">Simulate result</button> 
        </div>
        <script>
            load();
        </script>
    </body>
</html>

1 个答案:

答案 0 :(得分:-1)

这是我的nginx配置:

 map $http_upgrade $connection_upgrade {
      default upgrade;
      ''      close;
 }

  upstream pythonserver {

        server 127.0.0.1:8888;

  }
  server{
         listen 80;
         server_name 209.97.139.107;

         location /chatsocket {

              proxy_pass http://pythonserver;
              proxy_redirect off;
              proxy_set_header Host $host;
              proxy_set_header X-Real-IP $remote_addr;
              proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
              proxy_buffers 8 32k;
              proxy_buffer_size 64k;
              proxy_read_timeout 86400s;
              proxy_send_timeout 86400s;
              keepalive_timeout 90;
              proxy_cache off;
              proxy_buffering off;
              proxy_http_version 1.1;
              proxy_set_header Upgrade $http_upgrade;
              proxy_set_header Connection "upgrade";
              include         uwsgi_params;
              uwsgi_pass unix:/home/apideveloper/api/app.sock;

        }
        location / {
              include         uwsgi_params;
              uwsgi_pass unix:/home/apideveloper/api/app.sock;

        }
}

这是正在运行的主机:http://209.97.139.107/