【问题标题】:Why is my Nginx-Socket.io-Express-Setup not working over HTTPS?为什么我的 Nginx-Socket.io-Express-Setup 不能通过 HTTPS 工作?
【发布时间】:2019-12-21 08:38:13
【问题描述】:

在本地它运行良好,但只要我把它放在 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`);

【问题讨论】:

    标签: javascript node.js express nginx socket.io


    【解决方案1】:

    我假设你想通过你的 Nginx 代理使用 https。

    在这种情况下,您应该直接连接到您的 Nginx 服务器而不是而不是您的 Node 实例。

    var socket = require('socket.io-client');
    socket.connect('https://127.0.0.1', { secure: true, rejectUnauthorized: true });
    

    如果地址不正确,请将“127.0.0.1”替换为您的实际 Nginx 服务器。如果您的 Ssl 证书无效/自签名,请将 rejectUnauthorized 设置为 false。

    【讨论】:

    • 我为域名切换了 127.0.0.1,因为配置了多个服务器块,经过数小时的调试,它现在可以工作了。非常感谢!
    • 你可以把你的答案复制到here,我也会接受的
    猜你喜欢
    • 2011-05-22
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2022-01-15
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多