操作环境

  • 硬件:华为云服务器
  • 操作系统:Rocky Linux 9.0 64bit
  • 服务器软件:nginx 1.24.0,php 8.2.6,openssl 3.0.7,WordPress6.2.2

问题背景

最近,我在优化我的WordPress网站过程中,使用GTmetrix进行网站速度测试时,提示我的网站当前使用的是HTTP1.1,建议我开启HTTP2,因为HTTP2比HTTP1在性能上更优越。因此,我决定给我的nginx服务器开启HTTP2,现将具体开启过程记录如下。

操作过程

1.用命令curl --V查看curl版本。

[root@hecs-288529 ~]# curl -V
[2023-05-30 21:00:30]  
curl 7.76.1 (x86_64-redhat-linux-gnu) libcurl/7.76.1 OpenSSL/3.0.7 zlib/1.2.11 brotli/1.0.9 libidn2/2.3.0 libpsl/0.21.1 (+libidn2/2.3.0) libssh/0.10.4/openssl/zlib nghttp2/1.43.0
[2023-05-30 21:00:30]  Release-Date: 2021-04-14
[2023-05-30 21:00:30]  Protocols: dict file ftp ftps gopher gophers http https imap imaps ldap ldaps mqtt pop3 pop3s rtsp scp sftp smb smbs smtp smtps telnet tftp 
[2023-05-30 21:00:30]  Features: alt-svc AsynchDNS brotli GSS-API HTTP2 HTTPS-proxy IDN IPv6 Kerberos Largefile libz NTLM NTLM_WB PSL SPNEGO SSL TLS-SRP UnixSockets

2.使用curl测试网站是否支持HTTP2。


具体指令: curl -I -k --http2 https://localhost

[2023-05-30 21:00:30]  [root@hecs-288529 ~]# curl -I -k --http2 https://localhost
curl -I -k --http2 https://localhost
[2023-05-30 21:02:44]  
HTTP/1.1 301 Moved Permanently

[2023-05-30 21:02:44]  Server: nginx/1.24.0

[2023-05-30 21:02:44]  Date: Tue, 30 May 2023 13:02:44 GMT

[2023-05-30 21:02:44]  Content-Type: text/html; charset=UTF-8

[2023-05-30 21:02:44]  Connection: keep-alive

[2023-05-30 21:02:44]  X-Powered-By: PHP/8.2.6

[2023-05-30 21:02:44]  X-Redirect-By: WordPress

[2023-05-30 21:02:44]  Location: https://wslibai.com/

[2023-05-30 21:02:44]  Referrer-Policy: no-referrer-when-downgrade

[2023-05-30 21:02:44]  

查看指令输出,发现网站目前是HTTP/1.1。

3.修改nginx.conf文件,修改 HTTPS 部分内容。


将HTTPS的server块中 listen 443 ssl;修改为 listen 443 ssl http2 default_server;

 #HTTPS server

    server {
        listen 443 ssl http2 default_server;
        ......
    }

4.重启nginx服务。


具体指令: systemctl restart nginx。发现重启失败。

[2023-05-30 21:02:44]  [root@hecs-288529 ~]# systemctl restart nginx
[2023-05-30 21:07:12]  
Job for nginx.service failed because the control process exited with error code.
[2023-05-30 21:07:12]  See "systemctl status nginx.service" and "journalctl -xeu nginx.service" for details.

5.查看详细错误信息。


具体指令: systemctl status nginx

[2023-05-30 21:07:12]  [root@hecs-288529 ~]# systemctl status nginx
[2023-05-30 21:07:28]  

?nginx.service - nginx
[2023-05-30 21:07:28]       Loaded: loaded (/usr/lib/systemd/system/nginx.service; enabled; preset: disabled)
[2023-05-30 21:07:28]      Drop-In: /usr/lib/systemd/system/nginx.service.d
[2023-05-30 21:07:28]               php-fpm.conf
[2023-05-30 21:07:28]       Active: failed (Result: exit-code) since Tue 2023-05-30 21:07:12 CST; 15s ago
[2023-05-30 21:07:28]     Duration: 26min 51.616s
[2023-05-30 21:07:28]      Process: 12585 ExecStart=/usr/local/nginx/sbin/nginx (code=exited, status=1/FAILURE)
[2023-05-30 21:07:28]          CPU: 5ms
[2023-05-30 21:07:28]  
[2023-05-30 21:07:28]  May 30 21:07:12 hecs-288529 systemd[1]: Starting nginx...
[2023-05-30 21:07:28]  May 30 21:07:12 hecs-288529 nginx[12585]: nginx: [emerg] the "http2" parameter requires ngx_http_v2_module in /usr/local/nginx/conf/nginx.conf:111
[2023-05-30 21:07:28]  May 30 21:07:12 hecs-288529 systemd[1]: nginx.service: Control process exited, code=exited, status=1/FAILURE
[2023-05-30 21:07:28]  May 30 21:07:12 hecs-288529 systemd[1]: nginx.service: Failed with result 'exit-code'.
[2023-05-30 21:07:28]  May 30 21:07:12 hecs-288529 systemd[1]: Failed to start nginx.
[2023-05-30 21:07:28]  

查看输出信息,发现缺少 ngx_http_v2_module

6.检查nginx的ngx_http_v2_module模块安装情况。


先用命令 cd /usr/local/nginx/sbin进入安装目录下sbin子目录(安装目录不同的,请自行修改),再用命令 ./nginx -V查看编译配置参数(注意:此处要用大写V),发现没有配置 ngx_http_v2_module模块,问题找到。

[2023-05-30 21:09:48]  [root@hecs-288529 ~]# cd /usr/local/nginx/sbin
[root@hecs-288529 sbin]# ./nginx -V
[2023-05-30 21:27:11]  
nginx version: nginx/1.24.0
[2023-05-30 21:27:11]  built by gcc 11.3.1 20221121 (Red Hat 11.3.1-4) (GCC) 
[2023-05-30 21:27:11]  built with OpenSSL 3.0.7 1 Nov 2022
[2023-05-30 21:27:11]  TLS SNI support enabled
[2023-05-30 21:27:11]  configure arguments: --prefix=/usr/local/nginx --with-http_stub_status_module --with-http_ssl_module --user=nginx --group=nginx

7.进入nginx的源代码目录重新配置编译参数并编译安装。


具体命令如下,输出略。(源代码目录不同的需要相应的修改)

cd  /usr/local/src/nginx-1.24.0
./configure --prefix=/usr/local/nginx --with-http_stub_status_module --with-http_ssl_module  --with-http_gzip_static_module --with-http_V2_modiule --user=nginx --group=nginx
make && make install

8.重启nginx服务。


具体指令: systemctl restart nginx

[root@hecs-288529 nginx-1.24.0]# systemctl restart nginx
[root@hecs-288529 nginx-1.24.0]# systemctl status nginx
[2023-05-30 21:39:43]  

 nginx.service - nginx
[2023-05-30 21:39:43]       Loaded: loaded (/usr/lib/systemd/system/nginx.service; enabled; preset: disabled)
[2023-05-30 21:39:43]      Drop-In: /usr/lib/systemd/system/nginx.service.d
[2023-05-30 21:39:43]               php-fpm.conf
[2023-05-30 21:39:43]       Active: active (running) since Tue 2023-05-30 21:39:39 CST; 3s ago
[2023-05-30 21:39:43]      Process: 15322 ExecStart=/usr/local/nginx/sbin/nginx (code=exited, status=0/SUCCESS)
[2023-05-30 21:39:43]     Main PID: 15323 (nginx)
[2023-05-30 21:39:43]        Tasks: 3 (limit: 23205)
[2023-05-30 21:39:43]       Memory: 3.3M
[2023-05-30 21:39:43]          CPU: 12ms
[2023-05-30 21:39:43]       CGroup: /system.slice/nginx.service
[2023-05-30 21:39:43]               15323 "nginx: master process /usr/local/nginx/sbin/nginx"
[2023-05-30 21:39:43]               15324 "nginx: worker process"
[2023-05-30 21:39:43]               15325 "nginx: worker process"
[2023-05-30 21:39:43]  
[2023-05-30 21:39:43]  May 30 21:39:39 hecs-288529 systemd[1]: Starting nginx...
[2023-05-30 21:39:43]  May 30 21:39:39 hecs-288529 systemd[1]: Started nginx.
[2023-05-30 21:39:43]  

9.再次使用curl测试网站是否支持HTTP2。


具体指令: curl -I -k --http2 https://localhost

[root@hecs-288529 nginx-1.24.0]# curl -I -k --http2 https://localhost
[2023-05-30 21:40:02]  
HTTP/2 301 

[2023-05-30 21:40:02]  server: nginx/1.24.0

[2023-05-30 21:40:02]  date: Tue, 30 May 2023 13:40:02 GMT

[2023-05-30 21:40:02]  content-type: text/html; charset=UTF-8

[2023-05-30 21:40:02]  location: https://wslibai.com/

[2023-05-30 21:40:02]  x-powered-by: PHP/8.2.6

[2023-05-30 21:40:02]  x-redirect-by: WordPress

[2023-05-30 21:40:02]  referrer-policy: no-referrer-when-downgrade

[2023-05-30 21:40:02]  

查看输出,发现HTTP/2已经开启。

10.开启HTTP/2以后网站性能测试结果。


Archived Performance Report for: https://wslibai.com/ | GTmetrix

经开启HTTP/2前后对比测试,发现开启后性能提升很明显,总体评级由 B升为 ATime to First Byte (TTFB)由之前的1000ms左右降到631ms, Fully Loaded Time由之前的3s降到2s。(只可惜当时没有保存开启HTTP2之前的测试结果)

参考资料

h2setup