【发布时间】: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