怎么在nginx中通过配置SSL证书实现https服务

  介绍

这篇文章给大家介绍怎么在nginx中通过配置SSL证书实现https服务,内容非常详细,感兴趣的小伙伴们可以参考借鉴,希望对大家能有所帮助。

节点基本架构如下:

| - - - - -项目   |,|——安康;static ,,,, #,存放html文件   | |还以为;|——安康;index.html  #, index . html   |,|——安康;node_modules , #,依赖包   |,|——安康;app.js ,,,, #, node 入口文件   |,|——安康;package.json    |,|——安康;.babelrc ,,, #,转换es6文件

索引。html文件代码如下:

& lt; ! DOCTYPE  html>   & lt; html>   & lt; head>   ,& lt; meta  charset=utf-8>   ,& lt; meta  name=皉eferrer",内容=皀ever"比;   ,& lt; title> nginx配置https   & lt;/head>   & lt; body>   ,& lt; div>   & lt;才能h3>欢迎使用https来访问页面& lt;/h3>   ,& lt;/div>   & lt;/body>   & lt;/html>

应用程序。js代码如下:

const  Koa =,要求(& # 39;洋槐# 39;);   const  fs =,要求(& # 39;fs # 39;);   const  path =,要求(& # 39;path & # 39;);   const  router =,要求(& # 39;koa-router& # 39;) ();   const  koaBody =,要求(& # 39;koa-body& # 39;);   const  static =,要求(& # 39;koa-static& # 39;);      const  app =, new 高雅();      router.get (& # 39;/& # 39;,, (ctx,,下一个),=祝辞,{   ,//设置头类型,,如果不设置,会直接下载该页面=,ctx.type  & # 39; html # 39;;   ,//读取文件   ,const  pathUrl =, path.join (__dirname, & # 39;/静态/index . html # 39;);=,,ctx.body  fs.createReadStream (pathUrl);   接下来,();   });      app.use(静态(path.join (__dirname)));      app.use (router.routes ());   app.use (router.allowedMethods ());      app.listen(3001年,(),=祝辞,{   ,console.log (& # 39; server  is  listen 拷贝3001 & # 39;);   });

包。json代码如下;

{   ,“name":,“uploadandload"   ,“version":,“1.0.0"   ,“description":““,   ,“main":,“app.js"   ,“scripts": {   “dev"才能:,“nodemon 。/app.js"   },   ,“author":““,   ,“license":,“ISC"   ,“dependencies": {   “fs"才能:,“0.0.1-security"   “koa"才能:,“^ 2.7.0",   “koa-body"才能:,“^ 4.1.0",   “koa-router"才能:,“^ 7.4.0",   “koa-send"才能:,“^ 5.0.0",   “koa-static"才能:,“^ 5.0.0",   “nodemon"才能:,“^ 1.19.0",   “path"才能:,“^ 0.12.7"   ,}   }

然后我在项目的根目录下执行npm运行开发后,就可以在浏览器下访问http://localhost: 3001了,但是为了我想使用域名访问的话,因此我们可以在主机文件下绑定下域名,比如叫xxx.abc.com。主机文件如下绑定:

127.0.0.1  xxx.abc.com

因此这个时候我们使用http://xxx.abc.com: 3001/就可以访问页面了,如下所示:

怎么在nginx中通过配置SSL证书实现https服务

如上所示,我们就可以访问页面了,但是我们有没有发现,在chrome浏览器下显示http请求是不安全的,因此这个时候我想使用https来访问就好了,网页的安全性就得到了保障,但是这个时候如果我什么都不做,直接使用https去访问的话是不行的,比如地址:https://xxx.abc.com: 3001。如下图所示:

怎么在nginx中通过配置SSL证书实现https服务

我们知道使用https访问的话,一般是需要安全证书的,因此我们现在的任务是需要使用nginx来配置下安全证书之类的事情,然后使用https能访问网页就能达到目标。

<强> nginx配置https服务

1。首先进入nginx目录下,使用命令:cd/usr/local/etc/nginx。然后在该目录下创建cert文件夹,目的是存放证书文件。
使用命令:mkdir cert如下所示:

怎么在nginx中通过配置SSL证书实现https服务

2。然后我们需要把证书相关的文件,比如服务器。crt和服务器。关键文件复制到该cert目录下。比如如下证书文件:

怎么在nginx中通过配置SSL证书实现https服务

怎么在nginx中通过配置SSL证书实现https服务