NGINX部署SSL
一、检查你的版本支持不支持SSL
nginx.exe -V
nginx version: nginx/1.18.0
built by cl 16.00.40219.01 for 80x86
built with OpenSSL 1.1.1f 31 Mar 2020
TLS SNI support enabled
configure arguments: --with-cc=cl --builddir=objs.msvc8 --with-debug --prefix= --conf-path=conf/nginx.conf --pid-path=logs/nginx.pid --http-log-path=logs/access.log --error-log-path=logs/error.log --sbin-path=nginx.exe --http-client-body-temp-path=temp/client_body_temp --http-proxy-temp-path=temp/proxy_temp --http-fastcgi-temp-path=temp/fastcgi_temp --http-scgi-temp-path=temp/scgi_temp --http-uwsgi-temp-path=temp/uwsgi_temp --with-cc-opt=-DFD_SETSIZE=1024 --with-pcre=objs.msvc8/lib/pcre-8.44 --with-zlib=objs.msvc8/lib/zlib-1.2.11 --with-http_v2_module --with-http_realip_module --with-http_addition_module --with-http_sub_module --with-http_dav_module --with-http_stub_status_module --with-http_flv_module --with-http_mp4_module --with-http_gunzip_module --with-http_gzip_static_module --with-http_auth_request_module --with-http_random_index_module --with-http_secure_link_module --with-http_slice_module --with-mail --with-stream --with-openssl=objs.msvc8/lib/openssl-1.1.1f --with-openssl-opt='no-asm no-tests -D_WIN32_WINNT=0x0501' --with-http_ssl_module --with-mail_ssl_module --with-stream_ssl_module
二、采用LINUX命令生成
2.1生成秘钥key,运行:
openssl genrsa-des3-outserver.key2048
会有两次要求输入密码,输入同一个即可
输入密码,然后你就获得了一个server.key文件.
以后使用此文件(通过openssl提供的命令或API)可能经常回要求输入密码,如果想去除输入密码的步骤可以使用以下命令:
openssl rsa-inserver.key-outserver.key
2.2 创建服务器证书的申请文件server.csr,运行:
openssl req-new-key server.key-outserver.csr
其中Country Name填CN,Common Name填主机名也可以不填,如果不填浏览器会认为不安全.(例如你以后的url为https://abcd/xxxx....这里就可以填abcd),其他的都可以不填.
2.3 创建CA证书:
openssl req-new-x509-key server.key-outca.crt-days3650
此时,你可以得到一个ca.crt的证书,这个证书用来给自己的证书签名.
2.4创建自当前日期起有效期为期十年的服务器证书server.crt:
openssl x509 -req -days 3650 -in server.csr -CA ca.crt -CAkey server.key -CAcreateserial -out server.crt
2.5 ls你的文件夹,可以看到一共生成了5个文件:
ca.crt ca.srl
server.crt server.csr server.key其中,server.crt和server.key就是你的nginx需要的证书文件.
三、 如何配置nginx
3.1 打开你的nginx配置文件,搜索443找到https的配置,去掉这段代码的注释.或者直接复制我下面的这段配置:
server{
listen443;
server_name localhost;
ssl on;
ssl_certificate/root/Lee/keys/server.crt;#配置证书位置
ssl_certificate_key/root/Lee/keys/server.key;#配置秘钥位置
#ssl_client_certificate ca.crt;#双向认证
#ssl_verify_client on; #双向认证
ssl_session_timeout5m;
ssl_protocolsSSLv2SSLv3TLSv1;
ssl_ciphers ALL:!ADH:!EXPORT56:RC4+RSA:+HIGH:+MEDIUM:+LOW:+SSLv2:+EXP;
ssl_prefer_server_ciphers on;
3.2 将ssl_certificate改为server.crt的路径,将ssl_certificate_key改为server.key的路径.
3.3 nginx -s reload 重载配置
至此,nginx的https就可以使用了,默认443端口.
四、启动,直接运行nginx.exe ,如果端口被暂用则
443端口可能会被程序占用:
在window下可以使用如下方式检测:
netstat -aon|findstr "443"

浙公网安备 33010602011771号