前端nginx部署,后端springboot框架,前端和后端都要做相应的ssl加密部署。
1.生成证书
适用于测试阶段,正常情况下,证书应该向发证CA机构申请。
生成证书密钥库
keytool -genkeypair -alias honor -storetype PKCS12 -keyalg RSA -keystore home.pkcs12 -storepass 123456 -validity 3650 -keysize 2048
查看密钥库
keytool -list -rfc -keystore home.pkcs12
或
keytool -list -v -keystore home.pkcs12
导出证书文件,本质是CERTIFICATE
openssl pkcs12 -in home.pkcs12 -out home_crt.pem -clcerts -nokeys
生成证书密钥文件:本质是私钥
openssl pkcs12 -in home.pkcs12 -out home_key.pem -nocerts -nodes
2.后端配置
拷贝密钥库文件home.pkcs12到类路径下:可放在resources目录下,或其他java可查找到的类路径下
application.yml配置:
server:
port: 8000
ssl:
key-store-type: PKCS12
key-store: classpath:home.pkcs12
key-alias: honor
key-store-password: 123456
3.配置nginx
查看nginx是否支持SSL和TLS
nginx -V
nginx version: nginx/1.19.2
built by cl 16.00.40219.01 for 80x86
built with OpenSSL 1.1.1g 21 Apr 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.1g --with-openssl-opt='no-asm no-tests -D_WIN32_WINNT=0x0501' --with-http_ssl_module --with-mail_ssl_module --with-stream_ssl_module
nginx配置
在http段或者server段加入以下配置,http段的配置对所有server生效。
在nginx的config目录下新建cert目录,将证书和密钥文件放入,这里采用了相对路径。
server {
listen 443 ssl;
ssl_certificate cert/home_crt.pem;
ssl_certificate_key cert/home_key.pem;
ssl_protocols TLSv1 TLSv1.1 TLSv1.2;
ssl_ciphers HIGH:!aNULL:!MD5;
ssl_session_cache shared:SSL:10m;
ssl_session_timeout 10m;
server_name localhost;
location / {
root html/dist;
index index.html index.htm;
try_files $uri $uri/ /index.html;
}
error_page 500 502 503 504 /50x.html;
location = /50x.html {
root html;
}
}
转发http
如果需要转发http端口到https,可以加入以下配置:
server {
listen 8888;
server_name localhost;
rewrite ^(.*)$ https://${server_name}$1 permanent;
}