nginx配置https的双向认证方式

 更新时间:2024年11月19日 09:35:26   作者:CheerTan  
文章主要介绍了如何配置Nginx双向认证的详细步骤,包括生成证书、配置Nginx等,并提供了个人经验供参考

注意事项

配置双向认证,这里的common name需要都配置成不同

nginx 配置 https的双向认证

准备工作

linux环境安装openssl

生成证书步骤

1.新建一个文件夹

mkdir /root/keys

2.生成CA私钥 ca.key

openssl genrsa -out ca.key 4096

3.生成ca的数字证书 ca.crt

   openssl req -new -x509 -days 3650 -key ca.key -out ca.crt
   Country Name (2 letter code) [AU]:
   State or Province Name (full name) [Some-State]:
   Locality Name (eg, city) []:
   Organization Name (eg, company) [Internet Widgits Pty Ltd]:
   Organizational Unit Name (eg, section) []:
   Common Name (e.g. server FQDN or YOUR name) []:
   Email Address []:
   填入组织名称等信息

4.生成server端的私钥

openssl genrsa -out server.key 4096

5.生成 server 端数字证书请求

   openssl req -new -key server.key -out server.csr
   Country Name (2 letter code) [AU]:CN
   State or Province Name (full name) [Some-State]:Shanghai
   Locality Name (eg, city) []:
   Organization Name (eg, company) [Internet Widgits Pty Ltd]:Sidien Test
   Organizational Unit Name (eg, section) []:
   Common Name (e.g. server FQDN or YOUR name) []:192.168.0.162
   Email Address []:

   Please enter the following 'extra' attributes
   to be sent with your certificate request
   A challenge password []:
   An optional company name []:
  输入相关信息和密码

6.用 CA 私钥签发 server 的数字证书

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

7. 生成客户端私钥

openssl  genrsa  -out  client.key  4096

8. 生成客户端数字请求证书

  openssl req -new -key client.key -out client.csr
   Country Name (2 letter code) [AU]:
   State or Province Name (full name) [Some-State]:
   Locality Name (eg, city) []:
   Organization Name (eg, company) [Internet Widgits Pty Ltd]:
   Organizational Unit Name (eg, section) []:
   Common Name (e.g. server FQDN or YOUR name) []:
   Email Address []:

   Please enter the following 'extra' attributes
   to be sent with your certificate request
   A challenge password []:
   An optional company name []:
  输入相关信息和密码

9.用CA私钥签发数字证书:client.crt

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

10.生成windows安装证书程序

openssl pkcs12 -export -inkey client.key -in client.crt -out client.pfx

将client.pfx拷贝到作为客户端的机器上,安装证书,输入生成时的密码

11.配置nginx

 ssl_certificate  /etc/pki/ca_linvo/server/server.crt;     #server公钥路径
 ssl_certificate_key  /etc/pki/ca_linvo/server/server.key;   #server私钥路径
 ssl_client_certificate   /etc/pki/ca_linvo/root/ca.crt;   #根级证书公钥路径,用于验证各个二级client
 ssl_verify_client on;   开启双向认证

认证成功

curl --cert ./client.crt --key ./client.key https://url -k -v 

开启gzip跟缓存

gzip  on;

    gzip_min_length     256;
    gzip_buffers        4 16k;
    gzip_http_version   1.1;
    gzip_vary on;
    gzip_comp_level 3;
    gzip_disable "MSIE [1-6]\.";
    gzip_proxied any;
#这里设置需要压缩的格式
    gzip_types
	application/atom+xml
	application/javascript
	application/json
	application/ld+json
	application/manifest+json
	application/rss+xml
	application/vnd.geo+json
	application/vnd.ms-fontobject
	application/x-font-ttf
	application/x-web-app-manifest+json
	application/xhtml+xml
	application/xml
	font/opentype
	image/bmp
	image/svg+xml
	image/x-icon
	text/cache-manifest
	text/css
	text/plain
	text/vcard
	text/vnd.rim.location.xloc
	text/vtt
	text/x-component
	text/x-cross-domain-policy;
		location / {    
	   root   /root/www/web;
	   index  index.html;
	   autoindex on;
            if ($request_filename ~ .*.(js|css)$)
         {
         expires 7d;
         }
	}

总结

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

相关文章

  • 提升Nginx性能的一些建议

    提升Nginx性能的一些建议

    这篇文章主要介绍了提升Nginx性能的一些建议,帮助大家更好的使用nginx,感兴趣的朋友可以了解下
    2020-09-09
  • Nginx + Tomcat实现请求动态数据和请求静态资源的分离详解

    Nginx + Tomcat实现请求动态数据和请求静态资源的分离详解

    这篇文章主要给大家介绍了关于Nginx + Tomcat实现请求动态数据和请求静态资源的分离的相关资料,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的朋友们下面随着小编来一起学习学习吧
    2018-07-07
  • 深入理解Nginx之error_page模块的使用

    深入理解Nginx之error_page模块的使用

    error_page是nginx一个重要的指令,作用是定制化服务器错误页面,本文主要介绍了Nginx之error_page模块的使用,具有一定的参考价值,感兴趣的可以了解一下
    2023-09-09
  • Nginx proxy_set_header参数设置

    Nginx proxy_set_header参数设置

    本文主要介绍了Nginx proxy_set_header参数设置,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的朋友们下面随着小编来一起学习学习吧
    2023-09-09
  • Nginx Gzip模块启用和配置指令详解

    Nginx Gzip模块启用和配置指令详解

    这篇文章主要介绍了Nginx Gzip模块启用和配置指令详解的,需要的朋友可以参考下
    2014-04-04
  • 升级nginx支持HTTP/2服务端推送的方法

    升级nginx支持HTTP/2服务端推送的方法

    这篇文章主要介绍了升级nginx支持HTTP/2服务端推送的方法,小编觉得挺不错的,现在分享给大家,也给大家做个参考。一起跟随小编过来看看吧
    2018-05-05
  • nginx加php-fpm出现502 bad gateway错误的5种解决方法

    nginx加php-fpm出现502 bad gateway错误的5种解决方法

    这篇文章主要介绍了nginx加php-fpm环境中出现502 bad gateway错误的5种解决方法,总结归纳服务器出现502错误的原因多数为连接过多和脚本超时,本文总结了5种解决方法,需要的朋友可以参考下
    2014-05-05
  • Nginx之proxy_redirect使用详解

    Nginx之proxy_redirect使用详解

    这篇文章主要介绍了Nginx之proxy_redirect使用详解,小编觉得挺不错的,现在分享给大家,也给大家做个参考。一起跟随小编过来看看吧
    2018-12-12
  • 权限问题导致Nginx 403 Forbidden错误的解决方法

    权限问题导致Nginx 403 Forbidden错误的解决方法

    这篇文章主要介绍了权限问题导致Nginx 403 Forbidden错误的解决方法,本文中导致 403 Forbidden错误的原因是配置文件中没有指明一个用户,需要的朋友可以参考下
    2014-08-08
  • 在Nginx中增加对OAuth协议的支持的教程

    在Nginx中增加对OAuth协议的支持的教程

    这篇文章主要介绍了在Nginx中增加对OAuth协议的支持的教程,OAuth协议如今广泛用于社交网络的API中,需要的朋友可以参考下
    2015-06-06

最新评论