首页 > 系统相关 >生成Nginx服务器SSL证书和客户端证书

生成Nginx服务器SSL证书和客户端证书

时间:2024-02-01 11:55:41浏览次数:25  
标签:Name Nginx 证书 RSA server SSL key pass

Nginx服务器SSL证书

生成pass key

下面的命令用于生成一个2048bit的pass key, -passout pass:111111 用于避免交互式输入密码

1 2 3 4 5 [tomcat@a02 tmp]$ openssl genrsa -aes256 -passout pass:111111 -out server.pass.key 2048 Generating RSA private key, 2048 bit long modulus ...........+++ .....................+++ e is 65537 (0x10001)

生成key

下面的命令用于生成私钥, -passin pass:111111是和pass key的密码对应的, 用于避免交互式输入密码

1 2 [tomcat@a02 tmp]$ openssl rsa -passin pass:111111 -in server.pass.key -out server.key writing RSA key

生成证书签发请求文件(CSR)

下面的命令用于生成csr文件, 这里需要填写机构相关信息. 其中CN务必填写为对应的服务器域名. 最后那个challenge password, 是这个csr的password

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 [tomcat@a02 tmp]$ openssl req -new -sha256 -key server.key -out server.csr You are about to be asked to enter information that will be incorporated into your certificate request. What you are about to enter is what is called a Distinguished Name or a DN. There are quite a few fields but you can leave some blank For some fields there will be a default value, If you enter '.', the field will be left blank. ----- Country Name (2 letter code) [XX]:CN State or Province Name (full name) []:Beijing Locality Name (eg, city) [Default City]:Chaoyang Organization Name (eg, company) [Default Company Ltd]:HenSomeone Organizational Unit Name (eg, section) []:iSomeone     Common Name (eg, your name or your server's hostname) []:internal.someone.com Email Address []:   Please enter the following 'extra' attributes to be sent with your certificate request A challenge password []:222222 An optional company name []:

发送CSR文件给CA服务商签发证书

如果是购买的CA服务商的SSL证书服务, 这一步把CSR发给服务商就可以了. 收到证书后将内容写入到 server.pem 文件

在Nginx上这样配置

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 server {     listen       443;     server_name  www.example.com;       ssl                  on;     ssl_certificate      /path/to/ssl/server.pem;     ssl_certificate_key  /path/to/ssl/server.key;     ssl_protocols TLSv1.2 TLSv1.1 TLSv1;     ssl_session_cache shared:ssl_www_example_com:5m;     ssl_session_timeout  5m;     ssl_ciphers ECDHE-RSA-AES256-GCM-SHA384:ECDHE-ECDSA-AES256-GCM-SHA384:ECDHE-RSA-AES128-GCM-SHA256:ECDHE-ECDSA-AES128-GCM-SHA256:DHE-RSA-AES256-GCM-SHA384:DHE-RSA-AES128-GCM-SHA256:ECDHE-RSA-AES256-SHA384:ECDHE-ECDSA-AES256-SHA384:ECDHE-RSA-AES256-SHA:ECDHE-ECDSA-AES256-SHA:DHE-RSA-AES256-SHA256:DHE-RSA-AES256-SHA:ECDHE-RSA-AES128-SHA256:ECDHE-ECDSA-AES128-SHA256:ECDHE-RSA-AES128-SHA:ECDHE-ECDSA-AES128-SHA:DHE-RSA-AES128-SHA256:DHE-RSA-AES128-SHA:ECDHE-RSA-DES-CBC3-SHA:ECDHE-ECDSA-DES-CBC3-SHA:EDH-RSA-DES-CBC3-SHA:AES256-GCM-SHA384:AES256-SHA256:AES256-SHA:AES128-GCM-SHA256:AES128-SHA256:AES128-SHA:DES-CBC3-SHA;     #...     location / {         #...     }     #... }

制作自签名证书

如果是打算制作自签名证书, 则进行如下的操作生成pem证书

1 2 3 4 [tomcat@a02 tmp]$ openssl x509 -req -sha256 -days 3655 -in server.csr -signkey server.key -out server.pem Signature ok subject=/C=CN/ST=Beijing/L=Chaoyang/O=HenSomeone/OU=iSomeone/CN=internal.someone.com Getting Private key

 

Nginx客户端验证证书

Nginx客户端验证证书和服务端SSL证书其实是没关系的, 你可以一边使用CA签发的证书, 一边使用自己制作的客户端验证证书.

生成服务器端私钥

1 2 3 4 5 6 7 8 [tomcat@a02 tmp]$ openssl genrsa -aes256 -passout pass:201906 -out ca.pass.key 2048 Generating RSA private key, 2048 bit long modulus ...............................................................................................................+++ ...................................+++ e is 65537 (0x10001)   [tomcat@a02 tmp]$ openssl rsa -passin pass:201906 -in ca.pass.key -out ca.key writing RSA key

 

生成服务器端证书

下面的命令会生成服务器证书ca.pem, 用于配制到nginx.

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 [tomcat@a02 tmp]$ openssl req -new -x509 -days 3655 -key ca.key -out ca.pem You are about to be asked to enter information that will be incorporated into your certificate request. What you are about to enter is what is called a Distinguished Name or a DN. There are quite a few fields but you can leave some blank For some fields there will be a default value, If you enter '.', the field will be left blank. ----- Country Name (2 letter code) [XX]:CN State or Province Name (full name) []:Beijing Locality Name (eg, city) [Default City]:Chaoyang Organization Name (eg, company) [Default Company Ltd]:HenSomeone Organizational Unit Name (eg, section) []:iSomeone Common Name (eg, your name or your server's hostname) []:internal.someone.com Email Address []:

生成客户端私钥

1 2 3 4 5 6 7 8 [tomcat@a02 tmp]$ openssl genrsa -aes256 -passout pass:201906 -out client_01.pass.key 2048 Generating RSA private key, 2048 bit long modulus ..........................+++ .....+++ e is 65537 (0x10001)   [tomcat@a02 tmp]$ openssl rsa -passin pass:201906 -in client_01.pass.key -out client_01.key writing RSA key

生成客户端证书签发请求CSR

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 [tomcat@a02 tmp]$ openssl req -new -key client_01.key -out client_01.csr You are about to be asked to enter information that will be incorporated into your certificate request. What you are about to enter is what is called a Distinguished Name or a DN. There are quite a few fields but you can leave some blank For some fields there will be a default value, If you enter '.', the field will be left blank. ----- Country Name (2 letter code) [XX]:CN State or Province Name (full name) []:Beijing Locality Name (eg, city) [Default City]:Chaoyang Organization Name (eg, company) [Default Company Ltd]:HenSomeone Organizational Unit Name (eg, section) []:Staff Common Name (eg, your name or your server's hostname) []:Staff Email Address []:   Please enter the following 'extra' attributes to be sent with your certificate request A challenge password []:201907 An optional company name []:

签发客户端证书

下面的命令, 用服务端的私钥和服务端的证书, 对客户端的CSR进行签发, 生成服务端证书. 这里有一个 -set_serial 01 的参数, 如果签发多个客户端证书, 这个数字不能重复

1 2 3 4 [tomcat@a02 tmp]$ openssl x509 -req -days 3655 -in client_01.csr -CA ca.pem -CAkey ca.key -set_serial 01 -out client_01.pem Signature ok subject=/C=CN/ST=Beijing/L=Chaoyang/O=HenSomeone/OU=Staff/CN=Staff Getting CA Private Key

客户端证书格式转换

前面生成的证书, 不能直接用于常见的应用, 需要转换成应用需要的格式

Full PEM:

1 [tomcat@a02 tmp]$ cat client_01.key client_01.pem ca.pem > client_01.full.pem

PFX - 这里输入的export password, 就是应用导入PFX证书时需要输入的密码.

1 2 3 [tomcat@a02 tmp]$ openssl pkcs12 -export -out client_01.full.pfx -inkey client_01.key -in client_01.pem -certfile ca.pem Enter Export Password: Verifying - Enter Export Password:

 

配置Nginx的客户端验证证书

1 2 ssl_client_certificate /path/to/ca.pem; ssl_verify_client optional; # or `on` if you require client key

标签:Name,Nginx,证书,RSA,server,SSL,key,pass
From: https://www.cnblogs.com/kn-zheng/p/18000904

相关文章

  • [stable/nginx-ingress] [emerg] 46#46: bind() to 0.0.0.0:80 failed (13: Permissio
    该报错与nginx的报错是一样的,不同的是发生在kubernetes-ingress场景。使用NginxIngressController时,以Deployment的方式启动POD时会报错。使用的Deployment配置示例:https://github.com/nginxinc/kubernetes-ingress/blob/main/deployments/deployment/nginx-ingress.yaml这......
  • Nginx根据IP限制国家地区访问
    在实际开发中有可能会限制一些地区国家访问网站,通过Nginx可以很方便的限制某些国家允许/禁止访问网站安装ngx_http_geoip_module模块ngx_http_geoip_module:参数需设置在位置在http模块中。nginx默认情况下不构建此模块,应使用--with-http_geoip_module配置参数启用它。ngx_h......
  • 47从零开始用Rust编写nginx,配对还有这么多要求!负载均衡中的路径匹配
    wmproxywmproxy已用Rust实现http/https代理,socks5代理,反向代理,负载均衡,静态文件服务器,websocket代理,四层TCP/UDP转发,内网穿透等,会将实现过程分享出来,感兴趣的可以一起造个轮子项目地址国内:https://gitee.com/tickbh/wmproxygithub:https://github.com/tickbh/wmpro......
  • 原生Nginx文件:/etc/nginx/nginx.conf
    userwww-data;worker_processesauto;pid/run/nginx.pid;include/etc/nginx/modules-enabled/*.conf;events{worker_connections768;#multi_accepton;}http{###BasicSettings##sendfileon;......
  • 搭建Nginx服务器实现WEB服务
    一般搭建Web服务器,都会要求在该服务器上创建几个基于域名的虚拟主机,并且还需要使用DNS实现域名解析,下面内容我们就对这个问题来进行例题的演示。(用2个基于域名虚拟主机)希望能对各位it人士有所帮助,话不多说,我们直接进入主题!!!1.安装Nginxviminstallnginx2.创建所需的站点根目录,在根......
  • ClientBuilder https请求时忽略SSL证书。
    在chrome中连接https时,有时被告知不是私密链接,手动可以继续。用java代码进行https也遇到这种情况,可以 实现一个X509TrustManager接口,用于绕过验证,不用修改里面的方法附完整可用代码importorg.glassfish.jersey.jackson.JacksonFeature;importjavax.net.ssl.SSLContext;im......
  • nginx代理服务器
    一、Nginx是什么?Nginx(enginex)是一个高性能的HTTP和反向代理web服务器,同时也提供了IMAP/POP3/SMTP服务。Nginx是一款轻量级的Web服务器/反向代理服务器及电子邮件(IMAP/POP3)代理服务器,在BSD-like协议下发行。其特点是占有内存少,并发能力强,事实上nginx的并发能力在同类型的网......
  • ubuntu安装nginx遇到的问题
    执行./configure的时候出现error1.UbuntutheHTTPrewritemodulerequiresthePCRElibrary 缺少pcre 执行sudoapt-getinstalllibpcre3libpcre3-dev sudoapt-getinstallopenssllibssl-dev2.  ./configure:error:theHTTPgzipmodulerequiresthezlibl......
  • nginx-go-crossplane crossplane golang 版本的nginx 配置解析包
    nginx-go-crossplane属于python版本crossplanenginx配置解析包的golang移植可以实现nginx配置解析转换为json格式的数据,当然也支持将json转换为nginx配置格式说明对于希望基于nginx搞自己的流量统一平台,同时希望基于api管理的,nginx-go-crossplane是一个很不错的选择......
  • PMP成绩查询及电子版证书下载
    2023年11月25日PMP考试成绩今日凌晨开始发布,按照往年的情况,成绩都是分批出的,如果暂时没查到成绩的同学请耐心等待,预计一周内成绩会全部出来。原创:厦门微思网络 【微思2002年成立,专业IT认证培训21年!】 我们主要课程:华为、思科、红帽、ORACLE、VMware、CISP、PMP等认证培训及考......