Node,ws,ssl,nginx giving error 426,upgrade required

nkkqxpd9  于 2023-05-28  发布在  Nginx
关注(0)|答案(2)|浏览(351)

我正在使用Node.js和NGINX服务一个应用程序。我正在使用LetsEncrypt保护NGINX,并使用pm2在服务器上运行我的节点应用程序(使用NGINX作为反向代理)。
我的网站将不会加载任何东西(426错误-需要升级),但我可以连接以下scratchpad:

var port = 443;
var ws = new WebSocket("wss://mywebsite.com:" + port);

ws.onopen = function() {
  console.log("Connected");
}

ws.onmessage = function(comment) {
  console.log(JSON.parse(comment.data));
}

下面是NGINX的设置:

server {

        root /var/www/html;

        # Add index.php to the list if you are using PHP
        index index.html index.htm index.nginx-debian.html;

        server_name mywebsite.com www.mywebsite.com;

        location / {
                proxy_pass http://127.0.0.1:8080/;
                proxy_http_version 1.1;
                proxy_set_header Upgrade $http_upgrade;
                proxy_set_header Connection "upgrade";
    }

    listen [::]:443 ssl ipv6only=on; # managed by Certbot
    listen 443 ssl; # managed by Certbot
    ssl_certificate /path/to/cert; # managed by Certbot
    ssl_certificate_key /path/to/key; # managed by Certbot
    include /etc/letsencrypt/options-ssl-nginx.conf; # managed by Certbot
    ssl_dhparam /etc/letsencrypt/ssl-dhparams.pem; # managed by Certbot

}
server {
    if ($host = www.mywebsite.com) {
        return 301 https://$host$request_uri;
    } # managed by Certbot

    if ($host = mywebsite.com) {
        return 301 https://$host$request_uri;
    } # managed by Certbot

        listen 80 default_server;
        listen [::]:80 default_server;

        server_name mywebsite.com www.mywebsite.com;
    return 404; # managed by Certbot
}

我的客户端代码基本上和便签本一样。下面是相关的服务器端代码:

var WebSocket = require('ws');
var serverPort = 8080;
var wss = new WebSocket.Server({port:serverPort});
console.log("Server running on port " + serverPort + " started at: " + new Date());

wss.on('connection', function(ws) {

        console.log("Connected to websocket: " + ws);
        var introComment = JSON.stringify({
                user: "Welcome!",
                data: {
                        body: "Welcome to the realtime feed!",
                        name: "realtime-intro-connection-message",
                },

        });
        ws.send(introComment);
});

以下是浏览器接收的响应头:

HTTP/1.1 426 Upgrade Required
Server: nginx/1.10.3 (Ubuntu)
Date: Wed, 23 May 2018 19:20:36 GMT
Content-Type: text/plain
Content-Length: 16
Connection: keep-alive

我读到应该有一个“升级”标题,这是问题的一部分吗?

ds97pgxw

ds97pgxw1#

要将客户端和服务器之间的连接从HTTP/1.1转换为WebSocket,需要使用HTTP/1.1中可用的协议切换机制。
但有一个微妙之处:因为“升级”是逐跳报头,所以它不从客户端传递到代理服务器。通过前向代理,客户端可以使用CONNECT方法来规避此问题。然而,这不适用于反向代理,因为客户端不知道任何代理服务器,并且需要在代理服务器上进行特殊处理。
从1.3.13版本开始,nginx实现了一种特殊的操作模式,允许在客户端和代理服务器之间建立一个隧道,如果代理服务器返回一个带有代码101(切换协议)的响应,并且客户端通过请求中的“升级”报头请求协议切换。
如上所述,包括“升级”和“连接”的逐跳报头不会从客户端传递到代理服务器,因此为了使代理服务器了解客户端将协议切换到WebSocket的意图,必须显式传递这些报头:

location /chat/ {
        proxy_pass http://backend;
        proxy_http_version 1.1;
        proxy_set_header Upgrade $http_upgrade;
        proxy_set_header Connection "upgrade";
    }

一个更复杂的示例,其中对代理服务器的请求中的“连接”报头字段的值取决于客户端请求报头中的“升级”字段的存在:

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

    server {
        ...

        location /chat/ {
            proxy_pass http://backend;
            proxy_http_version 1.1;
            proxy_set_header Upgrade $http_upgrade;
            proxy_set_header Connection $connection_upgrade;
        }
    }
}
bvjveswy

bvjveswy2#

我从来没有真正找到答案,所以我改变了策略:
我从ws(npm的websockets)转移到了socket.io。这似乎得到了更广泛的支持。有关使用socket.io的示例应用程序,请观看这些精彩的视频here!现在一切正常。

相关问题