靓靓小知识-Nginx配置ssl证书
请先安装nginx,确保正常访问,且安装了http_ssl_module模块
1.准备域名和证书
一般证书是key+pem后缀名的文件,或者key+crt后缀名的文件
2.新建cert目录,存放证书文件
3.在vhosts下新建conf文件
server {
listen 80;
server_name a.test.com;
index index.html index.htm;
root /u01/dist;
rewrite ^/(.*) https://a.test.com/$1 permanent;
location ~* ^.+\.(eot|ttf|otf|woff|svg)$ {
expires max;
}
}
server {
listen 443 ssl;
server_name a.test.com;
ssl_certificate "/u01/nginx117/conf/cert/a.crt";
ssl_certificate_key "/u01/nginx117/conf/cert/a.key";
ssl_session_cache shared:SSL:10m;
ssl_session_timeout 10m;
ssl_ciphers HIGH:!aNULL:!MD5;
ssl_prefer_server_ciphers on;
location / {
add_header 'Access-Control-Allow-Origin' 'http://127.0.0.1:81';
add_header 'Access-Control-Allow-Methods' 'GET, POST, OPTIONS';
#add_header 'Access-Control-Allow-Headers' 'DNT,X-Mx-ReqToken,Keep-Alive,User-Agent,X-Requested-With,If-Modified-Since,Cache-Control,Content-Type,Authorization';
root /u01/dist;
try_files $uri $uri/ @router;
if ($request_method = 'OPTIONS') {
return 204;
}
}
location @router {
rewrite ^.*$ /index.html last;
}
location ^~ /api/ {
proxy_pass http://127.0.0.1:81/;
}
location /logs {
alias /data/logs;
autoindex on;
}
location /static/(.*).(js|jpg|jpeg|png|css) {
expires 12h;
}
}
4.测试nginx并重新加载
/u01/nginx117/sbin/nginx -t
/u01/nginx117/sbin/nginx -s reload
5.遇到的问题1——证书文件是ca-bundle和crt文件
首先需要将颁发的crt和ca-bundle文件合并成一个新的crt文件
cat xxx.crt xxx.ca-bundle > new.crt
然后用新的crt做证书文件,key还是用之前生成的!
就是把ca-bundle添加到crt文件的底部,把两个文件的内容合并成一个。
如果在windows下,可以用notepad++ 来编辑,crt结尾不需要主动换行。本身的文件自带了换行,可以直接在那一行添加。
6.遇到的问题2——nginx的key和crt不匹配,提示证书不一致
openssl x509 -noout -modulus -in your-cert.crt | openssl md5
openssl x509 -noout -modulus -in your-cert.key | openssl md5
#比较验证输出的校验码是否相同
7.遇到的问题3——GoDaddy SSL证书Private key问题
文件是UTF-8withBOM,要转换成utf-8,用notepad++转换下