node可以嵌入 https服务器里使用吗?
发布于 5 年前 作者 s7smile 4588 次浏览 来自 问答

场景是这样的,主站https PHP服务器,node.js 也在这个主机上,之前主站没有升级全站https的时候 是可以直接嵌入网页使用的,现在主站全部升级https之后,发现node无法通信了。希望大佬能给个思路如何搞

8 回复

受信证书还是自签发?

@waitingsong node服务器没有证书,主站www证书是买的

@s7smile 证书不会是我痛签发的吧。。 可以试试把证书的 intermediate 中间证书和顶级ca的证书安装到node所在服务器中。

php 肯定会跑在 nginx 后面的吧,在 nginx 这边做个反向代理呢

@hsiaosiyuan0 这个操作起来简单,今天我试一下,结果告诉大家

@waitingsong 我曾试过把node也做成https 牵扯到证书比较繁琐,没有去尝试

@waitingsong @hsiaosiyuan0 感谢两位同行提供的思路方法,现在把解决后的代码贴出来供遇到同样问题的朋友参考。 在nginx 443下增加 location /socket{ # switch off logging access_log off;

		# redirect all HTTP traffic to localhost:3000
		proxy_pass http://localhost:8080;
		proxy_set_header X-Real-IP $remote_addr;
		proxy_set_header Host $host;
		proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
		
		# WebSocket support (nginx 1.4)
		proxy_http_version 1.1;
		proxy_set_header Upgrade $http_upgrade;
		proxy_set_header Connection "upgrade";

		# Path rewriting
		rewrite /socket/(.*) /$1 break;
		proxy_redirect off;
	}

客户端页面文件更改为 <script>

	var socket = io("wss://www.domain.com",{path:'/socket/socket.io'});
	$('form').submit(function(){

注意是 ws:// 改为 wss://

QQ图片20190911163933.png 不过问题来了, 通过nginx后 外面还有CDN 请求走了GET POST 有点耗费系统资源

回到顶部