Nginx实现https
安装 nginx时,需要将–with-http_ssl_module 模块开启
如果安装nginx是没有安装ssl模块,可以后续添加
创建虚拟主机
cd /usr/local/nginx
mkdir extra
在主配置文件http域加入include标签
http{
......
include /usr/local/nginx/extra/*.conf #只要是以.conf结尾都能被识别
serser{}
}
在对应要进行加密的 server标签中添加以下内容开启 SSL
cd extra/
vim nginx_ssl.conf
server {
listen 443;
server_name www.hdsh.com;
ssl on;
ssl_certificate /usr/local/nginx/conf/ssl/hdsh.crt;
ssl_certificate_key /usr/local/nginx/conf/ssl/hdsh.key;
ssl_session_timeout 5m;
ssl_protocols TLSv1 TLSv1.1 TLSv1.2;
ssl_prefer_server_ciphers on;
ssl_ciphers "EECDH+CHACHA20:EECDH+CHACHA20-draft:EECDH+AES128:RSA+AES128:EECDH+AES256:RSA+AES256:EECDH+3DES:RSA+3DES:!MD5";
location / {
root html/;
index index.html index.htm index.php;
}
}
生成证书文件
cd /usr/local/nginx/conf
mkdir ssl
cd ssl
#注意:在实验环境中可以用命令生成测试,在生产环境中必须要在 https证书厂商注册
openssl genrsa -out hdsh.key 2048
#建立服务器私钥,生成 RSA 密钥
openssl req -new -key atguigu.key -out hdsh.csr
#需要依次输入国家,地区,组织,emai l。最重要的是有一个 common name,可以写你的名字或者域
#名。如果为了 https申请,这个必须和域名吻合,否则会引发浏览器警报。生成的 csr文件交给 CA 签
#名后形成服务端自己的证书
openssl x509 -req -days 365 -sha256 -in atguigu.csr -signkey hdsh.key -out hdsh.crt
设置 http自动跳转 https功能
新增以下 server标签(利用虚拟主机+rewrite的功能)
cd /usr/local/nginx/extra
vim hdsh.conf
server{
listen 80;
server_name www.hdsh.com;
rewrite ^(.*)$ https://www.hdsh.com permanent;
root html;
index index.html index.htm;
}
检测,重启,测试
./nginx -t
./nginx -s reload
浏览器访问www.hdsh.com会重定向到https