0
点赞
收藏
分享

微信扫一扫

https如何通过nginx完成双向认证转发

https 单向认证和双向认证

具体可以看看这篇文章 https双向认证,写的很详细和形象

  • 单向认证

https如何通过nginx完成双向认证转发_自定义

  • 双向认证

https如何通过nginx完成双向认证转发_客户端_02


生成自签证书、服务端证书和客户端证书

具体可以看这篇文章 Nginx配置ssl双向认证

1.CA 与自签名

1

2

3

4

5

6

7


# 生成CA私钥,会让你输自定义密码(例:000000)

openssl genrsa -aes256 -out ca.key 2048

 

# 制作CA公钥/根证书

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

# 输入CA密码:000000

# Common Name 随意填写(test.com);其它随意


2.服务端证书(server.client server.key)

1

2

3

4

5

6

7

8

9

10

11

12

13

14


# 生成服务器私钥(.pem文件),会让你输自定义密码(例:111111)

openssl genrsa -aes256 -out server.pem 1024

# .pem文件转换.key文件

openssl rsa -in server.pem -out server.key

# 输入Server密码:111111

 

# 生成签发请求

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

# 输入Server密码:111111

# Common Name填写自己的ip地址即可,其他随意

 

# 用CA签发证书

openssl x509 -req -sha256 -in server.csr -CA ca.crt -CAkey ca.key -CAcreateserial -days 3650 -out server.crt

# 输入CA密码:000000


3.客户端证书(client.client client.key)

1

2

3

4

5

6

7

8

9

10

11

12

13

14

15

16

17


# 生成客户端私钥(.pem文件),会让你输自定义密码(例:222222)

openssl genrsa -aes256 -out client.pem 1024

# .pem文件转换.key文件

openssl rsa -in client.pem -out client.key

# 222222

 

# 生成签发请求

openssl req -new -key client.pem -out client.csr

# 输入Client密码:222222

# Common Name填写自己的ip地址即可,其他随意

 

# 用CA签发证书

openssl x509 -req -sha256 -in client.csr -CA ca.crt -CAkey ca.key -CAcreateserial -days 3650 -out client.crt

# 输入CA密码:000000

 

# 使用浏览器访问时,需要生成p12格式的客户端证书,会让你输入自定义密码(例:123456)

openssl pkcs12 -export -clcerts -in client.crt -inkey client.key -out client.p12



服务端使用 https 进行通信,并使用双向认证

1

2

3

4

5

6

7

8

9

10

11

12

13

14

15

16

17

18


const express = require("express");

const https = require("https");

const fs = require("fs");

const app = express();

const PORT = 7000;

const options = {

key: fs.readFileSync("./ssl/server.key"),

cert: fs.readFileSync("./ssl/server.crt"),

ca: [fs.readFileSync("./ssl/ca.crt")],

requestCert: true,

rejectUnauthorized: true,

};

 

https

.createServer(options, app)

.listen(PORT, () => console.log(`App listening on port ${PORT}!`));

 

app.get("/v1", (req, res) => res.send("Hello World!"));



配置 nginx,进行转发

1

2

3

4

5

6

7

8

9

10

11

12

13

14

15

16

17


server {

listen       8080;

server_name  localhost;

 

location /api {

proxy_ssl_certificate ssl/client.crt;

proxy_ssl_certificate_key ssl/client.key;

# client-pw.txt 里面是Client密码:222222

proxy_ssl_password_file ssl/client-pw.txt;

proxy_pass https://localhost:7000/v1;

 

}

error_page   500 502 503 504  /50x.html;

location = /50x.html {

root   html;

}

}



测试

  1. 直接使用浏览器访问 https://loalhost:7000/v1 是获取不到数据的
  2. 使用 http://loalhost:8080/api 是可以获取到数据的
  3. 或者浏览器安装 client.p12 证书(安装时输入的密码是 p12 密码:123456)也是可以通过 https://loalhost:7000/v1 获取数据


总结

以上为个人经验,希望能给大家一个参考,也希望大家多多支持51CTO。

举报

相关推荐

0 条评论