V2EX = way to explore
V2EX 是一个关于分享和探索的地方
现在注册
已注册用户请  登录
V2EX 提问指南
zxCoder
V2EX  ›  问与答

有用到 vue+socket.io(node.js)+nginx(ssl)做项目的大哥吗,请教一下

  •  
  •   zxCoder · 2021-08-28 15:36:58 +08:00 · 483 次点击
    这是一个创建于 1184 天前的主题,其中的信息可能已经有所发展或是发生改变。

    客户端 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 上查了很多相关的问题,试了都没能解决。。。

    hly9469
        1
    hly9469  
       2021-08-28 17:53:53 +08:00 via iPhone
    nodejs 服务用 http 起,https 全放在 nginx 上
    关于   ·   帮助文档   ·   博客   ·   API   ·   FAQ   ·   实用小工具   ·   3620 人在线   最高记录 6679   ·     Select Language
    创意工作者们的社区
    World is powered by solitude
    VERSION: 3.9.8.5 · 31ms · UTC 04:51 · PVG 12:51 · LAX 20:51 · JFK 23:51
    Developed with CodeLauncher
    ♥ Do have faith in what you're doing.