Môi trường đã thực hiện:
- Ubuntu: 18.4
- Webinoly
- WordPress, WordPress multisite
Kiểm tra phiên bản nginx.
sudo nginx -v # nginx version: nginx/1.18.0
Tải xuống và biên dịch mã nguồn Brotli
Sau khi cài đặt Nginx, chúng ta cần xây dựng mô-đun Brotli ( ngx_brotli
) dưới dạng mô-đun Nginx động. Từ phiên bản Nginx 1.18.0, có thể biên dịch các mô-đun động riêng lẻ mà không cần biên dịch phần mềm Nginx hoàn chỉnh. Trong vài bước tiếp theo, chúng tôi sẽ xây dựng mô-đun Brotli dưới dạng động mà không cần biên dịch Nginx hoàn chỉnh.
Tải về các thư viện cần thiết.
sudo apt install -y libpcre3 libpcre3-dev zlib1g zlib1g-dev openssl libssl-dev
Tải xuống phiên bản mới nhất của mã nguồn Nginx chính và giải nén nó.
wget https://nginx.org/download/nginx-1.18.0.tar.gz && tar zxvf nginx-1.18.0.tar.gz
LƯU Ý : Điều rất quan trọng là số phiên bản của gói Nginx và mã nguồn Nginx khớp với nhau. Nếu bạn đã cài đặt Nginx 1.18.0 từ kho lưu trữ Nginx chính thức, thì bạn phải tải xuống cùng một phiên bản của mã nguồn , 1.18.0 trong trường hợp này.
Loại bỏ nginx-1.18.0.tar.gz
.
rm nginx-1.18.0.tar.gz
Bản sao ngx_brotli
từ GitHub.
git clone https://github.com/eustas/ngx_brotli.git cd ngx_brotli && git submodule update --init && cd ~
Điều hướng đến thư mục mã nguồn Nginx.
cd ~/nginx-1.18.0
Biên dịch ngx_brotli
dưới dạng một mô-đun động và sao chép nó vào thư mục tiêu chuẩn cho các mô-đun Nginx , /etc/nginx/modules
.
./configure --with-compat --add-dynamic-module=../ngx_brotli make modules sudo cp objs/*.so /etc/nginx/modules-available # hoặc sudo cp objs/*.so /usr/share/nginx/modules
Liệt kê các tập tin trong /etc/nginx/modules
(ls /etc/nginx/modules-available hoặc ls /usr/share/nginx/modules )và bạn sẽ thấy:
ngx_http_brotli_filter_module.so ngx_http_brotli_static_module.so
Nếu gặp lỗi: ./configure: error: C compiler cc is not found
. Điều này thường xảy ra khi bạn không cài đặt trình biên dịch C/GCC trên máy chủ của mình. Tất cả bạn phải làm là cài đặt gcc
để giải quyết điều này.
Cấu hình Nginx
Chúng ta đã sẵn sàng cấu hình hỗ trợ Brotli trong Nginx.
Mở tệp nginx.conf mặc định của bạn và tải lên hai mô-đun bạn vừa biên dịch.
sudo nano /etc/nginx/nginx.conf
Sau đó thêm hai chỉ thị sau vào đầu tệp để tải các mô-đun Brotli mới.
load_module modules/ngx_http_brotli_filter_module.so; load_module modules/ngx_http_brotli_static_module.so;
Tệp nginx.confcủa bạn sẽ trông giống như thế này:
load_module modules/ngx_http_brotli_filter_module.so; load_module modules/ngx_http_brotli_static_module.so; user www-data; worker_processes auto; pid /run/nginx.pid; include /etc/nginx/modules-enabled/*.conf; events { worker_connections 768; # multi_accept on; } http { .............................................
Sau đó, chạy thử nghiệm Nginx để xem bạn có gặp lỗi nào không.
sudo nginx -t
Bạn sẽ nhận được các dòng tương tự như dưới đây nếu mọi thứ đều thành công.
nginx: the configuration file /etc/nginx/nginx.conf syntax is ok nginx: configuration file /etc/nginx/nginx.conf test is successful
Nếu bạn gặp lỗi bên dưới, hãy sao chép lại hai tệp .so ở trên vào đúng thư mục.
nginx: [emerg] dlopen() "/usr/share/nginx/modules/ngx_http_brotli_filter_module.so" failed (/usr/share/nginx/modules/ngx_http_brotli_filter_module.so: cannot open shared object file: No such file or directory) in /etc/nginx/nginx.conf:1 nginx: configuration file /etc/nginx/nginx.conf test failed
Khi bạn muốn sử dụng Brotli với các tệp cấu hình máy chủ ảo của mình, hãy sử dụng ví dụ bên dưới. Ví dụ với webinoly, sửa file: /etc/nginx/sites-available/example.com
server { listen 80; server_name example.com; return 301 https://$server_name$request_uri; } server { listen 443 ssl http2; server_name example.com; ssl_certificate /etc/letsencrypt/example.com/fullchain.cer; ssl_certificate_key /etc/letsencrypt/example.com/example.com.key; brotli on; brotli_static on; brotli_types *; }
Truy cập: /var/www/domain.com/
Thêm tệp
brotli-nginx.conf
với nội dung bên dưới:
brotli on; brotli_static on; brotli_types *;
Hoặc
brotli on; brotli_static on; brotli_types text/plain text/css text/javascript application/javascript text/xml application/xml image/svg+xml application/json;
Hoặc là
brotli on; brotli_comp_level 6; brotli_types text/xml image/svg+xml application/x-font-ttf image/vnd.microsoft.icon application/x-font-opentype application/json font/eot application/vnd.ms-fontobject application/javascript font/otf application/xml application/xhtml+xml text/javascript application/x-javascript text/plain application/x-font-truetype application/xml+rss image/x-icon font/opentype text/css image/x-win-bitmap;
Restart Nginx
sudo nginx -t sudo systemctl reload nginx.service
Kiểm tra:
Vào website https://nixcp.com/tools/brotli-test/ để kiểm tra nhanh, hoặc kiểm tra trong chế độ nhà phát triển của trình duyệt (chorme, firefox)
content-encoding: br