首先配置nginx配置文件/etc/nginx/nginx.conf,启用ssl功能配置如下:
[[email protected] ~]# vim /etc/nginx/nginx.conf
上图中的证书路径都是使用了相对路径,为了避免出现错误,把相对路径修改为绝对路径如下图:
编辑证书openssl.cnf,确保证书路径正确,然后保存退出:
[[email protected] ~]# vim /etc/pki/tls/openssl.cnf
确认CA路径下证书相关目录是否都有:
[[email protected] ~]# cd /etc/pki/CA
[[email protected] CA]# ls #下面四个目录必须都存在(nginx服务器默认已建立)
certs crl newcerts private
[[email protected] CA]# ls private/ #查看是否有私钥文件
[[email protected] CA]# (umask 077; openssl genrsa 2048 > private/cakey.pem) #生成一个私钥证书
Generating RSA private key, 2048 bit long modulus
.............+++
............+++
e is 65537 (0x10001)
[[email protected] CA]# openssl req -new -x509 -key private/cakey.pem -out cacert.pem #针对cakey.pem该私钥证书生成一个自签证书。相关内容输入如下
[[email protected] CA]# ls
cacert.pem certs crl newcerts private
[[email protected] CA]# touch serial
[[email protected] CA]# echo 01 > serial
[[email protected] CA]# touch index.txt
[[email protected] CA]# cd /etc/nginx
[[email protected] nginx]# mkdir ssl
[[email protected] nginx]# cd ssl
[[email protected] ssl]# (umask 077;openssl genrsa 1024 > nginx.key) #生成一个私钥
Generating RSA private key, 1024 bit long modulus
...........................++++++
..++++++
e is 65537 (0x10001)
[[email protected] ssl]# openssl req -new -key nginx.key -out nginx.csr
[[email protected] ssl] openssl ca -in nginx.csr -out nginx.crt -days 3650
[[email protected] ssl]# service nginx restart
[[email protected] ssl]# cd /usr/html/ #进入网页保存目录,新建ssl
[[email protected] html]# ls
50x.html index.html test test1 test2 test3
[[email protected] html]# mkdir ssl
[[email protected] html]# ls
50x.html index.html ssl test test1 test2 test3
[[email protected] html]# cd ssl
[[email protected] ssl]# vim index.html #编辑ssl主页
<h1> SSL 10.109.134.252 SSL </h1>
[[email protected] ssl]# service nginx restart
nginx: the configuration file /etc/nginx/nginx.conf syntax is ok
nginx: configuration file /etc/nginx/nginx.conf test is successful
Stopping nginx: [ OK ]
Starting nginx: [ OK ]
*网页存放路径为/usr/html/ssl,所以/etc/nginx/nginx.conf配置文件中SSL服务中的location中的
root路径为/usr/html/ssl
然后通过客户端访问结果如下:
nginx域名访问功能,只要在nginx.conf配置文件中修改server字段,如下:
增加nginx虚拟主机功能,配置nginx.conf文件增加server字段
重启nginx服务,并在访问的主机host里面增加以下两行域名解析:
10.109.134.252 www.c.com
10.109.134.252 www.a.com
测试结果如下: