linux,  nginx

Nginx部署ngx_pagespeed模块加速网站

方法

Nginx动态添加模块(已经安装好的nginx,添加一个未被编译安装的模块)

模块介绍

PageSpeed是Google推出的一项网页加速服务,分别有Apache PageSpeed和ngx_pagespeed两个模块,适用于Apache和Nginx服务器。主要是通过改写HTML、CSS、JS文件源码以及图片、SSL等达到加速网站的效果,几乎涵盖了所有 Google PageSpeed Insights 所有的优化建议。

这篇文章主要是分享ngx_pagespeed模块在Nginx上的安装与配置方法,如果你用的是Apache服务器可以参考官网的安装文档。ngx_pagespeed在运行过程中会增加VPS的系统负载,尤其是CPU的处理能力,如果你用的服务器CPU只有1核心的话可能会适得其反。

系统

ubuntu x86_64

部署

1.安装编译环境

apt-get install build-essential zlib1g-dev libpcre3 libpcre3-dev unzip uuid-dev openssl libssl-dev

2.安装ngx_pagespeed模块

cd /usr/local/src
wget -O- https://github.com/apache/incubator-pagespeed-ngx/archive/v1.13.35.2-stable.tar.gz | tar -xz

3.下载PageSpeed优化库

cd /usr/local/src/incubator-pagespeed-ngx-1.13.35.2-stable
wget -O- https://dl.google.com/dl/page-speed/psol/1.13.35.2-x64.tar.gz | tar -xz

4.下载当前nginx版本的源码包

查看nginx版本

nginx -v
nginx version: nginx/1.22.0

下载源码包

cd /usr/local/src
wget -O- http://nginx.org/download/nginx-1.22.0.tar.gz | tar -xz

5.模块编译(注意:根据自己服务器的情况而定,不可照抄编译参数)

查看现有nginx编译参数

nginx -V
nginx version: nginx/1.22.0
built by gcc 9.3.0 (Ubuntu 9.3.0-10ubuntu2) 
built with OpenSSL 1.1.1f  31 Mar 2020
TLS SNI support enabled
configure arguments: --prefix=/etc/nginx --sbin-path=/usr/sbin/nginx --modules-path=/usr/lib/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='-g -O2 -fdebug-prefix-map=/data/builder/debuild/nginx-1.22.0/debian/debuild-base/nginx-1.22.0=. -fstack-protector-strong -Wformat -Werror=format-security -Wp,-D_FORTIFY_SOURCE=2 -fPIC' --with-ld-opt='-Wl,-Bsymbolic-functions -Wl,-z,relro -Wl,-z,now -Wl,--as-needed -pie

重新编译nginx,需添加相同的编译参数,并另外添加ngx_pagespeed模块

cd /usr/local/src/nginx-1.22.0
./configure --prefix=/etc/nginx --sbin-path=/usr/sbin/nginx --modules-path=/usr/lib/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='-g -O2 -fdebug-prefix-map=/data/builder/debuild/nginx-1.22.0/debian/debuild-base/nginx-1.22.0=. -fstack-protector-strong -Wformat -Werror=format-security -Wp,-D_FORTIFY_SOURCE=2 -fPIC' --with-ld-opt='-Wl,-Bsymbolic-functions -Wl,-z,relro -Wl,-z,now -Wl,--as-needed -pie' --add-dynamic-module=/usr/local/src/incubator-pagespeed-ngx-1.13.35.2-stable
make    //千万不要make install,不然就真的覆盖了

6.将生成的ngx_pagespeed.so模块加到nginx配置中

cp /usr/local/src/nginx-1.22.0/objs/ngx_pagespeed.so  /etc/nginx/modules/

修改nginx主配置文件

vim /etc/nginx/nginx.conf    #添加下面的内容
load_module "modules/ngx_pagespeed.so";

修改nginx站点配置文件,在server{}中添加下面内容。

pagespeed on;
pagespeed FileCachePath  /var/cache/nginx/easeware_websites/ngx_pagespeed_cache;
# 禁用CoreFilters
pagespeed RewriteLevel PassThrough;

# 启用压缩空白过滤器
pagespeed EnableFilters collapse_whitespace;

# 启用图片优化机制
pagespeed EnableFilters rewrite_images;
# 延时加载客户端看不见的图片
pagespeed EnableFilters lazyload_images;

# 启用JavaScript库卸载
pagespeed EnableFilters canonicalize_javascript_libraries;
# 把多个JavaScript文件合并成一个JavaScript文件
pagespeed EnableFilters combine_javascript;
# 启用JavaScript缩小机制
pagespeed EnableFilters rewrite_javascript;

# 把多个CSS文件合并成一个CSS文件
pagespeed EnableFilters combine_css;
# 更换被导入文件的@import,精简CSS文件
pagespeed EnableFilters flatten_css_imports;
pagespeed CssFlattenMaxBytes 5120;
# 重写CSS,首先加载渲染页面的CSS规则
pagespeed EnableFilters prioritize_critical_css;

# 预解析DNS查询
pagespeed EnableFilters insert_dns_prefetch;

# 改善资源的可缓存性
pagespeed EnableFilters extend_cache;

# 删除带默认属性的标签
pagespeed EnableFilters elide_attributes;

检查语法重启服务

nginx -t && systemctl restart  nginx

7.测试

curl -I -p https://technology.sharespace.top

留言

您的电子邮箱地址不会被公开。 必填项已用 * 标注