客户端 vue 是用的"socket.io-client": "^4.1.3",连接代码是
let socket=io("https://aa.bb.cc.dd");
服务端 node.js 代码是
const server = https.createServer({
key: fs.readFileSync('/etc/nginx/ssl/private.key'),
cert: fs.readFileSync('/etc/nginx/ssl/certificate.crt'),
});
const io = require("socket.io")(server,{
cors: {
origin: "*",
methods: ["GET", "POST","OPTION"],
}
});
io.listen(3001);
本地运行正常,放服务器后启动,然后配置 nginx 代理和 ssl,配置是
server {
listen 80 default_server;
listen [::]:80 default_server;
listen 443 ssl default_server;
listen [::]:443 ssl default_server;
ssl on;
ssl_certificate /etc/nginx/ssl/certificate.crt;
ssl_certificate_key /etc/nginx/ssl/private.key;
ssl_session_timeout 5m;
root /var/www/html;
index index.html index.htm index.nginx-debian.html;
server_name _;
location / {
try_files $uri $uri/ =404;
# 按文档加的
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
proxy_set_header Host $host;
proxy_pass https://localhost:3001;
proxy_http_version 1.1;
proxy_set_header Upgrade $http_upgrade;
proxy_set_header Connection "upgrade";
}
}
报错是
Access to XMLHttpRequest at 'https://aa.bb.cc.dd/socket.io/?EIO=4&transport=polling&t=NkBhswM' from origin 'https://前端域名' has been blocked by CORS policy: No 'Access-Control-Allow-Origin' header is present on the requested resource.
一直报跨域的问题,StackOverflow 上查了很多相关的问题,试了都没能解决。。。
1
hly9469 2021-08-28 17:53:53 +08:00 via iPhone
nodejs 服务用 http 起,https 全放在 nginx 上
|