配置站点使用 https,并且将 http 重定向至 https。
1. nginx 的 ssl 模块安装
查看 nginx 是否安装 http_ssl_module 模块,如果出现 configure arguments: --with-http_ssl_module, 则已安装(下面的步骤可以跳过,进入 nginx.conf 配置)。
$ /usr/local/nginx/sbin/nginx -V
配置 ssl 模块
$ cd nginx-1.14.1
$ ./configure --prefix=/usr/local/nginx --with-http_ssl_module
使用 make 命令编译(使用make install会重新安装nginx),此时当前目录会出现 objs 文件夹。
用新的 nginx 文件覆盖当前的 nginx 文件。
$ cp ./objs/nginx /usr/local/nginx/sbin/
再次查看安装的模块(configure arguments: --with-http_ssl_module说明ssl模块已安装)。
$ /usr/local/nginx/sbin/nginx -V
nginx version: nginx/1.14.1
…
configure arguments: –with-http_ssl_module
2. nginx.conf
配置
ssl证书文件(server.pem、server.key)放在/data/cert/目录下,nginx.conf配置如下:
server {
listen 5000 ssl;
#ssl证书的pem文件路径
ssl_certificate /data/cert/server.pem;
#ssl证书的key文件路径
ssl_certificate_key /data/cert/server.key;
location / {
root D:\webPage;
#root D:\gitcode\Web\base-js;
#autoindex on;
}
}
3. 重启Nginx
nginx -t
nginx -s reload
另:keytool制作证书
1.keytool生成密钥
keytool -genkey -alias uat -keypass password -keyalg RSA -keysize 1024 -validity 365 -keystore E:/keystore/uat.keystore -storepass password
E:/keystore/uat.keystore为生成文件的位置,密码为 password
2.导出证书
keytool -export -alias uat -keystore E:/keystore/uat.keystore -storepass password -rfc -file E:/keystore/uat.cer
E:/keystore/uat.cer 为生成文件的位置
3.解析出密钥:使用Java代码辅助
try {
BASE64Encoder encoder = new BASE64Encoder();
//读取文件内容
FileInputStream is = new FileInputStream("E:/JXD/keystore/uat.keystore");
KeyStore ks = KeyStore.getInstance("JKS");
ks.load(is, "password".toCharArray());
PrivateKey key = (PrivateKey) ks.getKey("uat", "password".toCharArray());
String encoded = encoder.encode(key.getEncoded());
System.out.println(encoded);
is.close();
} catch (Exception e){
}
将输出的内容保存到自己新建的key文件,可以新建一个文本文档然后改一下后缀