美文网首页
Nginx 和 node https配置

Nginx 和 node https配置

作者: WishPeng | 来源:发表于2018-03-08 14:18 被阅读0次

Nginx 和 node https配置

1.生成证书

自制CA私钥

openssl genrsa -des3 -out ca.key 4096

自制CA证书

openssl req -new -x509 -days 3650 -key ca.key -out ca.crt

自制Server私钥,生成免密码版本

openssl genrsa -des3 -out server.key 4096 openssl rsa -in server.key -out server.nosecret.key

制作csr文件

openssl req -new -key server.key -out server.csr

用CA证书私钥对csr签名(CA不能用X509,这点需要注意)生成Server证书

openssl ca -days 3650 -in server.csr -cert ca.crt -keyfile ca.key -out server.crt

2.创建node服务端

安装node环境,新建demo文件夹。打开终端输入命令:

npm init

npm install express

新建serve.js编写以下代码:

var app = require('express')();
var fs = require('fs');
var http = require('http');
var https = require('https');
var privateKey  = fs.readFileSync('private.pem', 'utf8');
var certificate = fs.readFileSync('file.crt', 'utf8');
var credentials = {key: privateKey, cert: certificate};

var httpServer = http.createServer(app);
var httpsServer = https.createServer(credentials, app);
var PORT = 18080;
var SSLPORT = 18081;

httpServer.listen(PORT, function() {
    console.log('HTTP Server is running on: http://localhost:%s', PORT);
});
httpsServer.listen(SSLPORT, function() {
    console.log('HTTPS Server is running on: https://localhost:%s', SSLPORT);
});

// Welcome
app.get('/', function(req, res) {
    if(req.protocol === 'https') {

终端运行开启服务:

node serve.js

3.添加ngnix服务

安装nginx,修改Nginx.cfg文件。

server {
    listen 443;
    server_name localhost;
    ssl on;
    ssl_certificate ssl/server.crt;
    ssl_certificate_key ssl/server.nosecret.key;
    location /t {
        echo "Hello World";
    }
}

4.配置服务转发

配置转发实现http和https共同访问

server {  
    listen  80;  
    server_name blog.90its.cn;   
    rewrite ^(.*)$  https://$host$1 permanent;  
}
server {
    listen  443 ssl;
    server_name  www.wishpeng.top;
    ssl         on; 
    ssl_certificate     /home/admin/https/file.crt; 
    ssl_certificate_key /home/admin/https/private.pem; 
    location / {
        proxy_pass http://localhost:18080;
        proxy_http_version 1.1;
        proxy_set_header Upgrade $http_upgrade;
        proxy_set_header Connection 'upgrade';
        proxy_set_header Host $host;
        proxy_cache_bypass $http_upgrade;
    }
    location /public {
        root /phpstudy/wishpeng;
    }
}

相关文章

  • Nginx 和 node https配置

    Nginx 和 node https配置 1.生成证书 自制CA私钥 openssl genrsa -des3 -...

  • Nginx 配置https

    Nginx 和 node https配置 1.生成证书 自制CA私钥 openssl genrsa -des3 -...

  • k8s学习笔记-8-L4代理

    安装nginx并配置成L4代理 安装节点 node1 node4 安装nginx 配置nginx 在nginx配置...

  • nginx

    node 使用 nginx 反向代理搭建https服务 使用自己生成的证书 nginx http配置 编辑ngin...

  • 腾讯云配置

    腾讯云配置启动多个node Nginx 配置多域名不同端口 首先需要编辑/etc/nginx/nginx.conf...

  • 2019-01-18

    nginx的基本配置和SSL的http跳转https基本配置 在nginx中的nginx.conf下配置 http...

  • HTTPS配置入门:Nginx、Node.js配置HTTPS服

    node.js 和 nginx 支持https服务器 下面案例是:通过nginx添加证书代理的方式,实现ngin...

  • # nginx https证书配置

    nginx https证书配置 一 前言 此文档针对于nginx配置反向代理使用https证书方法 nginx作为...

  • nginx https 配置

    nginx https 配置

  • nginx配置https

    nginx配置https https需要的证书我们已经申请到了,下面分享下nginx配置https的一些配置参数!...

网友评论

      本文标题:Nginx 和 node https配置

      本文链接:https://www.haomeiwen.com/subject/ilkyfftx.html