从官网下载nginx源码 http://nginx.org/en/download.html,我下载的是1.18版本
-
下载解压到root目录下
-
进入目录 cd /root/nginx-1.18.0
-
编辑auto/lib/openssl/conf,将全部
$OPENSSL/.openssl/
修改为$OPENSSL/
并保存 -
编译配置
./configure \ --without-http_gzip_module \ --with-http_ssl_module \ --with-http_stub_status_module \ --with-http_v2_module \ --with-file-aio \ --with-openssl="/usr/local/gmssl" \ --with-cc-opt="-I/usr/local/gmssl/include" \ --with-ld-opt="-lm"
-
编译安装
make install -
/usr/local/nginx即为生成的nginx目录
注:可能需要使用yum install pcre-devel需要安装pcre-devel
-
单向https配置
server { listen 0.0.0.0:443 ssl; ssl_protocols TLSv1 TLSv1.1 TLSv1.2; ssl_ciphers ECDHE-RSA-AES128-GCM-SHA256:AES128-SHA:DES-CBC3-SHA:ECC-SM4-SM3:ECDHE-SM4-SM3:SM2-WITH-SMS4-SM3:ECDHE-SM2-WITH-SMS4-GCM-SM3; ssl_verify_client off; ssl_certificate /usr/local/nginx/conf/demo1.sm2.sig.crt.pem; ssl_certificate_key /usr/local/nginx/conf/demo1.sm2.sig.key.pem; ssl_certificate_key /usr/local/nginx/conf/demo1.sm2.enc.key.pem; ssl_certificate /usr/local/nginx/conf/demo1.sm2.enc.crt.pem; location / { root html; index index.html index.htm; } }
注意:
SM2-WITH-SMS4-SM3: 这一个cipher是360浏览器访问时用到的
ECDHE-SM2-WITH-SMS4-GCM-SM3: 这个cipher是在使用gmssl s_client 命令时用到的
其他的有些是RSA用到的,可以不配置,这个随意。
配置完以上的后,重启nginx
- 使用 gmssl s_client -tls1_2 -connect localhost:443 -cipher SM2 -CAfile demo1.sm2.trust.pem 可以正常链接。
- 注意:如果你在nginx上部署了应用,使用s_client连接后是没有直接访问到应用的,下一步发送数据 “GET /” ,才能访问到,比如登录页面。 - 使用360安全浏览器访问 https://ip 也能正常打开欢迎页面。(ip是你部署nginx的电脑,360安全浏览器是在windows系统中使用的)
- 使用 gmssl s_client -tls1_2 -connect localhost:443 -cipher SM2 -CAfile demo1.sm2.trust.pem 可以正常链接。
-
双向https配置
server { listen 0.0.0.0:443 ssl; ssl_protocols TLSv1 TLSv1.1 TLSv1.2; ssl_ciphers ECDHE-RSA-AES128-GCM-SHA256:AES128-SHA:DES-CBC3-SHA:ECC-SM4-SM3:ECDHE-SM4-SM3:SM2-WITH-SMS4-SM3:ECDHE-SM2-WITH-SMS4-GCM-SM3; ssl_client_certificate /usr/local/nginx/conf/demo1.sm2.trust; ssl_verify_client on; ssl_certificate /usr/local/nginx/conf/demo1.sm2.sig.crt.pem; ssl_certificate_key /usr/local/nginx/conf/demo1.sm2.sig.key.pem; ssl_certificate /usr/local/nginx/conf/demo1.sm2.enc.crt.pem; ssl_certificate_key /usr/local/nginx/conf/demo1.sm2.enc.key.pem; location / { root html; index index.html index.htm; } }
双向的测试也可以使用 gmssl s_client的命令。
使用360测试双向的暂时还没找到方法,暂且放置。
-
单向 RSA/国密自适应
server
{
listen 0.0.0.0:443 ssl;
ssl_protocols TLSv1 TLSv1.1 TLSv1.2;
ssl_ciphers ECDHE-RSA-AES128-GCM-SHA256:AES128-SHA:DES-CBC3-SHA:ECC-SM4-SM3:ECDHE-SM4-SM3:SM2-WITH-SMS4-SM3:ECDHE-SM2-WITH-SMS4-GCM-SM3;
ssl_verify_client off;
ssl_certificate /usr/local/nginx/conf/demo1.rsa.crt.pem;
ssl_certificate_key /usr/local/nginx/conf/demo1.rsa.key.pem;
ssl_certificate /usr/local/nginx/conf/demo1.sm2.sig.crt.pem;
ssl_certificate_key /usr/local/nginx/conf/demo1.sm2.sig.key.pem;
ssl_certificate /usr/local/nginx/conf/demo1.sm2.enc.crt.pem;
ssl_certificate_key /usr/local/nginx/conf/demo1.sm2.enc.key.pem;
location /
{
root html;
index index.html index.htm;
}
}
以上配置完后,使用IE浏览器和360安全浏览器访问 https://ip 都是可以打开欢迎页面的。