Kích hoạt HTTP/2 với Nginx

Chỉ với một thao tác nhỏ, bạn có thể tăng tốc website lên nhiều lần so với hiện tại. Tại sao không thử nhỉ?

Trong bài viết trước mình đã giới thiệu về HTTP/2 và những lợi ích mà nó mang lại. Ngay từ bây giờ, bạn đã có thể áp dụng giao thức HTTP/2 cho website của mình rồi đó.

Một điều lưu ý nhỏ là HTTP/2 chỉ hoạt động khi cả web server và trình duyệt cùng hỗ trợ giao thức, nếu không kết nối sẽ tự động quay lại HTTP/1.1.

– Về phương diện trình duyệt, các trình duyệt phổ biến như Chrome, Firefox, Edge, Safari… đều đã hỗ trợ giao thức HTTP/2. Tuy vậy, tất cả lại chỉ hỗ trợ HTTP/2 qua kết nối SSL/TLS.

Như vậy, để tận hưởng lợi ích của HTTP/2, website cần phải sử dụng SSL.

– Về phương diện Hosting/Server, ngoài SSL, bạn cần đảm bảo hệ thống webserver đã thiết lập HTTP/2.

Nếu bạn dùng Hosting, hãy liên hệ hỏi nhà cung cấp cho chắc chắn. Với StableHost, Hawk Host, AZDIGI thì yên tâm dùng mà không cần hỏi.

Nếu bạn dùng VPS/Cloud Server, hãy kích hoạt HTTP/2 cho webserver thông qua các giao thức trực tiếp/module cài đặt (cụ thể tùy vào từng loại webserver).

Bài viết này mình sẽ hướng dẫn thiết lập HTTP/2 trên VPS sử dụng Nginx webserver (ví dụ HocVPS, Centmin Mod…) tương thích với các trình duyệt phổ biến hiện nay. Bạn cần:

  • Biên dịch mã nguồn Nginx tích hợp OpenSSL phiên bản 1.0.2 trở lên (làm 1 lần duy nhất)
  • Cấu hình SSL HTTP/2 trong Nginx Conf
Nếu bạn đã cài đặt HTTPS theo hướng dẫn cài đặt chứng chỉ SSL hoặc Let’s Encrypt thì chỉ cần tiến hành biên dịch lại mã nguồn Nginx (bước 1) là đã thiết lập được HTTP/2.

1. Tích hợp OpenSSL 1.1.0f vào Nginx

Bạn cần tích hợp OpenSSL v1.0.2 trở lên vào Nginx. HocVPS lựa chọn phiên bản mới nhất OpenSSL v1.1.0f để tích hợp vào Nginx 1.12.0. Bạn có thể tham khảo các phiên bản khác tại OpenSSL Source.

Để làm được điều đó, bạn cần phải biên dịch lại mã nguồn Nginx tích hợp OpenSSL v1.1.0f chứ không thể cài đặt như 1 module riêng lẻ.

1.1. Chuẩn bị

– Kiểm tra thông tin Nginx. Hiện tại, VPS sử dụng Nginx phiên bản mới nhất 1.12 tích hợp OpenSSL 1.0.1e (mặc định, ra đời từ năm 2013) cùng với các tham số Configure Arguments như phần bôi đỏ.

Bạn sẽ cần copy lại thông tin tham số này để chạy lệnh compile source Nginx ở bước sau.

# nginx -V
 nginx version: nginx/1.12.0
 built by gcc 4.4.7 20120313 (Red Hat 4.4.7-17) (GCC)
 built with OpenSSL 1.0.1e-fips 11 Feb 2013
 TLS SNI support enabled
 configure arguments: --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-compat --with-file-aio --with-threads --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_realip_module --with-stream_ssl_module --with-stream_ssl_preread_module --with-cc-opt='-O2 -g -pipe -Wall -Wp,-D_FORTIFY_SOURCE=2 -fexceptions -fstack-protector --param=ssp-buffer-size=4 -m64 -mtune=generic -fPIC' --with-ld-opt='-Wl,-z,relro -Wl,-z,now -pie'
 

– Tải và cài đặt các thành phần cần thiết như mã nguồn Nginx 1.12.0, mã nguồn OpenSSL 1.1.0f, các trình biên dịch.

# yum install gcc-c++ pcre-devel zlib-devel epel-release -y
 # cd /usr/local/src
 # wget http://nginx.org/download/nginx-1.12.0.tar.gz && tar -xvf nginx-1.12.0.tar.gz
 # wget https://www.openssl.org/source/openssl-1.1.0f.tar.gz && tar -xvf openssl-1.1.0f.tar.gz

1.2. Biên dịch Nginx

– Truy cập thư mục chứa Nginx để cấu hình mã nguồn Nginx với lệnh ./configure cùng toàn bộ tham số mình nói ở bước 1.1 và thêm vào --with-openssl=/usr/local/src/openssl-1.1.0f

# cd nginx-1.12.0
 # ./configure --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-compat --with-file-aio --with-threads --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_realip_module --with-stream_ssl_module --with-stream_ssl_preread_module --with-cc-opt='-O2 -g -pipe -Wall -Wp,-D_FORTIFY_SOURCE=2 -fexceptions -fstack-protector --param=ssp-buffer-size=4 -m64 -mtune=generic -fPIC' --with-ld-opt='-Wl,-z,relro -Wl,-z,now -pie' --with-openssl=/usr/local/src/openssl-1.1.0f
 

Kết quả hiển thị như dưới là thành công:

Configuration summary
   + using threads
   + using system PCRE library
   + using OpenSSL library: /usr/local/src/openssl-1.1.0f
   + using system zlib library
 
   nginx path prefix: "/etc/nginx"
   nginx binary file: "/usr/sbin/nginx"
   nginx modules path: "/usr/lib64/nginx/modules"
   nginx configuration prefix: "/etc/nginx"
   nginx configuration file: "/etc/nginx/nginx.conf"
   nginx pid file: "/var/run/nginx.pid"
   nginx error log file: "/var/log/nginx/error.log"
   nginx http access log file: "/var/log/nginx/access.log"
   nginx http client request body temporary files: "/var/cache/nginx/client_temp"
   nginx http proxy temporary files: "/var/cache/nginx/proxy_temp"
   nginx http fastcgi temporary files: "/var/cache/nginx/fastcgi_temp"
   nginx http uwsgi temporary files: "/var/cache/nginx/uwsgi_temp"
   nginx http scgi temporary files: "/var/cache/nginx/scgi_temp"

– Chạy lệnh phía dưới để tiến hành compile Nginx.

# make

Đợi tầm 3-5 phút để tiến trình thành công là bạn có thể sử dụng Nginx đã được biên dịch thủ công để làm web server.

1.3. Nâng cấp Nginx

– Tiến hành thay thế Nginx trên VPS bằng Nginx vừa được biên dịch lại:

# mv /usr/sbin/nginx /usr/sbin/nginx.bak
 # cp objs/nginx /usr/sbin/nginx
 # service nginx restart

– Kiểm tra lại nếu thấy thông tin như bên dưới là thành công.

# nginx -V
 nginx version: nginx/1.12.0
 built by gcc 4.4.7 20120313 (Red Hat 4.4.7-18) (GCC)
 built with OpenSSL 1.1.0f  25 May 2017
 TLS SNI support enabled
 configure arguments: --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-compat --with-file-aio --with-threads --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_realip_module --with-stream_ssl_module --with-stream_ssl_preread_module --with-cc-opt='-O2 -g -pipe -Wall -Wp,-D_FORTIFY_SOURCE=2 -fexceptions -fstack-protector --param=ssp-buffer-size=4 -m64 -mtune=generic -fPIC' --with-ld-opt='-Wl,-z,relro -Wl,-z,now -pie' --with-openssl=/usr/local/src/openssl-1.1.0f

2. Cấu hình SSL HTTP/2 trong Nginx Conf

Webserver Nginx trên hệ thống của bạn đã sẵn sàng thiết lập giao thức HTTP/2 giữa Server và Browser. Bạn cần kích hoạt giao thức HTTP2 và SSL trong cấu hình Nginx của mỗi site.

Bạn sửa cấu hình Nginx trong block server như sau (chỉnh sửa đối với từng website trong hệ thống).

listen   443 ssl http2;

Cụ thể, bạn cấu hình như hướng dẫn cài đặt chứng chỉ SSL hoặc Let’s Encrypt

Kiểm tra lại trực tiếp các kết nối trình duyệt tới website với công cụ Developer Tools (trình duyệt nào cũng có). Như bạn đã thấy, HocVPS đã dùng HTTP/2, thời gian load lần đầu homepage chỉ là 2.33s.

Nếu không thấy tab Protocol, bạn hãy nhấn chuột phải ở menu để kích hoạt.

Hi vọng với bài viết hướng dẫn này, các bạn có thể kích hoạt kết nối giao thức HTTP/2 cho website của mình. Nếu có điều gì thắc mắc, hãy để lại comment bên dưới nhé.

Nguồn: https://hocvps.com/http2-nginx/

Add Comment