如何在 Debian 8 上使用 Google PageSpeed 模块构建 nginx (Jessie)如何在 Debian 8 上使用 Google PageSpeed 模块构建 nginx (Jessie)如何在 Debian 8 上使用 Google PageSpeed 模块构建 nginx (Jessie)如何在 Debian 8 上使用 Google PageSpeed 模块构建 nginx (Jessie)
  • 文章
  • 正则表达式
    • 工具
  • 登录
找到的结果: {phrase} (显示: {results_count} 共: {results_count_total})
显示: {results_count} 共: {results_count_total}

加载更多搜索结果...

搜索范围
模糊匹配
搜索标题
搜索内容
发表 admin at 2025年2月28日
类别
  • 未分类
标签

如何在 Debian 8 上使用 Google PageSpeed 模块构建 nginx (Jessie)

本教程适用于这些操作系统版本

  • Debian 7(喘息)

在此页

  1. 1 使用 PageSpeed 构建 nginx
  2. 2 配置 PageSpeed
  3. 3 个链接

作者:F4RR3LL |斯文

本教程介绍了如何在 Debian Jessie 上使用最新版本的 nginx_pagespeed 模块构建 nginx。 PageSpeed 模块将网络性能最佳实践应用于页面和相关资产(CSS、JavaScript、图像),从而加快您的网站速度并减少加载时间。

1 使用 PageSpeed 构建 nginx

nginx 不支持模块的动态加载,因此我们必须用 PageSpeed 支持重建它。我们将 nginx 构建为 .deb 包,以便我们可以用它替换现有的 nginx 安装(或轻松地将其安装在其他系统上)。

先创建构建目录:

cd /usr/src
mkdir nginx-pagespeed && cd nginx-pagespeed

确保在 /etc/apt/sources.list 中有 Jessie 的 deb 和 deb-src 行:

nano /etc/apt/sources.list
[...]
deb http://ftp.de.debian.org/debian jessie main contrib non-free
deb-src http://ftp.de.debian.org/debian jessie main contrib non-free
[...]

更新您的包列表并安装一些先决条件:

apt-get update
apt-get install dpkg-dev build-essential zlib1g-dev libpcre3 libpcre3-dev unzip curl libcurl4-openssl-dev libossp-uuid-dev

下载 nginx 源代码和构建依赖项:

apt-get source nginx
apt-get build-dep nginx

让我们检查一下我们的 nginx 版本:

ls -l
:/usr/src/nginx-pagespeed# ls -l
insgesamt 1388
drwxr-xr-x 10 root root 4096 Apr 29 22:16 nginx-1.6.2
-rw-r–r– 1 root root 604568 Dez 1 12:51 nginx_1.6.2-5.debian.tar.xz
-rw-r–r– 1 root root 2827 Dez 1 12:51 nginx_1.6.2-5.dsc
-rw-r–r– 1 root root 804164 Sep 17 2014 nginx_1.6.2.orig.tar.gz

我们的 nginx 版本是 1.6.2。让我们转到下载的 nginx 源的 debian/modules 目录...

cd /usr/src/nginx-pagespeed/nginx-1.6.2/debian/modules/

...并下载 PageSpeed 源代码(您可以在此页面上查看最新版本:https://github.com/pagespeed/ngx_pagespeed/releases - 在本例中版本为 1.9.32.3-beta;如果您的版本不同,确保在本教程的其余部分替换它):

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

让我们编辑 debian/rules 文件:

nano /usr/src/nginx-pagespeed/nginx-1.6.2/debian/rules

在该文件中,您将找到三个 configure_flags 部分,其中行:

–add-module=$(MODULESDIR)/ngx_pagespeed \ 

需要补充:

在 –without-http_uwsgi_module 之后的 light_configure_flags 部分中,
在 –with-mail_ssl_module 之后的 full_configure_flags 部分中,
在 –add-module=$ (MODULESDIR)/nginx-development-kit 之后的 extras_configure_flags 部分中

生成的文件应如下所示(使用 Tab 键在行前添加空格)。

#!/usr/bin/make -f
export DH_VERBOSE=1

debian_cflags:=$(shell dpkg-buildflags --get CFLAGS) $(shell dpkg-buildflags --get CPPFLAGS)
debian_ldflags:=$(shell dpkg-buildflags --get LDFLAGS)

# export necessary perl hardenging flags
# see: src/http/modules/perl/Makefile.PL
DEBIAN_NGINX_PERL_LDFLAGS:= $(debian_ldflags)
export DEBIAN_NGINX_PERL_LDFLAGS

FLAVOURS := full light extras

MODULESDIR = $(CURDIR)/debian/modules
BASEDIR = $(CURDIR)
$(foreach flavour,$(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=ultrasparc
endif

ifneq (,$(filter parallel=%,$(DEB_BUILD_OPTIONS)))
	NUMJOBS = $(patsubst parallel=%,%,$(filter parallel=%,$(DEB_BUILD_OPTIONS)))
	MAKEFLAGS += -j$(NUMJOBS)
endif

# configure flags
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_module


light_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-echo

full_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_module

extras_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 systemd

override_dh_auto_configure: $(foreach flavour,$(FLAVOURS),config.arch.$(flavour))
override_dh_auto_build:     $(foreach flavour,$(FLAVOURS),build.arch.$(flavour))
override_dh_strip:          $(foreach flavour,$(FLAVOURS),strip.arch.$(flavour))
override_dh_clean:          $(foreach flavour,$(FLAVOURS),clean.$(flavour))
	dh_clean

override_dh_installinit:
	dh_installinit --no-restart-on-upgrade --no-start --name=nginx

override_dh_systemd_enable:
	dh_systemd_enable --name=nginx

override_dh_installlogrotate:
	dh_installlogrotate --package nginx-common --name=nginx

build.arch.%:
	$(MAKE) -C $(BUILDDIR_$*) build

strip.arch.%:
	dh_strip --package=nginx-$(*) --dbg-package=nginx-$(*)-dbg

config.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_$*)

然后运行:

cd /usr/src/nginx-pagespeed/nginx-1.6.2/ && dpkg-buildpackage -b

构建新的 nginx .deb 包。之后,让我们看一下生成的包:

cd /usr/src/nginx-pagespeed
ls -l
:/usr/src/nginx-pagespeed# ls -l
insgesamt 99720
drwxr-xr-x 10 root root 4096 Apr 29 22:16 nginx-1.6.2
-rw-r–r– 1 root root 72086 Apr 29 23:06 nginx_1.6.2-5_all.deb
-rw-r–r– 1 root root 3961 Apr 29 23:08 nginx_1.6.2-5_amd64.changes
-rw-r–r– 1 root root 604568 Dez 1 12:51 nginx_1.6.2-5.debian.tar.xz
-rw-r–r– 1 root root 2827 Dez 1 12:51 nginx_1.6.2-5.dsc
-rw-r–r– 1 root root 804164 Sep 17 2014 nginx_1.6.2.orig.tar.gz
-rw-r–r– 1 root root 86540 Apr 29 23:06 nginx-common_1.6.2-5_all.deb
-rw-r–r– 1 root root 83716 Apr 29 23:06 nginx-doc_1.6.2-5_all.deb
-rw-r–r– 1 root root 3403690 Apr 29 23:08 nginx-extras_1.6.2-5_amd64.deb
-rw-r–r– 1 root root 31745456 Apr 29 23:08 nginx-extras-dbg_1.6.2-5_amd64.deb
-rw-r–r– 1 root root 3232788 Apr 29 23:07 nginx-full_1.6.2-5_amd64.deb
-rw-r–r– 1 root root 29932616 Apr 29 23:07 nginx-full-dbg_1.6.2-5_amd64.deb
-rw-r–r– 1 root root 3136400 Apr 29 23:07 nginx-light_1.6.2-5_amd64.deb
-rw-r–r– 1 root root 28975322 Apr 29 23:08 nginx-light-dbg_1.6.2-5_amd64.deb

我们现在可以安装支持 PageSpeed 的 nginx,如下所示:

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

然后重启 nginx:

service nginx restart

让我们检查一下 PageSpeed 模块是否构建成功:

nginx -V

您应该在输出中看到 ngx_pagespeed 模块:

:/usr/src/nginx-pagespeed# nginx -V
nginx version: nginx/1.6.2
TLS SNI support enabled
configure arguments: –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 配置PageSpeed

要启用 PageSpeed,请打开 /etc/nginx/nginx.conf...

nano /etc/nginx/nginx.conf

...并添加行 pagespeed on;和 pagespeed FileCachePath /var/ngx_pagespeed_cache;在任何虚拟主机之前:

[...]
        pagespeed on;
        pagespeed FileCachePath /var/ngx_pagespeed_cache;

        ##
        # Virtual Host Configs
        ##

        include /etc/nginx/conf.d/*.conf;
        include /etc/nginx/sites-enabled/*;
[...]

创建缓存目录并重新加载 nginx:

mkdir /var/ngx_pagespeed_cache
chown -R www-data:www-data /var/ngx_pagespeed_cache
service nginx reload

让我们加载一个页面并检查输出中是否提到了 PageSpeed:

curl -I -p http://localhost|grep X-Page-Speed
:/usr/src/nginx-pagespeed# curl -I -p http://localhost|grep X-Page-Speed
% Total % Received % Xferd Average Speed Time Time Time Current
Dload Upload Total Spent Left Speed
0 0 0 0 0 0 0 0 --:--:-- --:--:-- --:--:-- 0
X-Page-Speed: 1.9.32.3-4448

现在我们可以单独或每个虚拟主机配置 PageSpeed,例如像这样:

nano /etc/nginx/sites-available/example.com.vhost
server {
[...]
  # let's speed up PageSpeed by storing it in the super duper fast memcached
  pagespeed MemcachedThreads 1;
  pagespeed MemcachedServers "localhost:11211";

  # Filter settings
  pagespeed RewriteLevel CoreFilters;
  pagespeed EnableFilters collapse_whitespace,remove_comments;

  #  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;
  }
  location /pagespeed_console {
    allow 127.0.0.1;
    deny all;
  }
[...]
}

重要的一行是 pagespeed EnableFilters 行,它告诉 PageSpeed 它应该应用哪些过滤器。您可以在此处找到所有过滤器的列表:http://ngxpagespeed.com/ngx_pagespeed_example/

之后不要忘记重启 nginx:

service nginx reload

当您现在打开一个页面并查看标头(例如,使用 FireFox 的 Live HTTP 标头附加组件)时,您应该会看到 x-page-speed 行。您还可以检查页面的 HTML 源代码以查看 PageSpeed 过滤器是否按预期工作。

3个链接

  • Google 开发者博客:http://googledevelopers.blogspot.nl/2013/04/speed-up-your-sites-with-pagespeed-for.html
  • ngx_pagespeed:https://developers.google.com/speed/pagespeed/ngx
  • 构建/使用 PageSpeed:https://github.com/pagespeed/ngx_pagespeed
  • ngx_pagespeed 过滤器示例:http://ngxpagespeed.com/ngx_pagespeed_example/

©2015-2025 艾丽卡 support@alaica.com