在本地它运行正常,但是当我将它放在 Nginx 反向代理后面时,我在 Chrome 开发者控制台中得到了这个:
GET https://127.0.0.1:8443/socket.io/?EIO=3&transport=polling&t=MoHVP61 net::ERR_SSL_PROTOCOL_ERROR
Nginx 配置:
upstream io_nodes {
ip_hash;
server 127.0.0.1:8443;
}
map $sent_http_content_type $expires {
default off;
text/html epoch;
text/css max;
application/javascript max;
~image/ max;
}
server {
listen 80;
listen [::]:80;
listen 443 ssl http2;
listen [::]:443 ssl http2;
server_name xxxxxxxxxx;
expires $expires;
large_client_header_buffers 8 32k;
error_page 404 /404.html;
error_page 500 502 503 504 /50x.html;
location / {
proxy_pass http://io_nodes;
proxy_set_header Upgrade $http_upgrade;
proxy_set_header Connection "upgrade";
proxy_http_version 1.1;
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
proxy_set_header Host $host;
}
location /socket.io {
proxy_pass http://io_nodes;
proxy_set_header Upgrade $http_upgrade;
proxy_set_header Connection "upgrade";
proxy_http_version 1.1;
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
proxy_set_header Host $host;
}
location = /404.html {
root /usr/share/nginx/html;
}
location = /50x.html {
root /usr/share/nginx/html;
}
ssl_certificate /etc/letsencrypt/live/thewthr.app/fullchain.pem;
ssl_certificate_key /etc/letsencrypt/live/thewthr.app/privkey.pem;
include /etc/letsencrypt/options-ssl-nginx.conf;
ssl_dhparam /etc/letsencrypt/ssl-dhparams.pem;
if ($scheme != "https") {
return 301 https://$host$request_uri;
}
}
Socket.io 后端:
const express = require('express');
const app = express();
const server = require('http').Server(app);
const io = require('socket.io')(server);
server.listen(process.env.PORT, () => console.log(`Express running → PORT ${server.address().port}`));
io.on('connection', () => console.log(`Socket.io running → PORT ${server.address().port}`));
Socket.io 客户端:
let socket = io.connect(`127.0.0.1:8443`);