考虑到已经在vps中部署了一个博客,而最近https好像比较流行,因此想学习一下如何部署https服务,下面将介绍如何在nginx中部署https
1.生成ssl 服务器证书csr
生成一个加密的ssl服务器私钥
root@gulucn:~/tmp# openssl genrsa -des3 -out server.key 2048
Generating RSA private key, 2048 bit long modulus
..............................................................
..................................................................
..................................................................
.......................+++
.......+++
e is 65537 (0x10001)
Enter pass phrase for server.key:
Verifying - Enter pass phrase for server.key:
生成证书请求(CSR)文件
openssl req -new -key server.key -out server.csr
You are about to be asked to enter information that will be incorporated
into your certificate request.
What you are about to enter is what is called a Distinguished Name or a DN.
There are quite a few fields but you can leave some blank
For some fields there will be a default value,
If you enter '.', the field will be left blank.
-----
Country Name (2 letter code) [XX]:CN
State or Province Name (full name) []:
Locality Name (eg, city) [Default City]:
Organization Name (eg, company) [Default Company Ltd]:gulucn
Organizational Unit Name (eg, section) []:gulu
Common Name (eg, your name or your server's hostname) []:gulucn.com
Email Address []:[email protected]
Please enter the following 'extra' attributes
to be sent with your certificate request
A challenge password []:
An optional company name []:
2.到提供免费ssl证书机构签发证书
目前比较出名的有wosign与startssl网站可以提供域名认证的ssl服务器证书,该类型证书申请比较简单,只需要验证域名所有者,签发速度比较快,wosign一般只需要十几分钟即可。
但这两个网站签发机制还是不一样:
- wosign注册比较简单,只需要填写一些简单的资料即可,并且验证域名方式有两种:whois中的邮箱地址以及在自身域名中提供wosign提供的验证页面即可。wosign签发的证书可以支持多个域名,需要在申请时全部填写,证书有效期可以申请3年,上传csr证书后,就可以等待结果。
- startssl注册相对严格一些,需要填写地址及手机号码等信息,如果信息不够详细,会另外发送一封邮件让你补充详细的信息。一旦通过后,startssl会先提供一个登录证书(不是要申请的服务器证书,有效期只有一年),然后在浏览器导入证书就可以使用startssl的控制面板内容,签发的证书只能是一年有效期,并且只能申请两个域名。每年需要重新申请登录证书与ssl服务器证书。
3.部署ssl证书
补全证书链
由于本人是通过startssl申请证书,证书链中可能会缺少中级机构的证书(可以使用curl测试,会报 StartCom Class 1 Primary Intermediate Server CA 缺失),因此需要补全。
用以下命令获取中级证书的pem内容
wget https://www.startssl.com/certs/sub.class1.server.ca.pem
然后把文件内容追加到server.crt证书即可
cat sub.class1.server.ca.pem >> server.crt
去除私钥密码
由于刚才创建的私钥带有密码,如果直接使用,每次nginx启动时都需要输入密码,为了这种情况,可以导出一个无密码的私钥
openssl rsa -in server.key -out server.key.unsecure
部署到nginx
修改/etc/nginx/sites-enabled/default,去掉https server 注释内容,如
server {
listen 443 ssl spdy;
server_name gulucn.com www.gulucn.com;
root /usr/share/nginx/html;
index index.html index.htm;
ssl_certificate /var/www/key/server.crt;
ssl_certificate_key /var/www/key/server.key.unsecure;
ssl_session_timeout 5m;
ssl_protocols TLSv1 TLSv1.1 TLSv1.2;
ssl_ciphers "HIGH:!aNULL:!MD5 or HIGH:!aNULL:!MD5:!3DES";
ssl_prefer_server_ciphers on;
}
4.其他
重定向gulucn.com的http到https
可以按以下配置
server {
listen 80 ;
server_name gulucn.com www.gulucn.com;
rewrite ^ https://$server_name$request_uri? permanent;
}
为网部配置HSTS
可以在上面的https server段配置增加以下内容
add_header Strict-Transport-Security "max-age=63072000";
add_header X-Frame-Options SAMEORIGIN;
add_header X-Content-Type-Options nosniff;
add_header X-XSS-Protection "1; mode=block";
测试https服务器安全性
可以到https://www.ssllabs.com/ssltest/ 网站测试