nginx_SSL虚拟主机

SSL虚拟主机

配置基于加密网站的虚拟主机,实现:
域名为www.c.com
该站点通过https访问
通过私钥、证书对该站点所有数据加密

方案
源码安装Nginx时必须使用--with-http_ssl_module参数,启用加密模块,对于需要进行SSL加密处理的站点添加ssl相关指令(设置网站需要的私钥和证书)。
加密算法一般分为对称算法、非对称算法、信息摘要。
对称算法有:AES、DES,主要应用在单机数据加密。
非对称算法有:RSA、DSA,主要应用在网络数据加密。
信息摘要:MD5、sha256,主要应用在数据完整性校验、数据秒传等。

步骤

步骤一:配置SSL虚拟主机
1)生成私钥与证书(公钥)

# cd /usr/local/nginx/conf/
# openssl genrsa > cert.key #生成私钥,genrsa这个单词,gen(生成)rsa(类型的算法)

出现页面如下:
Generating RSA private key, 2048 bit long modulus
......+++
.+++
e is 65537 (0x10001)

# openssl req -new -x509 -key cert.key > cert.pem #生成证书(公钥)
请求 新的证书 类型 私钥 私钥文件名 公钥文件名

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 #国家(要求是2个字母)
State or Province Name (full name) []:guangdong #省份
Locality Name (eg, city) [Default City]:shenzhen #城市
Organization Name (eg, company) [Default Company Ltd]:a #公司
Organizational Unit Name (eg, section) []:b #部门
Common Name (eg, your name or your server's hostname) []:c #名字
Email Address []:[email protected] #邮箱

[root@proxy conf]# ls #公钥私钥在/usr/local/nginx/conf这个目录下
可以看到里面有刚生成的2个文件cert.key cert.pem
------------------------------------------------------
2)修改Nginx配置文件,设置加密网站的虚拟主机

# vim /usr/local/nginx/conf/nginx.conf

98,115s/#// #把98行到115行的注释去掉
...
server {
listen 443 ssl;
server_name www.c.com; #只修改域名

ssl_certificate cert.pem; #这里是证书文件
ssl_certificate_key cert.key; #这里是私钥文件

ssl_session_cache shared:SSL:1m;
ssl_session_timeout 5m;

ssl_ciphers HIGH:!aNULL:!MD5;
ssl_prefer_server_ciphers on;

location / {
root html;
index index.html index.htm;
}
}
...
-------------------------------------------------------------------
3)重启nginx服务

# nginx
# nginx -s reload
--------------------------------------------------------------------
步骤二:客户端验证

1)修改客户端主机192.168.4.10的/etc/hosts文件,进行域名解析
# vim /etc/hosts #把新域名加入这个文件,让客户机自己可以解析域名,才能访问得到
... ...
192.168.4.5 www.a.com www.b.com www.c.com

2)客户端测试:
firefox https://www.c.com/ #一定是https!!!
页面会显示您的连接不安全。信任证书后可以访问。
##############################################################################
常见错误:Nginx如果未开启ssl模块,配置https时提示错误

原因很简单,nginx缺少http_ssl_module模块,编译安装的时候带上--with-http_ssl_module配置就行了,但是现在的情况是我的nginx已经安装过了,怎么添加模块,其实也很简单,往下看: 做个说明:我的nginx的安装目录是/usr/local/nginx这个目录,我的源码包在/root/lnmp_soft/nginx-1.12.2目录

# nginx -s reload
报错信息
nginx: [emerg] the "ssl" parameter requires ngx_http_ssl_module in /usr/local/nginx/conf/nginx.conf:99

# cd /root/lnmp_soft/nginx-1.12.2 #切换到源码包
# nginx -V #查看nginx原有的模块
在configure arguments:后面显示的原有的configure参数如下:
configure arguments: --prefix=/usr/local/nginx --user=nginx --group=nginx

# ./configure --user=nginx --group=nginx --with-http_ssl_module #我们的新配置要Nginx开启SSL模块
# make #这里不要进行make install,否则就是覆盖安装
# mv /usr/local/nginx/sbin/nginx{,.bak} #备份原有已安装好的nginx
# cp objs/nginx /usr/local/nginx/sbin/ #将刚编译好的nginx覆盖掉原有的nginx(这个时候nginx要停止状态)
# make upgrade
# nginx #成功启动
# nginx -V #通过命令查看是否已经加入成功

猜你喜欢

转载自www.cnblogs.com/summer2/p/10787903.html