• Trang chủ
  • Tăng tốc website bằng cách kích hoạt HTTP/2 với Nginx

Tăng tốc website bằng cách kích hoạt HTTP/2 với Nginx

HTTP/2 được phát triển bởi Hypertext Transfer Protocol, nó được ra đời với mục tiêu chính là là giảm độ trễ, và tăng hiệu quả với một vài kỹ thuật cụ thể…

HTTP/2 có một số đặc điểm như sau

  • nhị phân
  • đa truyền hoàn toàn
  • sử dụng một kết nối cho trạng thái song song
  • sử dụng nén header để giảm chi phí

Và giao thức HTTP/2 có tác dụng là có thể làm tăng tốc độ của Website lên cao chỉ với những thao tác đơn giản mà chúng tôi sẽ hướng dẫn sau đây qua bài viết tăng tốc website bằng cách kích hoạt HTTP/2 với Nginx

Một số lưu ý trước khi cài đặt tăng tốc website bằng cách kích hoạt HTTP/2 với Nginx

– 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.

– Các trình duyệt phổ biến như Chrome, Firefox, Edge, Safari… đều đã hỗ trợ giao thức HTTP/2 (kiểm tra tại CanIUse). Tất cả chỉ hỗ trợ HTTP/2 qua kết nối SSL/TLS.

– Để sử dụng HTTP/2 website cần phải sử dụng SSL. Ngoài SSL, bạn cần đảm bảo hệ thống webserver đã thiết lập HTTP/2 và hỗ trợ tương tác bằng ALPN (Application-Layer Protocol Negotiation)

– Nếu bạn dùng VPS/Cloud Server, hãy kích hoạt SSL, HTTP/2 cho webserver thông qua các giao thức trực tiếp/module cài đặt, VPS bạn cần có OpenSSL v1.0.2 trở lên. Kiểm tra

# openssl version
OpenSSL 1.0.1e-fips 11 Feb 2013

Chúng tôi 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. Do đó chúng ta 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

Các bước tăng tốc website bằng cách kích hoạt HTTP/2 với Nginx

Bước 1: Tích hợp OpenSSL vào Nginx

Tích hợp OpenSSL bằng cách biên dịch lại mã nguồn Nginx. HocVPS lựa chọn phiên bản mới nhất OpenSSL v1.1.0g để tích hợp vào Nginx 1.12.2.

Chuẩn bị
– Kiểm tra thông tin Nginx:  Chúng tôi hiện đang sử dụng Nginx 1.12.1 tích hợp OpenSSL 1.0.1e cùng với các tham số Configure Arguments như phần in đậm bên dưới. Bạn sẽ cần copy lại các tham số này để tiến hành biên dịch lại Nginx ở bước sau.

# nginx -V
nginx version: nginx/1.12.1
built by gcc 4.4.7 20120313 (Red Hat 4.4.7-18) (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: mã nguồn Nginx 1.12.2, mã nguồn OpenSSL 1.1.0g cùng 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.2.tar.gz && tar -xzvf nginx-1.12.2.tar.gz
# wget https://www.openssl.org/source/openssl-1.1.0g.tar.gz && tar -xzvf openssl-1.1.0g.tar.gz

Biên dịch Nginx

– Truy cập thư mục Nginx vừa tải để cấu hình mã nguồn với lệnh ./configure cùng toàn bộ tham số Nginx ban đầu (đề cập bước 1.1) và thêm vào –with-openssl=/usr/local/src/openssl-1.1.0g

Lưu ý: Để biên dịch, bạn cần 512MB RAM trở lên (bao gồm cả Swap)

# cd nginx-1.12.2
# ./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.0g

Kết quả hiển thị như hình dưới đây nghĩa là đã thành công:

Configuration summary
+ using threads
+ using system PCRE library
+ using OpenSSL library: /usr/local/src/openssl-1.1.0g
+ 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

Chỉ ít phút sau là bạn có thể sử dụng bộ Nginx đã được biên dịch thủ công để làm Webserver.

Thay thế 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.2
built by gcc 4.8.5 20150623 (Red Hat 4.8.5-16) (GCC)
built with OpenSSL 1.1.0g 2 Nov 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-strong –param=ssp-buffer-size=4 -grecord-gcc-switches -m64 -mtune=generic -fPIC’ –with-ld-opt=’-Wl,-z,relro -Wl,-z,now -pie’ –with-openssl=/usr/local/src/openssl-1.1.0g

>>> Multisite là gì và cách cài đặt WordPress Multisite trên Server

Bước 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 kèm 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;

Bước 3: Kiểm tra

Để kiểm tra, các bạn sử dụng công cụ Developer Tools của trình duyệt hoặc kiểm tra qua website.

Kiểm tra trực tiếp các kết nối trình duyệt tới website với công cụ Developer Tools F12 (trình duyệt nào cũng có).

Lưu ý: Một số phần mềm Antivirus chặn HTTP/2 Protocol nên cần kiểm tra cả ở máy tính khác và check cả Firefox và Chrome.

  • Với Chrome, nếu không thấy tab Protocol, bạn hãy nhấn chuột phải ở menu để kích hoạt.
  • Với Firefox, các bạn check tab Header trong Network sẽ thấy Version: HTTP/2.0
  • Bên cạnh đó, bạn có thể kiếm tra qua website HTTP2.pro. Kết quả tích hợp HTTP/2 kèm ALPN là ok.

Đến đây chắc hẳn mọi người đã biết cách kích hoạt kết nối giao thức HTTP/2 để tăng tốc cho website của mình, bài viết được trình bày bởi các kỹ thuật viên có trình độ và kinh nghiệm của chúng tôi. Các bài viết tổng hợp về dịch vụ đặt máy chủ sẽ tiếp tục có những bài viết hướng dẫn về các trình cài đặt, các ứng dụng công nghệ hay cho mọi người, hãy tiếp tục theo dõi. Xin chân thành cảm ơn!

0/5 (0 Reviews)

Leave a Reply