这篇文章上次修改于 3388 天前,可能其部分内容已经发生变化,如有疑问可询问作者。
1.生成安全证书
这里有一个shell脚本,可以帮助自动生成证书
#!/bin/sh
# create self-signed server certificate:
read -p "Enter your domain [www.example.com]: " DOMAIN
echo "Create server key..."
openssl genrsa -des3 -out $DOMAIN.key 1024
echo "Create server certificate signing request..."
SUBJECT="/C=US/ST=Mars/L=iTranswarp/O=iTranswarp/OU=iTranswarp/CN=$DOMAIN"
openssl req -new -subj $SUBJECT -key $DOMAIN.key -out $DOMAIN.csr
echo "Remove password..."
mv $DOMAIN.key $DOMAIN.origin.key
openssl rsa -in $DOMAIN.origin.key -out $DOMAIN.key
echo "Sign SSL certificate..."
openssl x509 -req -days 3650 -in $DOMAIN.csr -signkey $DOMAIN.key -out $DOMAIN.crt
echo "TODO:"
echo "Copy $DOMAIN.crt to /etc/nginx/ssl/$DOMAIN.crt"
echo "Copy $DOMAIN.key to /etc/nginx/ssl/$DOMAIN.key"
echo "Add configuration in nginx:"
echo "server {"
echo " ..."
echo " listen 443 ssl;"
echo " ssl_certificate /etc/nginx/ssl/$DOMAIN.crt;"
echo " ssl_certificate_key /etc/nginx/ssl/$DOMAIN.key;"
echo "}"
按照提示 输入你的域名和口令,输入的四次口令要一致
Enter your domain [www.example.com]: www.example.com
Create server key...
Generating RSA private key, 1024 bit long modulus
..........++++++
......++++++
e is 65537 (0x10001)
Enter pass phrase for 9527.shutung.com.key: #输入口令
Verifying - Enter pass phrase for 9527.shutung.com.key: #输入口令
Create server certificate signing request...
Enter pass phrase for 9527.shutung.com.key: #输入口令
Remove password...
Enter pass phrase for 9527.shutung.com.origin.key: #输入口令
writing RSA key
Sign SSL certificate...
Signature ok
subject=/C=US/ST=Mars/L=iTranswarp/O=iTranswarp/OU=iTranswarp/CN=9527.shutung.com
Getting Private key
TODO:
Copy www.example.com.crt to /etc/nginx/ssl/www.examole.com.crt
Copy www.example.com.key to /etc/nginx/ssl/www.example.com.key
Add configuration in nginx:
server {
...
listen 443 ssl;
ssl_certificate /etc/nginx/ssl/www.example.com.crt;
ssl_certificate_key /etc/nginx/ssl/www.example.com.key;
}
在当前目录下会创建出4个文件:
- www.example.com.crt:自签名的证书
- www.example.com.csr:证书的请求
- www.example.com.key:不带口令的Key
- www.example.com.origin.key:带口令的Key
Web服务器需要把 www.example.com.crt 发给浏览器验证,然后用 www.example.com.key 解密浏览器发送的数据,剩下两个文件不需要上传到Web服务器上。
2.配置nginx
server {
listen 443;
ssl on;
ssl_certificate /etc/nginx/ssl/www.example.com.crt;
ssl_certificate_key /etc/nginx/ssl/www.example.com.key;
......
}
3.80端口重定向到443
server {
listen 80;
server_name www.example.com;
rewrite ^(.*) https://$server_name$1 permanent;
}
4.重启nginx
ok,大功告成
已有 6 条评论