Tác giả: Falko Timme,F4RR3LL | Sven
Bài hướng dẫn này giải thích cách xây dựng nginx với phiên bản mới nhất của mô-đun nginx_pagespeed trên Debian Jessie. Mô-đun PageSpeed áp dụng các biện pháp thực hành tốt nhất về hiệu suất web cho các trang và các tài sản liên quan (CSS, JavaScript, hình ảnh) và do đó tăng tốc trang web của bạn và giảm thời gian tải.
Trước tiên, hãy tạo thư mục dựng:
Đảm bảo bạn có các dòng deb và deb-src cho Jessie trong /etc/apt/sources.list:
Cập nhật danh sách các gói và cài đặt một số điều kiện tiên quyết:
Tải xuống các nguồn nginx và các phụ thuộc bản dựng:
Hãy kiểm tra phiên bản nginx của chúng ta:
Phiên bản nginx của chúng tôi là 1.6.2. Hãy đến thư mục debian/modules của các nguồn nginx đã tải xuống...
... và tải xuống các nguồn PageSpeed (bạn có thể kiểm tra bản phát hành mới nhất trên trang này: https://github.com/pagespeed/ngx_pagespeed/releases - trong ví dụ này, phiên bản là 1.9.32.3-beta; nếu phiên bản của bạn khác, hãy đảm bảo thay thế phiên bản đó trong phần còn lại của hướng dẫn này):
Hãy chỉnh sửa tệp debian/rules:
Trong tệp đó, bạn sẽ tìm thấy ba phần configure_flags trong đó dòng:
cần phải thêm:
trong phầnlight_configure_flags sau –without-http_uwsgi_module ,
trong phầnfull_configure_flags sau –with-mail_ssl_module,
trong phầnextras_configure_flags sau –add-module=$(MODULESDIR)/nginx-development-kit
Tệp kết quả sẽ trông giống như tệp bên dưới (sử dụng phím tab để thêm khoảng trắng trước các dòng).
Sau đó chạy:
để xây dựng gói nginx .deb mới. Sau đó, chúng ta hãy xem các gói được tạo:
Bây giờ chúng ta có thể cài đặt nginx với hỗ trợ PageSpeed như sau:
Sau đó khởi động lại nginx:
Hãy kiểm tra xem mô-đun PageSpeed đã được xây dựng thành công chưa:
Bạn sẽ thấy ngx_pagespeed module trong đầu ra:
... và thêm các dòng pagespeed on; và pagespeed FileCachePath /var/ngx_pagespeed_cache; trước bất kỳ vhost nào:
Tạo thư mục bộ nhớ đệm và tải lại nginx:
Hãy tải một trang và kiểm tra xem PageSpeed có được đề cập trong đầu ra không:
Bây giờ chúng ta có thể cấu hình PageSpeed riêng lẻ hoặc từng vhost, ví dụ như thế này:
Dòng quan trọng là dòng pagespeed EnableFilters cho PageSpeed biết bộ lọc nào cần áp dụng. Bạn có thể tìm thấy danh sách tất cả các bộ lọc tại đây: http://ngxpagespeed.com/ngx_pagespeed_example/
Đừng quên khởi động lại nginx sau đó:
Bây giờ khi bạn mở một trang và xem các tiêu đề (ví dụ: với tiện ích bổ sung tiêu đề Live HTTP cho FireFox), bạn sẽ thấy dòng x-page-speed. Bạn cũng có thể kiểm tra nguồn HTML của các trang để xem bộ lọc PageSpeed có hoạt động như mong đợi không.
Bài hướng dẫn này giải thích cách xây dựng nginx với phiên bản mới nhất của mô-đun nginx_pagespeed trên Debian Jessie. Mô-đun PageSpeed áp dụng các biện pháp thực hành tốt nhất về hiệu suất web cho các trang và các tài sản liên quan (CSS, JavaScript, hình ảnh) và do đó tăng tốc trang web của bạn và giảm thời gian tải.
1 Xây dựng nginx với PageSpeed
nginx không hỗ trợ tải động các mô-đun, do đó chúng ta phải xây dựng lại nó với sự hỗ trợ của PageSpeed. Chúng ta sẽ xây dựng nginx dưới dạng gói .deb để có thể thay thế cài đặt nginx hiện tại bằng gói này (hoặc dễ dàng cài đặt trên các hệ thống khác).Trước tiên, hãy tạo thư mục dựng:
Mã:
cd /usr/src
mkdir nginx-pagespeed && cd nginx-pagespeed
Mã:
nano /etc/apt/sources.list
Mã:
[...]deb http://ftp.de.debian.org/debian jessie main contrib non-freedeb-src http://ftp.de.debian.org/debian jessie main contrib non-free[...]
Mã:
apt-get update
Mã:
apt-get install dpkg-dev build-essential zlib1g-dev libpcre3 libpcre3-dev unzip curl libcurl4-openssl-dev libossp-uuid-dev
Mã:
apt-get source nginx
Mã:
apt-get build-dep nginx
Mã:
ls -l
Mã:
root@nginx:/usr/src/nginx-pagespeed# ls -l
insgesamt 1388
drwxr-xr-x 10 root root 4096 29/04 22:16 nginx-1.6.2
-rw-r–r– 1 root root 604568 12:51 nginx_1.6.2-5.debian.tar.xz
-rw-r–r– 1 root root 2827 12:51 nginx_1.6.2-5.dsc
-rw-r–r– 1 root root 804164 17/09/2014 nginx_1.6.2.orig.tar.gz
Mã:
cd /usr/src/nginx-pagespeed/nginx-1.6.2/debian/modules/
Mã:
wget https://github.com/pagespeed/ngx_pagespeed/archive/release-1.9.32.3-beta.zip
unzip release-1.9.32.3-beta.zip
mv ngx_pagespeed-release-1.9.32.3-beta/ ngx_pagespeed
cd ngx_pagespeed/
wget https://dl.google.com/dl/page-speed/psol/1.9.32.3.tar.gz
tar -xzvf 1.9.32.3.tar.gz
Mã:
nano /usr/src/nginx-pagespeed/nginx-1.6.2/debian/rules
Mã:
–add-module=$(MODULESDIR)/ngx_pagespeed \
trong phầnlight_configure_flags sau –without-http_uwsgi_module ,
trong phầnfull_configure_flags sau –with-mail_ssl_module,
trong phầnextras_configure_flags sau –add-module=$(MODULESDIR)/nginx-development-kit
Tệp kết quả sẽ trông giống như tệp bên dưới (sử dụng phím tab để thêm khoảng trắng trước các dòng).
Mã:
#!/usr/bin/make -fexport DH_VERBOSE=1debian_cflags:=$(shell dpkg-buildflags --get CFLAGS) $(shell dpkg-buildflags --get CPPFLAGS)debian_ldflags:=$(shell dpkg-buildflags --get LDFLAGS)# export các cờ bảo vệ perl cần thiết# xem: src/http/modules/perl/Makefile.PLDEBIAN_NGINX_PERL_LDFLAGS:= $(debian_ldflags)export DEBIAN_NGINX_PERL_LDFLAGSHƯƠNG VỊ := các phần bổ sung đầy đủMODULESDIR = $(CURDIR)/debian/modulesBASEDIR = $(CURDIR)$(foreach flavor,$(FLAVOURS),$(eval BUILDDIR_$(flavour) = $(CURDIR)/debian/build-$(flavour)))DEB_BUILD_ARCH ?=$(shell dpkg-architecture -qDEB_BUILD_ARCH)ifeq ($(DEB_BUILD_ARCH),sparc) debian_cflags += -m32 -mcpu=ultrasparcendififneq (,$(filter parallel=%,$(DEB_BUILD_OPTIONS))) NUMJOBS = $(patsubst parallel=%,%,$(filter parallel=%,$(DEB_BUILD_OPTIONS))) MAKEFLAGS += -j$(NUMJOBS)endif# cấu hình cờcommon_configure_flags := \ --with-cc-opt="$(debian_cflags)" \ --with-ld-opt="$(debian_ldflags)" \ --prefix=/usr/share/nginx \ --conf-path=/etc/nginx/nginx.conf \ --http-log-path=/var/log/nginx/access.log \ --error-log-path=/var/log/nginx/error.log \ --lock-path=/var/lock/nginx.lock \ --pid-path=/run/nginx.pid \ --http-client-body-temp-path=/var/lib/nginx/body \ --http-fastcgi-temp-path=/var/lib/nginx/fastcgi \ --http-proxy-temp-path=/var/lib/nginx/proxy \ --http-scgi-temp-path=/var/lib/nginx/scgi \ --http-uwsgi-temp-path=/var/lib/nginx/uwsgi \ --with-debug \ --with-pcre-jit \ --with-ipv6 \ --with-http_ssl_module \ --with-http_stub_status_module \ --with-http_realip_module \ --with-http_auth_request_modulelight_configure_flags := \ $(common_configure_flags) \ --with-http_gzip_static_module \ --without-http_browser_module \ --without-http_geo_module \ --without-http_limit_req_module \ --without-http_limit_zone_module \ --without-http_memcached_module \ --without-http_referer_module \ --without-http_scgi_module \ --without-http_split_clients_module \ --without-http_ssi_module \ --without-http_userid_module \ --without-http_uwsgi_module \ --add-module=$(MODULESDIR)/ngx_pagespeed \ --add-module=$(MODULESDIR)/nginx-echofull_configure_flags := \ $(common_configure_flags) \ --with-http_addition_module \ --with-http_dav_module \ --with-http_geoip_module \ --with-http_gzip_static_module \ --with-http_image_filter_module \ --with-http_spdy_module \ --with-http_sub_module \ --with-http_xslt_module \ --with-mail \ --with-mail_ssl_module \ --add-module=$(MODULESDIR)/ngx_pagespeed \ --add-module=$(MODULESDIR)/nginx-auth-pam \ --add-module=$(MODULESDIR)/nginx-dav-ext-module \ --add-module=$(MODULESDIR)/nginx-echo \ --add-module=$(MODULESDIR)/nginx-upstream-fair \ --add-module=$(MODULESDIR)/ngx_http_substitutions_filter_moduleextras_configure_flags := \ $(common_configure_flags) \ --with-http_addition_module \ --with-http_dav_module \ --with-http_flv_module \ --with-http_geoip_module \ --with-http_gzip_static_module \ --with-http_image_filter_module \ --with-http_mp4_module \ --with-http_perl_module \ --with-http_random_index_module \ --with-http_secure_link_module \ --with-http_spdy_module \ --with-http_sub_module \ --with-http_xslt_module \ --with-mail \ --with-mail_ssl_module \ --add-module=$(MODULESDIR)/headers-more-nginx-module \ --add-module=$(MODULESDIR)/nginx-auth-pam \ --add-module=$(MODULESDIR)/nginx-cache-purge \ --add-module=$(MODULESDIR)/nginx-dav-ext-module \ --add-module=$(MODULESDIR)/nginx-development-kit \ --add-module=$(MODULESDIR)/ngx_pagespeed \ --add-module=$(MODULESDIR)/ngx-fancyindex \ --add-module=$(MODULESDIR)/nginx-http-push \ --add-module=$(MODULESDIR)/nginx-lua \ --add-module=$(MODULESDIR)/nginx-upload-progress \ --add-module=$(MODULESDIR)/nginx-upstream-fair \ --add-module=$(MODULESDIR)/ngx_http_substitutions_filter_module%: dh $@ --with systemdoverride_dh_auto_configure: $(foreach flavor,$(FLAVOURS),config.arch.$(flavour))override_dh_auto_build: $(foreach flavor,$(FLAVOURS),build.arch.$(flavour))override_dh_strip: $(foreach flavor,$(FLAVOURS),strip.arch.$(flavour))override_dh_clean: $(foreach flavor,$(FLAVOURS),clean.$(flavour)) dh_cleanoverride_dh_installinit: dh_installinit --no-restart-on-upgrade --no-start --name=nginxoverride_dh_systemd_enable: dh_systemd_enable --name=nginxoverride_dh_installlogrotate: dh_installlogrotate --package nginx-common --name=nginxbuild.arch.%: $(MAKE) -C $(BUILDDIR_$*) buildstrip.arch.%: dh_strip --package=nginx-$(*) --dbg-package=nginx-$(*)-dbgconfig.arch.%: dh_testdir mkdir -p $(BUILDDIR_$*) cp -Pa $(CURDIR)/auto $(BUILDDIR_$*)/ cp -Pa $(CURDIR)/conf $(BUILDDIR_$*)/ cp -Pa $(CURDIR)/configure $(BUILDDIR_$*)/ cp -Pa $(CURDIR)/contrib $(BUILDDIR_$*)/ cp -Pa $(CURDIR)/src $(BUILDDIR_$*)/ cp -Pa $(CURDIR)/man $(BUILDDIR_$*)/ cd $(BUILDDIR_$*) && ./configure $($*_configure_flags)clean.%: rm -rf $(BUILDDIR_$*)
Mã:
cd /usr/src/nginx-pagespeed/nginx-1.6.2/ && dpkg-buildpackage -b
Mã:
cd /usr/src/nginx-pagespeed
ls -l
Mã:
root@nginx:/usr/src/nginx-pagespeed# ls -l
insgesamt 99720
drwxr-xr-x 10 root root 4096 29 tháng 4 22:16 nginx-1.6.2
-rw-r–r– 1 root root 72086 29 tháng 4 23:06 nginx_1.6.2-5_all.deb
-rw-r–r– 1 root root 3961 29 tháng 4 23:08 nginx_1.6.2-5_amd64.changes
-rw-r–r– 1 root root 604568 1 tháng 12 12:51 nginx_1.6.2-5.debian.tar.xz
-rw-r–r– 1 root root 2827 1 tháng 12 12:51 nginx_1.6.2-5.dsc
-rw-r–r– 1 root root 804164 17 tháng 9 năm 2014 nginx_1.6.2.orig.tar.gz
-rw-r–r– 1 root root 86540 29 tháng 4 23:06 nginx-common_1.6.2-5_all.deb
-rw-r–r– 1 root root 83716 29 tháng 4 23:06 nginx-doc_1.6.2-5_all.deb
-rw-r–r– 1 root root 3403690 29/04 23:08 nginx-extras_1.6.2-5_amd64.deb
-rw-r–r– 1 root root 31745456 29/04 23:08 nginx-extras-dbg_1.6.2-5_amd64.deb
-rw-r–r– 1 root root 3232788 29/04 23:07 nginx-full_1.6.2-5_amd64.deb
-rw-r–r– 1 root root 29932616 29/04 23:07 nginx-full-dbg_1.6.2-5_amd64.deb
-rw-r–r– 1 root root 3136400 29/04 23:07 nginx-light_1.6.2-5_amd64.deb
-rw-r–r– 1 root root 28975322 29 tháng 4 23:08 nginx-light-dbg_1.6.2-5_amd64.deb
Mã:
dpkg --install nginx-common_1.6.2-5_all.deb nginx_1.6.2-5_all.deb nginx-full_1.6.2-5_amd64.deb
Mã:
service nginx restart
Mã:
nginx -V
Mã:
root@nginx:/usr/src/nginx-pagespeed# nginx -V
phiên bản nginx: nginx/1.6.2
Hỗ trợ TLS SNI được bật
cấu hình đối số: –with-cc-opt='-g -O2 -fstack-protector-strong -Wformat -Werror=format-security -D_FORTIFY_SOURCE=2' –with-ld-opt=-Wl,-z,relro –prefix=/usr/share/nginx –conf-path=/etc/nginx/nginx.conf –http-log-path=/var/log/nginx/access.log –error-log-path=/var/log/nginx/error.log –lock-path=/var/lock/nginx.lock –pid-path=/run/nginx.pid –http-client-body-temp-path=/var/lib/nginx/body –http-fastcgi-temp-path=/var/lib/nginx/fastcgi –http-proxy-temp-path=/var/lib/nginx/proxy –http-scgi-temp-path=/var/lib/nginx/scgi –http-uwsgi-temp-path=/var/lib/nginx/uwsgi –with-debug –with-pcre-jit –with-ipv6 –with-http_ssl_module –with-http_stub_status_module –with-http_realip_module –with-http_auth_request_module –with-http_addition_module –with-http_dav_module –with-http_geoip_module –with-http_gzip_static_module –with-http_image_filter_module –with-http_spdy_module –with-http_sub_module –with-http_xslt_module –with-mail –with-mail_ssl_module –add-module=/usr/src/nginx-pagespeed/nginx-1.6.2/debian/modules/ngx_pagespeed –add-module=/usr/src/nginx-pagespeed/nginx-1.6.2/debian/modules/nginx-auth-pam –add-module=/usr/src/nginx-pagespeed/nginx-1.6.2/debian/modules/nginx-dav-ext-module –add-module=/usr/src/nginx-pagespeed/nginx-1.6.2/debian/modules/nginx-echo –add-module=/usr/src/nginx-pagespeed/nginx-1.6.2/debian/modules/nginx-upstream-fair –add-module=/usr/src/nginx-pagespeed/nginx-1.6.2/debian/modules/ngx_http_substitutions_filter_module
2 Cấu hình PageSpeed
Để bật PageSpeed, hãy mở /etc/nginx/nginx.conf...
Mã:
nano /etc/nginx/nginx.conf
Mã:
[...] pagespeed on; pagespeed FileCachePath /var/ngx_pagespeed_cache; ## # Cấu hình máy chủ ảo ## include /etc/nginx/conf.d/*.conf; bao gồm /etc/nginx/sites-enabled/*;[...]
Mã:
mkdir /var/ngx_pagespeed_cache
chown -R www-data:www-data /var/ngx_pagespeed_cache
Mã:
servicenginx reload
Mã:
curl -I -p http://localhost|grep X-Page-Speed
Mã:
root@nginx:/usr/src/nginx-pagespeed# curl -I -p http://localhost|grep X-Page-Speed
% Tổng số % Đã nhận % Xferd Tốc độ trung bình Thời gian Thời gian Thời gian Hiện tại
Dload Tải lên Tổng số Đã sử dụng Tốc độ còn lại
0 0 0 0 0 0 0 --:--:-- --:--:-- --:--:-- 0
X-Page-Speed: 1.9.32.3-4448
Mã:
nano /etc/nginx/sites-available/example.com.vhost
Mã:
server {[...] # hãy tăng tốc PageSpeed bằng cách lưu trữ nó trong memcached pagespeed siêu nhanh MemcachedThreads 1; pagespeed MemcachedServers "localhost:11211"; # Thiết lập bộ lọc pagespeed RewriteLevel CoreFilters; pagespeed EnableFilters collapse_whitespace,remove_comments; # Đảm bảo các yêu cầu về tài nguyên được tối ưu hóa cho pagespeed sẽ chuyển đến trình xử lý pagespeed # và không có tiêu đề không cần thiết nào được đặt. 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; } location /pagespeed_console { allow 127.0.0.1; deny all; }[...]}
Đừng quên khởi động lại nginx sau đó:
Mã:
servicenginx reload
3 Liên kết
- Blog dành cho nhà phát triển của Google: http://googledevelopers.blogspot.nl/2013/04/speed-up-your-sites-with-pagespeed-for.html
- ngx_pagespeed: https://developers.google.com/speed/pagespeed/ngx
- Xây dựng/sử dụng PageSpeed: https://github.com/pagespeed/ngx_pagespeed
- Ví dụ về bộ lọc ngx_pagespeed: http://ngxpagespeed.com/ngx_pagespeed_example/