服务器配https

169 阅读1分钟

一. nginx 服务器配https?

腾讯云官方文档:https://cloud.tencent.com/document/product/400/35244

1. 将下载好的nginx证书 传输到远程服务器 /usr/local/nginx/conf 目录下

2. 使用xshell进入/usr/local/nginx/conf目录

3. 编辑 nginx.conf文件: vim nginx.conf

image.png

4.在 Nginx 根目录下(/usr/local/nginx),通过执行以下命令验证配置文件问题。

./sbin/nginx -t

重启 Nginx,即可使用https访问


报这个错:# [nginx: [emerg] the "ssl" parameter requires ngx_http_ssl_module in /usr/l](https://www.cnblogs.com/ghjbk/p/6744131.html)

nginx缺少http_ssl_module模块

1.首先:停止nginx服务: pkill -9 nginx
2.切换到源码包:cd /usr/local/src/nginx-1.11.3 (查看你自己下载解压的nginx文件夹,带版本号的文件夹)
3.看nginx原有的模块: /usr/local/nginx/sbin/nginx -V

在configure arguments:后面显示的原有的configure参数如下:

--prefix=/usr/local/nginx --with-http_stub_status_module

配置:./configure --prefix=/usr/local/nginx --with-http_stub_status_module --with-http_ssl_module
4. 配置完成后,运行命令: make
5. 然后备份原有已安装好的nginx: cp /usr/local/nginx/sbin/nginx /usr/local/nginx/sbin/nginx.bak
6. 然后将刚刚编译好的nginx覆盖掉原有的nginx(这个时候nginx要停止状态): cp ./objs/nginx /usr/local/nginx/sbin/
7. 然后启动nginx,仍可以通过命令查看是否已经加入成功: /usr/local/nginx/sbin/nginx -V

二. express 服务器配https?

1. 将下载的证书带有.key和.pem后缀的文件 传输到线上express项目里面 新建的cert文件夹 里面

2. 复制代码到 www文件里面

var http = require('http');
var fs = require('fs')
var https = require('https')
var path = require('path')
var express = require('express')
const options = {

key: fs.readFileSync(path.join(__dirname, '../cert/xxx.key')),

cert: fs.readFileSync(path.join(__dirname, '../cert/xxx.pem'))

}

app.set('porthttps',11443)
https.createServer(options,app).listen(app.get('porthttps'),function(){

console.log('11443端口运行');

})

结束