Loading
0

Nginx 使用 Let’s Encrypt 配置 HTTPS 和 HTTP2

具有更快的 HTTPS 传输性能,非常值得开启。开启 HTTP2 的前提是要先开启 HTTPS.

重启 Nginx

sudo systemctl restart nginx

证书有效期是3个月,三个月后需要刷新证书。由于执行更新操作的时候,只有当过期时间小于30天时,才会真的更新。因此一个实际的方式是每星期或者每天执行一次更新操作。

执行这个命令就会刷新

sudo letsencrypt renew

添加 Ctrontab 任务

sudo crontab -e

在 crontab 文件中添加以下任务

1
2
30 2 * * 1 /usr/bin/letsencrypt renew >> /var/log/le-renew.log
35 2 * * 1 /bin/systemctl reload nginx

每星期1的2点30分执行更新操作,2点35分执行Nginx 重启(这样新的证书就会启用),然后输出日志到 /var/log/le-renew.log

在其他服务器上验证域名所有权

也可以在另外一台服务器上执行 letsencrypt,来验证域名的所有权,这时用手动模式

letsencrypt certonly --manual -d www.example.com

会要求去那台真正的服务器上创建一个验证文件,让 letsencrypt 去访问来验证。验证文件还是放在 .well-known 目录下,具体根据提示。

如果当前服务器的80端口被占用,或者防火墙那边不开放80端口,只能是采用这种手动的方式,先将域名指定到一台有80端口的服务器上,验证通过后,再将域名指定回来真正的服务器上面。

安装最新的 openssl

如果 openssl 版本小于1.0.2,是无法启用 http2 的,查看 openssl 版本

openssl version

编译安装

1
2
3
4
5
6
7
8
9
10
11
12
cd /usr/src
wget https://www.openssl.org/source/openssl-1.1.0c.tar.gz
tar -zxf openssl-1.1.0c.tar.gz
cd openssl-1.1.0c
./config
make
make test
make install

# 备份旧的openssl
mv /usr/bin/openssl /root/
ln -s /usr/local/ssl/bin/openssl /usr/bin/openssl

解决错误 ./openssl: error while loading shared libraries: libssl.so.1.1: cannot open shared object file: No such file or directory

1
2
ln -s /usr/local/lib64/libssl.so.1.1 /usr/lib64/libssl.so.1.1
ln -s /usr/local/lib64/libcrypto.so.1.1 /usr/lib64/libcrypto.so.1.1

创建新版本的符号链接

1
2
3
4
# 删除旧的符号链接
rm /bin/openssl
# 添加新的
ln -s /usr/local/bin/openssl /bin/openssl

查看 openssl 版本

openssl version

重启一下 Nginx 就可以支持了,可以在 Chrome 中,网络那边,查看协议是否为 h2

查看 Nginx 版本信息

nginx -V

如果看到是 openssl 1.01 的版本编译的,还需要重新编译 Nginx

平滑升级 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
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
#下载nginx最新版
cd /root
wget http://nginx.org/download/nginx-1.10.2.tar.gz
#解压源码
tar zxvf nginx-1.10.2.tar.gz
#进入源码目录
cd nginx-1.10.2
加上所需参数开始编译
./configure \
--with-openssl=/usr/src/openssl-1.1.0c \
--prefix=/etc/nginx \
--sbin-path=/usr/sbin/nginx \
--modules-path=/usr/lib64/nginx/modules \
--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-file-aio \
--with-threads \
--with-ipv6 \
--with-http_addition_module \
--with-http_auth_request_module \
--with-http_dav_module \
--with-http_flv_module \
--with-http_gunzip_module \
--with-http_gzip_static_module \
--with-http_mp4_module \
--with-http_random_index_module \
--with-http_realip_module \
--with-http_secure_link_module \
--with-http_slice_module \
--with-http_ssl_module \
--with-http_stub_status_module \
--with-http_sub_module \
--with-http_v2_module \
--with-mail \
--with-mail_ssl_module \
--with-stream \
--with-stream_ssl_module \
--with-cc-opt='-O2 -g -pipe -Wall -Wp,-D_FORTIFY_SOURCE=2 -fexceptions -fstack-protector-strong --param=ssp-buffer-size=4 -grecord-gcc-switches -m64 -mtune=generic'
分页阅读: 1 2 3
【声明】:8090安全小组门户(https://www.8090-sec.com)登载此文出于传递更多信息之目的,并不代表本站赞同其观点和对其真实性负责,仅适于网络安全技术爱好者学习研究使用,学习中请遵循国家相关法律法规。如有问题请联系我们:邮箱hack@ddos.kim,我们会在最短的时间内进行处理。