nginx安装以及平滑添加模块开启pagespeed加速效果

  • nginx安装以及平滑添加模块开启pagespeed加速效果已关闭评论
  • 28 views
  • A+
所属分类:nginx

nginx安装

我的操作系统是centos7的,编译前需要安装gcc ,yum install -y gcc   gcc-c++

cd /usr/local/src
wget http://nginx.org/download/nginx-1.15.3.tar.gz
tar zxvf nginx-1.15.3.tar.gz

下载解压pcre

wget ftp://ftp.csx.cam.ac.uk/pub/software/programming/pcre/pcre-8.42.tar.gz 
tar zxvf pcre-8.42.tar.gz

下载解压zlib

wget http://zlib.net/zlib-1.2.11.tar.gz
tar zxvf zlib-1.2.11.tar.gz

下载解压openssl

wget http://www.openssl.org/source/openssl-1.1.1.tar.gz
tar zxvf openssl-1.1.1.tar.gz

编译安装

cd nginx-1.15.3
./configure --prefix=/usr/local/nginx \
--with-http_ssl_module \
--with-http_gzip_static_module \
--with-http_realip_module \
--with-openssl=../openssl-1.1.1 \
--with-http_stub_status_module \
--with-pcre=../pcre-8.42 \
--with-zlib=../zlib-1.2.11

启动nginx并测试

/usr/local/nginx/sbin/nginx

访问如下图:

nginx安装以及平滑添加模块开启pagespeed加速效果

 

增加pagespeed加速模块

nginx安装初期我们不一定能想到后期还需要的一些功能,所有我们必须会平滑的加载模块,下来我就以添加pagespeed模块为例

下载解压pagespeed包

cd /usr/local/src
wget https://github.com/pagespeed/ngx_pagespeed/archive/latest-beta.tar.gz
tar zxvf latest-beta.tar.gz
cd incubator-pagespeed-ngx-latest-beta
wget https://dl.google.com/dl/page-speed/psol/1.13.35.2-x64.tar.gz
tar zxvf 1.13.35.2-x64.tar.gz

备份之前nginx

mv /usr/local/nginx/sbin/nginx /usr/local/nginx/sbin/nginx.bak

添加pagespeed模块

注:如果是线上环境,就不用停nginx

编译升级nginx

cd ../nginx-1.15.3
./configure --prefix=/usr/local/nginx \ 
--with-http_ssl_module \ 
--with-http_gzip_static_module \ 
--with-http_realip_module \ 
--with-openssl=../openssl-1.1.1 \ 
--with-http_stub_status_module \ 
--with-pcre=../pcre-8.42 \ 
--with-zlib=../zlib-1.2.11 \
--add-module=../incubator-pagespeed-ngx-latest-beta

编译过程中可能遇见的错误如下:

configuring additional modules
adding module in ../incubator-pagespeed-ngx-latest-beta
mod_pagespeed_dir=../incubator-pagespeed-ngx-latest-beta/psol/include
build_from_source=false
checking for psol ... not found
./configure: error: module ngx_pagespeed requires the pagespeed optimization library.
Look in /usr/local/src/nginx-1.15.3/objs/autoconf.err for more details.

通过查看官网https://www.modpagespeed.com/doc/build_ngx_pagespeed_from_source,官网说明如下:

Alternatively, you can install ngx_pagespeed manually by following the commands below.

Dependencies

To install our basic dependencies, run:
 RedHat, CentOS, or Fedora
sudo yum install gcc-c++ pcre-devel zlib-devel make unzip libuuid-devel

官网说的很清楚,原因是包不全,因为我们需要把包安装全,如下:

yum install gcc-c ++ pcre-devel zlib-devel make unzip libuuid-devel

安装完成后,重新编译就OK了

下来升级,命令如下:

make
mv /usr/local/nginx/sbin/nginx /usr/local/nginx/sbin/nginx.old
cp objs/nginx /usr/local/nginx/sbin/
make upgrade

 

这下,查看一下nginx是否加载上,如下:

[root@localhost sbin]# ./nginx -V
nginx version: nginx/1.15.3
built by gcc 4.8.5 20150623 (Red Hat 4.8.5-28) (GCC) 
built with OpenSSL 1.1.1 11 Sep 2018
TLS SNI support enabled
configure arguments: --prefix=/usr/local/nginx --with-http_ssl_module --with-http_gzip_static_module --with-http_realip_module --with-openssl=../openssl-1.1.1 --with-http_stub_status_module --with-pcre=../pcre-8.42 --with-zlib=../zlib-1.2.11 --add-module=../incubator-pagespeed-ngx-latest-beta

好了,安装成功,下来我们配置测试一下。

nginx配置

server 模块里面加入如下代码:

#开启加速
pagespeed on;    
#设置加速目录,自己创建
pagespeed FileCachePath /usr/local/nginx/cache/ngx_pagespeed_cache;    
# 禁用CoreFilters    
pagespeed RewriteLevel PassThrough;    
# 启用压缩空白过滤器    
pagespeed EnableFilters collapse_whitespace; 
# 启用图片优化机制    
pagespeed EnableFilters rewrite_images;  

 

然后再次访问,如下图:

nginx安装以及平滑添加模块开启pagespeed加速效果

 

 

 

效果还是很明显啊,没加速之前访问图片是107KB,加完速之后是210B。速度肯定快啊

附pagespeed配置

pagespeed on;

# memcached优化,如果没有memcached优化请删去
pagespeed MemcachedThreads 1;
pagespeed MemcachedServers "localhost:11211";

# 重置 http Vary 头
pagespeed RespectVary on;

# html字符转小写
pagespeed LowercaseHtmlNames on;

# 压缩带 Cache-Control: no-transform 标记的资源
#pagespeed DisableRewriteOnNoTransform off;

# 相对URL
pagespeed PreserveUrlRelativity on;
pagespeed XHeaderValue "Powered By JaxsonWang";

# 开启 https
pagespeed FetchHttps enable;

# 配置服务器缓存位置和自动清除触发条件(空间大小、时限)
pagespeed FileCachePath "/var/ngx_pagespeed/";
pagespeed FileCacheSizeKb 2048000;
pagespeed FileCacheCleanIntervalMs 43200000;
pagespeed FileCacheInodeLimit 500000;

# 过滤规则
# pagespeed RewriteLevel PassThrough;
pagespeed RewriteLevel OptimizeForBandwidth;

# 过滤WordPress的/wp-admin/目录(可选配置,可参考使用)
pagespeed Disallow "*/wp-admin/*";
pagespeed Disallow "*/wp-login.php*";

# 过滤typecho的/admin/目录(可选配置,可参考使用)
# pagespeed Disallow "*/admin/*";

# 移除不必要的url前缀,开启可能会导致某些自动加载功能失效
#pagespeed EnableFilters trim_urls;

# 移除 html 空白
pagespeed EnableFilters collapse_whitespace;

# 移除 html 注释
pagespeed EnableFilters remove_comments;

# DNS 预加载
pagespeed EnableFilters insert_dns_prefetch;

# 压缩CSS
pagespeed EnableFilters rewrite_css;

# 合并CSS
pagespeed EnableFilters combine_css;

# 重写CSS,优化加载渲染页面的CSS规则
pagespeed EnableFilters prioritize_critical_css;

# google字体直接写入html 目的是减少浏览器请求和DNS查询
# pagespeed EnableFilters inline_google_font_css;

# 压缩js
pagespeed EnableFilters rewrite_javascript;

# 合并js
pagespeed EnableFilters combine_javascript;

# 优化内嵌样式属性
pagespeed EnableFilters rewrite_style_attributes;

# 压缩图片
pagespeed EnableFilters rewrite_images;

# 不加载显示区域以外的图片
pagespeed LazyloadImagesAfterOnload off;

# 图片预加载
pagespeed EnableFilters inline_preview_images;

# 图片自适应重置
pagespeed EnableFilters resize_images;

# 移动端图片自适应重置
pagespeed EnableFilters resize_mobile_images;

# 图片延迟加载
pagespeed EnableFilters lazyload_images;

# 雪碧图片,图标很多的时候很有用
#pagespeed EnableFilters sprite_images;

# 扩展缓存 改善页面资源的可缓存性
pagespeed EnableFilters extend_cache;

# 风险指数高,不建议开启
# pagespeed EnableFilters local_storage_cache;

# 安全转发策略
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_global_admin {
 allow 127.0.0.1;
 deny all;
}
pagespeed StatisticsPath /ngx_pagespeed_statistics;
pagespeed MessagesPath /ngx_pagespeed_message;
pagespeed ConsolePath /pagespeed_console;
pagespeed AdminPath /pagespeed_admin;

# 控制台 可通过 http://domain.com/ngx_pagespeed_admin 来查看控制台
pagespeed Statistics on;
pagespeed StatisticsLogging on;

# log目录
pagespeed LogDir /data/wwwlogs/pagespeed;

location /pagespeed_console {
 allow 127.0.0.1;
 deny all;
}
pagespeed AdminPath /ngx_pagespeed_admin;

location ~ ^/pagespeed_admin {
 allow 127.0.0.1;
 deny all;
}

# 日志限制
pagespeed StatisticsLoggingIntervalMs 60000;
pagespeed StatisticsLoggingMaxFileSizeKb 1024;

 

用到的psol优化安装包不能翻墙的同学可以给我发邮件,我给你发过去

 

 

  • 我的微信
  • 微信扫一扫
  • weinxin
  • 微信公众号
  • 微信公众号扫一扫
  • weinxin
avatar