在部署过程中,用户要求把http改成https,并且不花钱不开放外网,只是在内网上使用。
这个需求只能是通过ssl自签名方法进行解决,方法如下:
1、在linux系统中安装nginx;
2、在nginx安装目录下输入命令:mkdir ssl;
3、在ssl目录下创建文件:touch MyCompanyCA.cnf
并在配置文件输入以下内容:
[ req ]
distinguished_name = req_distinguished_name
x509_extensions = root_ca
[ req_distinguished_name ]
# 配置证书信息
countryName = CN (2 letter code)
countryName_min = 2
countryName_max = 2
stateOrProvinceName = SiChuan
localityName = ChengDu
0.organizationName = Mycompany
organizationalUnitName = technology
commonName = develop
commonName_max = 64
emailAddress = xxxxxxxx@gmail.com
emailAddress_max = 64
[ root_ca ]
basicConstraints = critical, CA:true
4、继续创建文件:touch MyCompanyLocalhost.ext
并在文件中输入以下内容:
subjectAltName = @alt_names
extendedKeyUsage = serverAuth
[alt_names]
# 域名,如有多个用DNS.2,DNS.3…来增加
DNS.2=你的域名
# IP地址
IP.1 = 你的ip
5、保存以上2个文件,然后输入以下脚本:
openssl req -x509 -newkey rsa:2048 -out MyCompanyCA.cer -outform PEM -keyout MyCompanyCA.pvk -days 10000 -verbose -config MyCompanyCA.cnf -nodes -sha256 -subj "/CN=MyCompany CA"
openssl req -newkey rsa:2048 -keyout MyCompanyLocalhost.pvk -out MyCompanyLocalhost.req -subj /CN=localhost -sha256 -nodes
openssl x509 -req -CA MyCompanyCA.cer -CAkey MyCompanyCA.pvk -in MyCompanyLocalhost.req -out MyCompanyLocalhost.cer -days 10000 -extfile MyCompanyLocalhost.ext -sha256 -set_serial 0x1111
6、等到的目录结构如下:
7、先给你的nginx.conf备份(改配置文件先备份是个好习惯),cp nginx.conf nginx.conf_bak
8、配置nginx.conf:输入以下代码,文件路径修改为你的路径:
9、重启nginx :
nginx -s reload
10、登录相关网站,把证书设置为受信任的即可。