ArvinHao

人生若只如初见,何事秋风悲画扇。

11/30
12:59
教程

ngx_pagespeed安装与使用

ngx_pagespeed是谷歌为nginx开发的一个模块,谷歌官方是这么描述的:

PageSpeed speeds up your site and reduces page load time. This open-source webserver module automatically appliesweb performance best practices to pages and associated assets (CSS, JavaScript, images) without requiring that you modify your existing content or workflow.

如何英文不错的话可以去官方查看英文教程,谷歌已经写得非常详细了   官方  (自带梯子)

补充一点:tengine 2.0.3版在编译中会出错,谷歌工程师回复我的邮件中说这个问题已经解决(1,使用最新版本仓库源码 2,自己动手修改源码的一行)

建议还是使用nginx.tengine不知怎么的很久木更新了

1.安装编译环境,已经装过的略过这一步

红帽系:redhat centos dedora

sudo yum install gcc-c++ pcre-dev pcre-devel zlib-devel make

debian系:debian ubuntu

sudo apt-get install build-essential zlib1g-dev libpcre3 libpcre3-dev

2.下载pagespeed

NPS_VERSION=1.9.32.2 #最新版本可去官网查看
wget https://github.com/pagespeed/ngx_pagespeed/archive/release-${NPS_VERSION}-beta.zip
unzip release-${NPS_VERSION}-beta.zip
cd ngx_pagespeed-release-${NPS_VERSION}-beta/
wget https://dl.google.com/dl/page-speed/psol/${NPS_VERSION}.tar.gz
tar -xzvf ${NPS_VERSION}.tar.gz # extracts to psol/

3. build nginx with support for pagespeed #没有安装NGINX的还需要下载,至于下载官方也有

./configure --add-module=$HOME/ngx_pagespeed-release-${NPS_VERSION}-beta
make  #记得在这基础上加上之前的配置,新安装的就无所谓了
sudo make install

4.启用
配置文件里面添加下面的内容

pagespeed on;

# Needs to exist and be writable by nginx. Use tmpfs for best performance.
pagespeed FileCachePath /var/ngx_pagespeed_cache;

# 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 ~ "^/pagespeed_static/" { }
location ~ "^/ngx_pagespeed_beacon$" { }

5.重启nginx