【发布时间】:2018-05-23 20:30:28
【问题描述】:
我正在使用 Node.js 和 NGINX 提供应用程序。我正在使用 LetsEncrypt 保护 NGINX,并使用 pm2 在服务器上运行我的节点应用程序(使用 NGINX 作为反向代理)。
我的网站不会加载任何内容(426 错误 - 需要升级),但我可以使用以下暂存器进行连接:
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
我读到应该有一个“升级”标题,这是问题的一部分吗?
【问题讨论】:
-
可能需要改一下:proxy_http_version 1.1;到更高的地方?
-
这个好像是推荐版本@nginx官方(只有1.0和1.1):nginx.org/en/docs/http/…
-
你的nodejs服务器发送的http版本是什么?