1、certbotcertbot官网:https://certbot.eff.org/certbot和let's encrypt什么关系?我的理解,certbot是更方便的生成let's encrypt签发证书的一个工具。2、阿里云阿里云的免费证书在 域名服务->找到对应的域名->管理->免费开启SSL证书,最后去下载证书。下载下来是2个文件。二、nuxt配置我用的nuxt是有sever文件夹的,其中sever/index.js:
const fs = require('fs');const http = require('http');const https = require('https');const path =require('path');import express from 'express'import bodyParser from 'body-parser';import { Nuxt, Builder } from 'nuxt'import {timed} from "./schedule/index";import api from './api'const app = express()const host = process.env.HOST || '127.0.0.1'const port = process.env.PORT || 3000;// app.set('port', port)//express.static 函数提供的路径相对于您在其中启动 node 进程的目录。app.use('/static',express.static('static'));app.use(express.static('./')); //./这是命令运行的位置app.use(bodyParser.json({limit: '1mb'})); //这里指定参数使用 json 格式app.use(bodyParser.urlencoded({extended: true}));// Import API Routesapp.use('/api', api)// Import and Set Nuxt.js optionslet config = require('../nuxt.config.js')config.dev = !(process.env.NODE_ENV === 'production')// Init Nuxt.jsconst nuxt = new Nuxt(config)// Build only in dev modeif (config.dev) {const builder = new Builder(nuxt)builder.build()}// Give nuxt middleware to expressapp.use(nuxt.render)// Listen the serverconst httpServer = http.createServer(app);httpServer.listen(port, host,function(){console.log('http启动...');});console.log('Server listening on ' + host + ':' + port) // eslint-disable-line no-console//io//https相关if(process.env.NODE_ENV === 'production'){const privateKey = fs.readFileSync(path.resolve(__dirname,'cert_ali/1538367494158.key'),'utf8');const certificate = fs.readFileSync(path.resolve(__dirname,'cert_ali/1538367494158.pem'), 'utf8');// const ca = fs.readFileSync(path.resolve(__dirname,'cert/chain.pem'), 'utf8');const credentials = {key: privateKey,cert: certificate,// ca: ca};const httpsServer = https.createServer(credentials, app);httpsServer.listen(443,host,function(){console.log('https启动...');})}
这段代码只在生产环境启用https服务。
三、管理阿里云防火墙,开启443端口
前面2步骤结束后,一直访问https没反应,不报错也不返回任何东西,一直在加载网站。我的网站,直播客:
https://www.zhiboke.site/