Nginx SSL fast two-way authentication configuration (script)
这篇文章主要介绍了关于Nginx SSL快速双向认证配置(脚本),有着一定的参考价值,现在分享给大家,有需要的朋友可以参考一下
目前遇到一个项目有安全性要求,要求只有个别用户有权限访问。本着能用配置解决就绝不用代码解决的原则,在Nginx上做一下限制和修改即可。
这种需求其实实现方式很多,经过综合评估考虑,觉得SSL双向认证方案对用户使用最简单,遂决定用此方案。
注: 本方案在Ubuntu Server 16.04 LTS实施,其他操作系统请酌情修改
SSL双向认证
绝大多数SSL应用都以单向认证为主,即客户端只要信任服务端,就可以使用服务端的公钥加密后向服务端发起请求,由服务端的私钥解密之后获得请求数据。
如果这个过程反过来,让服务端信任客户端,服务端使用客户端的公钥加密之后将数据返回给客户端,其实也是可以做到的,原理和实现跟单向认证都差不多。
服务端信任客户端的操作往往也会伴随着客户端认证服务端的过程,所以让服务端信任客户端的SSL认证方式往往也被称为SSL双向认证,并且要配置SSL双向认证必须先开启服务端SSL,先配置客户端信任服务端。
Nginx的SSL双向认证配置
第一步 开启https访问
根据理论知识,我们必须先开启Nginx的SSL配置,即启用https。这个过程较为简单,目前有let's encrypt这种免费的证书方案,再也不用发愁自己搭建CA自签了。申请免费证书的过程略过,直接贴启用https的配置:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 |
|
以上那一大堆ssl的配置参考来自于: https://cipherli.st/ 加强SSL的安全性配置
特别注意最后的强制https跳转,我们的目的是SSL双向认证,不走https无任何意义,所以必须强制跳转https。
第二步 生成客户端证书并签证(脚本)
这个过程详细描述的文章太多了,这里就不啰嗦介绍openssl和签证过程了,本篇内容是快速生成双向认证配置的证书,所以直接贴脚本就行了,命令都是参考互联网上各种openssl双向配置文档,在此基础之上进行了命令上的简化与非交互式的支持。
整个目录结构如图:
1 2 3 4 5 6 7 |
|
自行创建/etc/nginx/ssl_certs/
,放入三个脚本,分别用于生成CA证书以及CA目录(create_ca_cert.sh
脚本的作用,只有第一次需要运行),创建客户端证书,并用CA证书签证(create_client_cert.sh
脚本的作用,必须先生成CA证书),revoke_cert.sh
脚本用于吊销证书,需要收回权限的时候可以使用。
每个脚本内容如下:
create_ca_cert.sh
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 |
|
create_client_cert.sh
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 |
|
revoke_cert.sh
1 2 3 4 5 6 |
|
简单分析一波脚本,首先是创建CA,对于Ubuntu系统来说,/etc/ssl/openssl.cnf
配置中默认的CA路径就是./demoCA
,为了不改动默认配置,直接按照默认配置的内容创建这些目录和文件即可。还有就是openssl子命令非常多,但是也和git一样,可以合并命令,比如用一条命令同时生成私钥和签证请求openssl req -nodes -newkey rsa:2048 -keyout client.key -new -out client.csr
,在req
的同时就做了genrsa
。由于创建CA脚本只是第一次运行需要,因此把证书配置直接写死在脚本中就完事了。
接下来是创建客户端证书,为了简化用户的使用,在服务端帮助用户生成证书并签证,把签证过的证书下发给用户就可以了。由于用户可能是不同部门,不同姓名,不同邮件地址,因此将这三个参数外部化,做一下参数解析,加上友好的命令行提示防止遗忘。这个脚本特别注意最后一行,会生成一个PKCS12
格式的证书。openssl默认产生的证书格式都是PEM
的,会将公钥和私钥分开,但是浏览器导入的时候需要将这些内容合并起来形成证书链
,所以需要将签证过的证书和私钥文件合并成一个PKCS12
格式的证书,直接将这个.p12
格式的证书交给用户就可以了。
最后是吊销证书了,当希望收回某个用户的访问权限时,直接运行这个脚本跟上目录名就可以了。
接下来运行创建CA的脚本:
1 2 3 4 5 6 7 |
|
此时产生的./demoCA
目录结构如下:
1 2 3 4 5 6 7 8 9 10 11 12 |
|
此时就可以配置nginx了,在上面单向ssl的配置中,追加以下配置:
1 2 3 |
|
ssl_client_certificate
就是客户端证书的CA证书了,代表此CA签发的证书都是可信的,ssl_verify_client on;
代表强制启用客户端认证,非法客户端(无证书,证书不可信)都会返回400错。
特别注意ssl_crl
这个配置,代表Nginx会读取一个CRL(Certificate Revoke List)文件,之前说过,可能会有收回用户权限的需求,因此我们必须有吊销证书的功能,产生一个CRL文件让Nginx知道哪些证书被吊销了即可。
注意: Nginx配置都是静态的,读取配置文件之后都会加载到内存中,即使文件内容变化也不会重新读取。因此当CRL文件发生变更之后,Nginx并不能意识到有新的证书被吊销了,所以必须使用reload
指令让Nginx重新读取配置文件:service nginx reload
或nginx -s reload
此时重启Nginx服务,就可以完成SSL双向认证配置了。
我们签发一个证书看看:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 |
|
这个脚本生成了私钥文件key
,签证请求文件csr
,经过CA签证后的证书文件crt
(里面没有私钥),以及将crt文件和key进行bundle之后的PKCS12
格式的证书文件p12
,将p12
文件下载到本地,双击一路Next导入证书即可。
注: 由于CA的证书文件不会发生变化,因此签证新的客户端证书不需要restart或reload nginx
这次打开我们的网站https://www.example.com
,浏览器就会提示我们选择一个已有的客户端证书进行认证了,没问题就可以看到网站内容了
注: 每次导入新的证书之后,必须重启浏览器才能提示使用新的证书文件
按照这种方式,有多少人需要授权,就可以用这个脚本签发多少个这样的证书,用户将p12
证书导入本地就可以正常访问网站了。
当我们需要收回某人的权限的时候(比如离职了),我们需要吊销他的证书:
1 2 3 4 5 6 7 |
|
这个脚本会自动吊销他的签证文件crt
,并且自动更新CRL
文件。特别注意需要reload或restart nginx才能让nginx重新加载CRL。这样被吊销的证书将无法访问网站了。
小结
本文我们通过Nginx配置SSL双向认证实现对客户端的加密认证,我们使用了简易的脚本帮助我们快速生成各种证书与签证,免除记忆繁琐openssl命令行,简化使用。
当然这只是一个最小可用集,当规模比较大的时候可能需要做很多改进,比如加入CA的web ui,直接可以操作签证和吊销证书,并且可以自动重启nginx。
再比如CRL这种静态配置文件不适合你的场景,希望的动态更新吊销证书列表,那么可以考虑OCSP方案,这个Nginx也是支持的,通过ssl_stapling_responder配置指定一个OCSP地址,这样将不需要每次吊销证书的时候都去重启nginx了,openssl也提供了ocsp服务端的功能,这里就不赘述了,可以自行查找相关资料。
以上就是本文的全部内容,希望对大家的学习有所帮助,更多相关内容请关注PHP中文网!
相关推荐:
The above is the detailed content of Nginx SSL fast two-way authentication configuration (script). For more information, please follow other related articles on the PHP Chinese website!

Hot AI Tools

Undresser.AI Undress
AI-powered app for creating realistic nude photos

AI Clothes Remover
Online AI tool for removing clothes from photos.

Undress AI Tool
Undress images for free

Clothoff.io
AI clothes remover

Video Face Swap
Swap faces in any video effortlessly with our completely free AI face swap tool!

Hot Article

Hot Tools

Notepad++7.3.1
Easy-to-use and free code editor

SublimeText3 Chinese version
Chinese version, very easy to use

Zend Studio 13.0.1
Powerful PHP integrated development environment

Dreamweaver CS6
Visual web development tools

SublimeText3 Mac version
God-level code editing software (SublimeText3)

Hot Topics



The methods that can query the Nginx version are: use the nginx -v command; view the version directive in the nginx.conf file; open the Nginx error page and view the page title.

How to configure an Nginx domain name on a cloud server: Create an A record pointing to the public IP address of the cloud server. Add virtual host blocks in the Nginx configuration file, specifying the listening port, domain name, and website root directory. Restart Nginx to apply the changes. Access the domain name test configuration. Other notes: Install the SSL certificate to enable HTTPS, ensure that the firewall allows port 80 traffic, and wait for DNS resolution to take effect.

Starting an Nginx server requires different steps according to different operating systems: Linux/Unix system: Install the Nginx package (for example, using apt-get or yum). Use systemctl to start an Nginx service (for example, sudo systemctl start nginx). Windows system: Download and install Windows binary files. Start Nginx using the nginx.exe executable (for example, nginx.exe -c conf\nginx.conf). No matter which operating system you use, you can access the server IP

You can query the Docker container name by following the steps: List all containers (docker ps). Filter the container list (using the grep command). Gets the container name (located in the "NAMES" column).

How to confirm whether Nginx is started: 1. Use the command line: systemctl status nginx (Linux/Unix), netstat -ano | findstr 80 (Windows); 2. Check whether port 80 is open; 3. Check the Nginx startup message in the system log; 4. Use third-party tools, such as Nagios, Zabbix, and Icinga.

How to configure Nginx in Windows? Install Nginx and create a virtual host configuration. Modify the main configuration file and include the virtual host configuration. Start or reload Nginx. Test the configuration and view the website. Selectively enable SSL and configure SSL certificates. Selectively set the firewall to allow port 80 and 443 traffic.

To deploy a JAR program on Nginx, seven steps need to be followed: 1) Install JRE, 2) Install Nginx, 3) Configure Nginx, 4) Deploy JAR, 5) Grant execution permissions, 6) Restart Nginx, 7) Verify deployment.

To get Nginx to run Apache, you need to: 1. Install Nginx and Apache; 2. Configure the Nginx agent; 3. Start Nginx and Apache; 4. Test the configuration to ensure that you can see Apache content after accessing the domain name. In addition, you need to pay attention to other matters such as port number matching, virtual host configuration, and SSL/TLS settings.
