如何安装SSL证书到nginx服务器

辰漪
2021-12-21 / 0 评论 / 107 阅读 / 正在检测是否收录...

未开启ssl.png

当你不安装ssl证书时,浏览器会显示不安全字样,而且此时协议为http,安装ssl证书之后协议就是https

1. 首先要先申请ssl证书

我用的是阿里云的,可以在阿里云ssl证书里边申请免费ssl证书
申请ssl证书.png

选择你的服务器,我用的是nginx,点击下载会下载一个压缩包,里边有两个文件
下载ssl证书.png
下载完证书之后,压缩包中有一个key一个pem文件
两个文件.png

2. 在nginx根目录创建一个文件夹cert,将两个文件放里边

kxfryak7.png

3. 配置nginx.conf文件

kxfrz799.png

    server {
        listen       80;
        server_name  localhost;
        rewrite ^(.*)$ https://$host$1;  // 重定向到https://
        location / {
            root   dist;
            index  index.html index.htm;
            try_files $uri $uri/ /index.html;
        }

        location /adm {
            alias   adm/dist;
            index  index.html index.htm;
            try_files $uri $uri/ /adm/index.html;
        }
    }
server {
    listen       443 ssl;
    server_name  www.wrz521.top;
    ssl_certificate      ../cert/6287074_www.wrz521.top.pem;
    ssl_certificate_key  ../cert/6287074_www.wrz521.top.key;
    ssl_session_cache    shared:SSL:1m;
    ssl_session_timeout  5m;
    ssl_ciphers  HIGH:!aNULL:!MD5;
    ssl_prefer_server_ciphers  on;
    location / {
        root   dist;
        index  index.html index.htm;
        try_files $uri $uri/ /index.html;
    }
    location /adm {
        alias   adm/dist;
        index  index.html index.htm;
        try_files $uri $uri/ /adm/index.html;
    }
}

4. 测试配置是否成功

nginx -t

配置成功.png

5. 重启nginx

nginx -s reload

6. 输入网址测试

开启ssl.png

注意:
443端口务必在安全组放行

测试完之后,竟然发现跨域了
kxfs29rv.png

将后端nodejs进行https配置

const express = require('express')
const app = express()
const https = require('https')
const fs = require("fs");
const cors = require('cors')
const httpsOption = { // 配置ssl证书
    key: fs.readFileSync("./https/6287074_www.wrz521.top.key"),
    cert: fs.readFileSync("./https/6287074_www.wrz521.top.pem")
}
const serve = https.createServer(httpsOption, app)
serve.listen(8080, () => {
    console.log('serve is running at 127.0.0.1:8080')
})
1
选择打赏方式:
微信

评论 (0)

取消