使用openssl制作证书:
1、服务器单向验证
创建并进入sslkey存放目录
# mkdir /opt/nginx/sslkey
# cd /opt/nginx/sslkey
①、生成RSA密钥:
# openssl genrsa -out key.pem 2048
②、生成一个证书请求
# openssl req -new -key key.pem -out cert.csr
# //会提示输入省份、城市、域名信息等,重要的是,email 一定要是你的域名后缀的你可以拿着这个文件去数字证书颁发机构(即CA)申请一个数字证书。CA会给你一个新的文件cacert.pem,那才是你的数字证书。
如果是自己做测试,就可以用下面这个命令来生成证书:
# openssl req -new -x509 -nodes -out server.crt -keyout server.key
③、修改 nginx 配置
# HTTPS server
#
server {
listen 443;
server_name localhost;
ssl on;
ssl_certificate /opt/nginx/sslkey/server.crt;
ssl_certificate_key /opt/nginx/sslkey/server.key;
ssl_session_timeout 5m;
ssl_protocols SSLv2 SSLv3 TLSv1;
ssl_ciphers ALL:!ADH:!EXPORT56:RC4+RSA:+HIGH:+MEDIUM:+LOW:+SSLv2:+EXP;
ssl_prefer_server_ciphers on;
location / {
root /home/workspace/;
index index.asp index.aspx;
}
}
配置好后,重启nginx,采用 https打开网站,浏览器会提示证书错误,点击继续浏览即可。
2、服务器-客户端双向验证
在nginx 目录下建立ca文件夹,进入ca。
# mkdir newcerts private conf server。
其中newcerts子目录将存放CA签署(颁发)过的数字证书(证书备份目录)。而private目录用于存放CA的私钥。目录conf只是用于存放一些简化参数
用的配置文件,server存放服务器证书文件。
①、在conf目录创建文件openssl.conf配置文件,内容如下:
[ ca ]
default_ca = foo # The default ca section
[ foo ]
dir = /opt/nginx/ca # top dir
database = /opt/nginx/ca/index.txt # index file.
new_certs_dir = /opt/nginx/ca/newcerts # new certs dir
certificate = /opt/nginx/ca/private/ca.crt # The CA cert
serial = /opt/nginx/ca/serial # serial no file
private_key = /opt/nginx/ca/private/ca.key # CA private key
RANDFILE =/opt/nginx/ca/private/.rand # random number file
default_days = 365 # how long to certify for
default_crl_days= 30 # how long before next CRL
default_md = md5 # message digest method to use
unique_subject = no # Set to 'no' to allow creation of
# several ctificates with same subject.
policy = policy_any # default policy
[ policy_any ]
countryName = match
stateOrProvinceName = match
organizationName = match
organizationalUnitName = match
localityName = optional
commonName = supplied
emailAddress = optional
注:你也可以直接修改openssl的配置文件,这样的话后面制作证书的代码中就不用引用这个配置文件了。
