Cài đặt Ngx_pagespeed cho Nginx trên Direct Admin

Bình luận · 1350 Lượt xem

Nginx quả là lợi hại với khả năng xử lý file tĩnh, không những thế, với Direct Admin, giờ đây Nginx còn có thể xử lý các file PHP một cách hoàn hảo với tộc độ tuyệt vời trên các VPS Linux. Tuy thế, dường như vẫn chưa hài lòng

Bản chất của ngx_pagespeed là tăng tốc website và giảm thiểu thời gian tải trang bằng cách tự động chọn và apply các mẫu xử lý thích hợp nhất vào từng trang mà không cần thay đổi mã nguồn.

Để cài đặt ngx_pagespeed trên Direct Admin ta làm như sau:

Bước 1: Download các gói cần thiết để build từ source

# wget https://github.com/pagespeed/ngx_pagespeed/archive/release-${VERSION}-beta.zip# unzip release-${VERSION}-beta.zip# cd ngx_pagespeed-release-${VERSION}-beta/# wget https://dl.google.com/dl/page-speed/psol/${VERSION}.tar.gz# tar -xzvf ${VERSION}.tar.gz

Bước 2: Tạo file config cho Nginx

# cd /usr/local/directadmin/custombuild# mkdir -p custom/nginx# cp -fp configure/nginx/configure.nginx custom/nginx/configure.nginx

Bước 3: Thêm module ngx_pagespeed vào file cấu hình như sau

# vi /usr/local/directadmin/custombuild/custom/nginx/configure.nginx
#!/bin/sh./configure \"--add-module=/root/ngx_pagespeed-release-1.11.3.0-beta" \"--user=nginx" \"--group=nginx" \"--prefix=/usr" \"--sbin-path=/usr/sbin" \"--conf-path=/etc/nginx/nginx.conf" \"--pid-path=/var/run/nginx.pid" \"--http-log-path=/var/log/nginx/access_log" \"--error-log-path=/var/log/nginx/error_log" \"--with-ipv6" \"--without-mail_imap_module" \"--without-mail_smtp_module" \"--with-http_ssl_module" \"--with-http_realip_module" \"--with-http_stub_status_module" \"--with-http_gzip_static_module" \

Bước 4: Build Ngix theo các config ở trên

# ./build used_configs# ./build nginx

Sau khi build xong ta kiểm tra lại như sau

# nginx -V

nginx version: nginx/1.10.2 built by gcc 4.8.8 20120313 TLS SNI support enabled
configure arguments: –add-module=/root/ngx_pagespeed-release-1.11.33.0-beta –user=nginx –group=nginx –prefix=/usr –sbin-path=/usr/sbin –conf-path=/etc/nginx/nginx.conf –pid-path=/var/run/nginx.pid –http-log-path=/var/log/nginx/access_log –error-log-path=/var/log/nginx/error_log –with-ipv6 –without-mail_imap_module –without-mail_smtp_module –with-http_ssl_module –with-http_realip_module –with-http_stub_status_module –with-http_gzip_static_module –with-http_dav_module –with-cc-opt=”-D FD_SETSIZE=32768”

Bước 5: Thêm cấu hình pagespeed vào từng domain mong muốn trong Direct Admin

Chọn: Directadmin>Admin Level>Custom HTTPD Configurations>

Thêm các option sau:

pagespeed on;pagespeed FileCachePath /var/cache/ngx_pagespeed;# Ensure requests for pagespeed optimized resources go to the pagespeed handler# and no extraneous headers get set.location ~ "\.pagespeed\.([a-z]\.)?[a-z]{2}\.[^.]{10}\.[^.]+" { add_header "" "";}location ~ "^/ngx_pagespeed_static/" { }location ~ "^/ngx_pagespeed_beacon$" { }location /ngx_pagespeed_statistics { allow 127.0.0.1; deny all; }location /ngx_pagespeed_global_statistics { allow 127.0.0.1; deny all; }location /ngx_pagespeed_message { allow 127.0.0.1; deny all; }pagespeed MemcachedThreads 1;pagespeed MemcachedServers "localhost:11211";pagespeed MemcachedTimeoutUs 100000;# Defer and minify Javascript#pagespeed EnableFilters defer_javascript;pagespeed EnableFilters rewrite_javascript;pagespeed EnableFilters combine_javascript;pagespeed EnableFilters canonicalize_javascript_libraries;# Inline and minimize csspagespeed EnableFilters rewrite_css;pagespeed EnableFilters fallback_rewrite_css_urls;# Loads CSS faster#pagespeed EnableFilters move_css_above_scripts;pagespeed EnableFilters move_css_to_head;# Rewrite, resize and recompress images#pagespeed EnableFilters rewrite_images;# pagespeed EnableFilters rewrite_images;pagespeed DisableFilters rewrite_images;pagespeed DisableFilters recompress_images;pagespeed DisableFilters convert_png_to_jpeg;pagespeed DisableFilters extend_cache_images;# pagespeed EnableFilters convert_png_to_jpeg;# pagespeed EnableFilters convert_jpeg_to_webp;# pagespeed EnableFilters convert_to_webp_lossless;# remove tags with default attributespagespeed EnableFilters elide_attributes;pagespeed FetchHttps enable,allow_self_signed;

Vậy là xong, giờ chỉ cần ngồi tận hưởng thành quả. Hãy nhớ, có thêm ngx_pagespeed, VPS của bạn như hổ mọc thêm cánh, sức chịu tải của hệ thống tăng lên đáng kể ngay cả khi là những vps giá rẻ. Nếu vẫn vướng mắc trong quá trình thực hiện

Bình luận