配置ssl证书
- 证书说明:这里使用的证书是阿里云个人一年免费的SSL证书,只需要在阿里云上申请即可获得。
- web服务器说明:这里采用的web服务器是Nginx服务器。
- 服务器操作系统说明:这里使用的是Centos 7.x系统。
- 终端登陆工具与FTP工具说明:这里使用的是Xshell与Xftp工具。
下载证书
登陆到阿里云控制台,今天证书安全下载界面,选择Nginx/Tengine
,下载证书,并解压到桌面。
上传证书到服务器
登陆到服务器,并进入到Nginx目录:
创建证书存放目录:
使用Xftp将证书上传至新建的目录下:
配置Nginx使其支持SSL证书
进入nginx下的conf.d目录,创建一个新的.conf文件,命名为ssl.域名.conf(命名可任意,但需以.conf结尾)。
为新建的.conf文件添加如下代码:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32server {
listen 443;
# 修改为你的域名
server_name 域名.com www.域名.com;
ssl on;
# root目录,即为你网站的存放目录
root /mnt/www/域名.com/public_html/wordpress/;
index index.html index.htm index.php;
# 将下面两行修改为SSL证书存放路径
ssl_certificate /etc/nginx/ssl/域名.com/214190949470644.pem;
ssl_certificate_key /etc/nginx/ssl/域名.com/214190949470644.key;
ssl_session_timeout 5m;
ssl_ciphers ECDHE-RSA-AES128-GCM-SHA256:ECDHE:ECDH:AES:HIGH:!NULL:!aNULL:!MD5:!ADH:!RC4;
ssl_protocols TLSv1 TLSv1.1 TLSv1.2;
ssl_prefer_server_ciphers on;
# 以下为Nginx的基本配置
location / {
try_files $uri $uri/ /index.php?$query_string;
}
location ~ \.php$ {
fastcgi_pass 127.0.0.1:9000;
fastcgi_index index.php;
include fastcgi.conf;
}
}
- 测试下配置的正确性,成功后重启下Nginx,再打开浏览器测试HTTPS是否能正常访问。
另外说明:可以看到目前conf.d目录里有3个以.conf结尾的配置文件,basic.conf为基本的解析配置文件;域名.conf为不使用SSL证书的http配置文件;ssl.域名.com为新增加的使用SSL证书的HTTPS配置文件。因此,在目前未配置301重定向的情况下,打开浏览器测试网站的时候,可以发现网站即可正常访问http,也可以正常访问https。
为Nginx配置301重定向
说明:在上面说了,目前网站的状态应该是http和https都可以正常访问。现在则为Nginx配置一下301重定向,这样当访问网站的时候就会自动定向到https,而不会在通过http访问网站了。
进入Nginx配置目录,打开http的配置文件
编辑域名.conf配置文件
1
2
3
4
5
6server {
listen 80;
# 将“域名”改为你自己的域名
server_name 域名.com www.域名.com;
return 301 https://$host$request_uri;
}
注意:这里编辑的是域名.conf,而不是ssl.域名.conf!!!
- 重启Nginx,打开浏览器测试
1
systemctl restart nginx
配置至此,网站已经只会再通过https访问了,但是仍然还不会使用http2.0协议.
配置HTTP2
查看网站协议
打开浏览器,将审查元素打开,点击Network选项卡,将Protocol调出来,查看传输协议。目前显示的为http/1.1,传输协议。
查看Nginx版本,查看OpenSSL版本
目前显示的版本是nginx/1.10.2,OpenSSL 1.0.1e-fips,因为之前安装Nginx时,使用的时系统默认的源,然而Centos7自带的Nginx版本并不够新,想要支持使用http2.0协议,OpenSS必须升级到2.0版本以上才可以。因此要对Nginx重新编译。
编译安装Nginx和openssl
安装编译过程中需要使用的工具
1
yum install wget curl perl gcc pcre-devel zlib-devel make -y
下载Nginx源和openssl源
1
2
3
4
5
6
7
8
9
10//下载openssl新版的源和Nginx新版源
wget https://www.openssl.org/source/openssl-1.0.2l.tar.gz http://nginx.org/download/nginx-1.11.10.tar.gz
//解压两个压缩包
tar zxvf nginx-1.11.10.tar.gz
tar zxvf openssl-1.0.2l.tar.gz
//重命名nginx和openssl
mv nginx-1.11.10/ nginx
mv openssl-1.0.2l/ openssl编译nginx
首先卸载原先版本的额nginx
1
yum remove nginx -y
配置编译nginx
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24//进入nginx和openssl所在目录。
cd ~
//将nginx和openssl移动到/usr/local/src文件夹
mv nginx/ openssl/ /usr/local/src/
//进入/etc/local/src/nginx目录下
cd /usr/local/src/nginx
//配置nginx
./configure --prefix=/etc/nginx --sbin-path=/usr/sbin/nginx --conf-path=/etc/nginx/nginx.conf --error-log-path=/var/log/nginx/error.log --http-log-path=/var/log/nginx/access.log --pid-path=/var/run/nginx.pid --lock-path=/var/run/nginx.lock --http-client-body-temp-path=/var/cache/nginx/client_temp --http-proxy-temp-path=/var/cache/nginx/proxy_temp --http-fastcgi-temp-path=/var/cache/nginx/fastcgi_temp --http-uwsgi-temp-path=/var/cache/nginx/uwsgi_temp --http-scgi-temp-path=/var/cache/nginx/scgi_temp --user=nginx --group=nginx --with-http_ssl_module --with-http_realip_module --with-http_addition_module --with-http_sub_module --with-http_dav_module --with-http_flv_module --with-http_mp4_module --with-http_gunzip_module --with-http_gzip_static_module --with-http_random_index_module --with-http_secure_link_module --with-http_stub_status_module --with-http_auth_request_module --with-threads --with-stream --with-stream_ssl_module --with-http_slice_module --with-mail --with-mail_ssl_module --with-file-aio --with-http_v2_module --with-stream_realip_module --with-openssl=/usr/local/src/openssl
//使用make命令编译一下,时间较长
make
//编译完成后执行下make insatll
make install
//为新编译的nginx添加用户,创建配置目录
useradd nginx && mkdir /etc/nginx/conf.d
//创建nginx缓存目录,并设置一些权限
mkdir /var/cache/nginx && chown nginx:root /var/cache/nginx
//删除用不着的文件
rm -rf /usr/local/src/nginx && rm -rf /usr/local/src/openssl && rm -rf /var/cache/yum继续配置nginx,并启动它
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23//配置nginx服务文件
vim /usr/lib/systemd/system/nginx.service
//为nginx.service添加配置代码
[Unit]
Description=nginx - high performance web server
Documentation=http://nginx.org/en/docs/
After=network-online.target remote-fs.target nss-lookup.target
Wants=network-online.target
[Service]
Type=forking
PIDFile=/run/nginx.pid
ExecStartPre=/usr/sbin/nginx -t -c /etc/nginx/nginx.conf
ExecStart=/usr/sbin/nginx -c /etc/nginx/nginx.conf
ExecReload=/bin/kill -s HUP $MAINPID
ExecStop=/bin/kill -s QUIT $MAINPID
[Install]
WantedBy=multi-user.target
//启动下nginx,再设置开机启动
systemctl start nginx
systemctl enable nginx
systemctl status nginx编辑nginx.conf文件
1
2
3
4
5
6
7
8
9
10
11//进入nginx目录
cd /etc/nginx/
//为nginx.conf添加代码
vim nginx.conf
//在nginx.conf中的gzip on代码下面一行添加如下代码
include /etc/nginx/conf.d/*.conf;
//重启nginx
systemctl restart nginx
配置http2.0协议
1 | //编辑ssl.域名.conf文件 |
测试
至此,http2.0协议配置完成,打开浏览器查看传输协议。