内容开发的功能使用http通信, 部署到线上环境时, 为防止http劫持, 需要将替换为https, 不打算修改底层代码, 直接使用nginx做反向代理来解决问题
实现细节
. 安装openssl, 并检查版本是不是1.1.1, 低于该版本使用https`访问时可能会出现ERR_SSL_PROTOCOL_ERROR错误
| 1 | openssl version | 
- 生成私钥 - 1 - openssl genrsa -out key.pem 2048 
- 生成公钥, 生成过程中, 按照提示输入相应的内容即可 - 1 - openssl req -new -sha256 -key key.pem -out csr.pem 
- 自签名 - 1 - openssl x509 -req -in csr.pem -signkey key.pem -out cert.pem 
- 配置nginx.conf文件 - 1 
 2
 3
 4
 5
 6
 7
 8
 9
 10
 11
 12
 13
 14- server { 
 listen 80;
 listen 443 ssl;
 ssl_certificate cert.pem;
 ssl_certificate_key key.pem;
 ssl_session_timeout 5m;
 server_name localhost;
 location / {
 proxy_pass http://ip:port; # 目标地址
 proxy_set_header Host $host;
 proxy_set_header X-Real-IP $remote_addr; # 请求真实地址
 proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
 }
 }
- docker部署nginx, 其中 - key.pem就是你的私钥文件,- cert.pem就是证书文件- 1 
 2
 3
 4
 5
 6
 7
 8- nginx配置文件server部分 
 docker run \
 -d -p $port443:443 \
 --name $name \
 -v ${pwd}/conf/nginx.conf:/etc/nginx/nginx.conf \
 -v ${pwd}/key.pem:/etc/nginx/key.pem \
 -v ${pwd}/cert.pem:/etc/nginx/cert.pem \
 nginx
- 使用自签名证书在访问时浏览器会提示连接不安全, 可以使用https证书工具之类的工具申请证书的证书 
 
         
              