您的位置:首页 > 运维架构 > Nginx

Linux命令:nginx启用SSL功能设置及虚拟主机

2017-08-07 14:21 429 查看
首先配置nginx配置文件/etc/nginx/nginx.conf,启用ssl功能配置如下:

[root@lamp ~]# vim /etc/nginx/nginx.conf



上图中的证书路径都是使用了相对路径,为了避免出现错误,把相对路径修改为绝对路径如下图:



编辑证书openssl.cnf,确保证书路径正确,然后保存退出:
[root@lamp ~]# vim /etc/pki/tls/openssl.cnf



确认CA路径下证书相关目录是否都有:
[root@lamp ~]# cd /etc/pki/CA
[root@lamp CA]# ls #下面四个目录必须都存在(nginx服务器默认已建立)
certs crl newcerts private
[root@lamp CA]# ls private/ #查看是否有私钥文件
[root@lamp CA]# (umask 077; openssl genrsa 2048 > private/cakey.pem) #生成一个私钥证书

Generating RSA private key, 2048 bit long modulus
.............+++
............+++
e is 65537 (0x10001)
[root@lamp CA]# openssl req -new -x509 -key private/cakey.pem -out cacert.pem #针对cakey.pem该私钥证书生成一个自签证书。相关内容输入如下



[root@lamp CA]# ls

cacert.pem certs crl newcerts private

[root@lamp CA]# touch serial
[root@lamp CA]# echo 01 > serial
[root@lamp CA]# touch index.txt
[root@lamp CA]# cd /etc/nginx
[root@lamp nginx]# mkdir ssl
[root@lamp nginx]# cd ssl
[root@lamp ssl]# (umask 077;openssl genrsa 1024 > nginx.key) #生成一个私钥
Generating RSA private key, 1024 bit long modulus
...........................++++++
..++++++
e is 65537 (0x10001)
[root@lamp ssl]# openssl req -new -key nginx.key -out nginx.csr



[root@lamp ssl] openssl ca -in nginx.csr -out nginx.crt -days 3650



[root@lamp ssl]# service nginx restart



[root@lamp ssl]# cd /usr/html/ #进入网页保存目录,新建ssl
[root@lamp html]# ls
50x.html index.html test test1 test2 test3
[root@lamp html]# mkdir ssl
[root@lamp html]# ls
50x.html index.html ssl test test1 test2 test3
[root@lamp html]# cd ssl
[root@lamp ssl]# vim index.html #编辑ssl主页
<h1> SSL 10.109.134.252 SSL </h1>

[root@lamp 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
测试结果如下:





内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签:  SSL Nginx