node项目部署https服务

难以形容这份喜悦了,这次是真的一杯茶一包烟一个bug改一天了
先描述我的问题,我的网页配置了https证书,但是node部署后是http服务,在调用接口时候前端报错:“Mixed Content: The page at ‘https://XXX’ was loaded over HTTPS, but requested an in…”
因为之前遇到过所以知道他是因为用了http访问了https接口或者反过来导致的,一波操作加上看了错误的方法受到了误导搞得心态很崩,在放弃的边缘终于找到了解决的方法!
好了不废话了开始展示了,我的服务器是apache服务,前后端是vue+node+express:

首先是node部分,网上说需要配置一下https打开端口,我没做测试不写这个可不可以,但我觉得应该可以不需要(一开始没改服务器写了也不生效),以防万一写一下吧。

//文件处理模块
let fs = require('fs')
//http 和 https服务模块
let http = require('http')
let https = require('https')
//配置你的证书,注意这里的证书是nginx的,不管你服务器用的是apache还是什么这里只要放nginx就好了
const httpsOption = {
    
    
    key: fs.readFileSync("./https/4432850_www.czjdream.com.key"),
    cert: fs.readFileSync("./https/4432850_www.czjdream.com.pem")
}
let express = require('express');
let app = express();
//在指定端口启动你的项目
http.createServer(app).listen(8082);
https.createServer(httpsOption, app).listen(8088);

然后我用的是node管理器是PM2管理器,node项目找个地扔服务器上,然后在PM2里选择文件目录点击启动就可了。
然后配置你的apache配置相关(我用的是宝塔面板,这里就默认你已经配置好了SSL证书):
apache/conf/extra/httpd-ssl.conf:

# 设置监听你的端口,调用接口时就是https://xxx.com:8089
Listen 8089
# 新建一个VirtualHost,写上监听的端口
<VirtualHost *:8089>     
    ServerName   blogMaster
    SSLEngine on
    SSLProtocol all -SSLv2 -SSLv3
    SSLCipherSuite HIGH:!RC4:!MD5:!aNULL:!eNULL:!NULL:!DH:!EDH:!EXP:+MEDIUM
    SSLHonorCipherOrder on
    SSLCertificateFile C:/BtSoft/apache/cert/xxxxx.crt
    SSLCertificateKeyFile C:/BtSoft/apache/cert/xxxxx.key 
    SSLCertificateChainFile C:/BtSoft/apache/cert/xxxxx.crt
	
        SSLProxyEngine on

          Proxyrequests off
<Proxy *>
         Order deny,allow
         Allow from all
 </Proxy>

<Directory />
		    Options FollowSymLinks ExecCGI
		    AllowOverride All
		    Require all granted
</Directory>

<Location />
	  # 这里是你在node里配置启动的端口,我的是8082
      ProxyPass http://localhost:8082/
      ProxyPassReverse http://localhost:8082/
</Location>
</VirtualHost>     

配置完记得重启apache服务。

哇心态崩掉,我弄了一天结果就这么几行解决,就这?就这?

本项目参考了http://blog.csdn.net/aerchi/article/details/73605496
但是大部分为自己原创总结,在此记录

猜你喜欢

转载自blog.csdn.net/qq_43511063/article/details/109785146
今日推荐