【问题标题】:SSL breaks socket.ioSSL 破坏了 socket.io
【发布时间】:2019-03-09 13:25:50
【问题描述】:

我一直在使用 socket.io 运行消息传递服务,一切正常,直到我加密托管 socket.io 服务器的域名,现在我得到“CORS 阻止同源策略原因:CORS 请求未成功”

该网址在未加密时有效(使用 http),在加密时中断并使用 https。

服务器托管在使用 NGINX 代理请求的 ubuntu 服务器上。

任何帮助将不胜感激

服务器代码:

const fs = require("fs")
const https = require('https')

const options =    {
    key: fs.readFileSync(
        "/etc/letsencrypt/live/talk.liven.online/privkey.pem"
    ),
    cert: fs.readFileSync(
        "/etc/letsencrypt/live/talk.liven.online/cert.pem"
    ),
    ca: fs.readFileSync(
        "/etc/letsencrypt/live/talk.liven.online/chain.pem"
    )
}


const http = https.createServer(options)

const io = require('socket.io')(http, { transports:['websocket', 'htmlfile', 'xhr-polling', 'jsonp-polling', 'polling']})

io.origins(["https://navigator.liven.online"])
io.on("connection", socket => {
    console.log("a user connected!")
    socket.on('disconnect', () => console.log('user disconnected'))
    socket.on('chat message', msg => io.emit("chat message", msg))
}
const port = process.env.PORT || 2001;
http.listen(port, port => console.log("listening on ${port}"))

客户端代码:

// heavily redacted but i think this is all thats relevant
io.connect("https://talk.liven.online", {secure: true, rejectUnauthorized: false})

nginx 块配置

server {
    listen 80;
    listen [::]:80;

    root /var/www/talk.liven.online/html;
    index index.html index.htm index.nginx-debian.html;

    server_name talk.liven.online www.talk.liven.online;

    location / {
        proxy_pass https://localhost:2001;
        proxy_http_version 1.1;
        proxy_set_header Upgrade $http_upgrade;
        proxy_set_header Connection 'upgrade';
        proxy_set_header Host $host;
        proxy_cache_bypass $http_upgrade;
    }
    listen [::]:443 ssl ipv6only=on; # managed by Certbot
    listen 443 ssl; # managed by Certbot

    ssl_certificate /etc/letsencrypt/live/talk.liven.online/fullchain.pem; # managed by Certbot
    ssl_certificate_key /etc/letsencrypt/live/talk.liven.online/privkey.pem; # 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 = talk.liven.online) {
        return 301 https://$host$request_uri;
    } # managed by Certbot


    listen 80;
    listen [::]:80;

    server_name talk.liven.online www.talk.liven.online;
    return 404; # managed by Certbot


}

【问题讨论】:

    标签: javascript ssl nginx socket.io


    【解决方案1】:

    您展示的 NGINX 配置是针对纯 HTTP,而不是 HTTPS。它还使用 HTTP 代理请求,而不是 HTTPS:

    proxy_pass http://localhost:2001
               ^^^^
    

    如果你想允许客户端连接到https://...,你必须让 NGINX 处理 SSL 部分。

    由于socket.io 服务器与 NGINX 在同一台机器上运行,您可能可以通过普通 HTTP 进行实际代理。

    【讨论】:

    • 感谢这位帮助伙伴,这是否需要将 localhost 更改为 https://..?还是我完全错了(服务器配置的新手)?
    • @Red 我认为您需要删除您发布的配置中的前两个 listen 语句(端口 80 的语句)。所以第一个 server 块监听端口 443,并处理 SSL,第二个 server 块监听端口 80,并将所有请求重定向到 HTTPS 部分。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2018-08-15
    • 1970-01-01
    • 2014-09-07
    • 1970-01-01
    • 1970-01-01
    • 2018-08-11
    • 2016-02-24
    相关资源
    最近更新 更多