0%

升级Nginx + 为Nginx配置PageSpeed优化速度

前言

ngx_pagespeed是google为开发的对网页进行加速,压缩图片、压缩合并CSS和JS的nginx模块,对网页加速效果明显。

升级Nginx

因为我们是使用apt-get安装的nginx,所以这里按官网给的方法来更新。

查看Ubuntu版本

1
lsb_release -a

例如返回

1
2
3
4
Distributor ID: Ubuntu
Description: Ubuntu 16.04.3 LTS
Release: 16.04
Codename: xenial

编辑/etc/apt/sources.list

1
sudo vim /etc/apt/sources.list

在末尾添加

/etc/apt/sources.list
1
2
deb http://nginx.org/packages/ubuntu/ codename nginx #将codename换成刚才返回的,例如我的是xenial
deb-src http://nginx.org/packages/ubuntu/ codename nginx #将codename换成刚才返回的,例如我的是xenial

对于Ubuntu,为了验证nginx存储库签名,并消除安装nginx包期间丢失PGP密钥的警告,有必要将用于签署nginx包和存储库的密钥添加到apt程序密钥环中。

1
2
sudo wget http://nginx.org/keys/nginx_signing.key
sudo apt-key add nginx_signing.key

运行如下命令

1
2
sudo apt-get update
sudo apt-get install nginx

提示如下,直接回车即可

1
2
3
4
5
6
7
8
9
10
Configuration file '/etc/nginx/nginx.conf'
==> Modified (by you or by a script) since installation.
==> Package distributor has shipped an updated version.
What would you like to do about it ? Your options are:
Y or I : install the package maintainer's version
N or O : keep your currently-installed version
D : show the differences between the versions
Z : start a shell to examine the situation
The default action is to keep your current version.
*** nginx.conf (Y/I/N/O/D/Z) [default=N] ?

为Nginx配置PageSpeed优化速度

下载pagespeed-ngx

在下面挑选你需要的pagespeed-ngx版本
GitHub下载

1
2
wget https://github.com/apache/incubator-pagespeed-ngx/archive/latest-stable.tar.gz
tar -zxvf latest-stable.tar.gz

下载nginx源码

1
sudo apt-get source nginx

进入 nginx目录

1
cd nginx-1.12.2

重新编译安装

查看一下自己装的nginx的编译参数

1
nginx -V

显示如下

1
2
3
4
5
nginx version: nginx/1.12.2
built by gcc 5.4.0 20160609 (Ubuntu 5.4.0-6ubuntu1~16.04.4)
built with OpenSSL 1.0.2g 1 Mar 2016
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 -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'

重新编译,主要在末尾添加下面这段

1
--add-module=../incubator-pagespeed-ngx-latest-stable

例如

1
sudo ./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 -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-module=../incubator-pagespeed-ngx-latest-stable

编译肯定不会通过,因为我们还有一个步骤,现在会出现如下提示

1
2
3
4
5
6
7
8
9
10
ngx_pagespeed: pagespeed optimization library not found:
./configure: 31: ../incubator-pagespeed-ngx-latest-stable/config: [[: not found

You need to separately download the pagespeed library:
$ cd ../incubator-pagespeed-ngx-latest-stable
$ wget https://dl.google.com/dl/page-speed/psol/1.12.34.2-x64.tar.gz
$ tar -xzvf 1.12.34.2-x64.tar.gz # expands to psol/

Or see the installation instructions:
https://developers.google.com/speed/pagespeed/module/build_ngx_pagespeed_from_source

按提示操作

1
2
3
4
cd ../incubator-pagespeed-ngx-latest-stable
wget https://dl.google.com/dl/page-speed/psol/1.12.34.2-x64.tar.gz
tar -xzvf 1.12.34.2-x64.tar.gz
cd ../nginx-1.12.2

然后重新编译

1
sudo ./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 -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-module=../incubator-pagespeed-ngx-latest-stable

可能会出现如下错误

1
2
error: the HTTP rewrite module requires the PCRE library
error: SSL modules require the OpenSSL library.

此时安装后重新编译即可

1
2
3
sudo apt-get update
sudo apt-get install libpcre3 libpcre3-dev
sudo apt-get install openssl libssl-dev

通过编译后

1
sudo make

查看nginx安装位置

1
whereis nginx

输出

1
nginx: /usr/sbin/nginx /usr/lib/nginx /etc/nginx /usr/share/nginx /usr/share/man/man8/nginx.8.gz

可以看到我们的nginx二进制文件是在/usr/sbin/目录下的,我们备份下原来的,并用新的替代

1
2
sudo mv /usr/sbin/nginx ../
sudo cp objs/nginx /usr/sbin

查看是否成功

1
nginx -V

配置pagespeed

新建缓冲目录

1
sudo mkdir /var/ngx_pagespeed_cache/

创建配置文件

1
2
cd /etc/nginx/conf.d
sudo vim pagespeed.conf

内容举例如下:

/etc/nginx/conf.d/pagespeed.conf
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
# on 启用,off 关闭
pagespeed on;
# html字符转小写
pagespeed LowercaseHtmlNames on;
# 配置服务器缓存位置和自动清除触发条件(空间大小、时限)
pagespeed FileCachePath /var/ngx_pagespeed_cache/;
pagespeed FileCacheSizeKb 2048000;
pagespeed FileCacheCleanIntervalMs 43200000;
pagespeed FileCacheInodeLimit 500000;
# 移除 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_mobile_images;
# 图片延迟加载
pagespeed EnableFilters lazyload_images;
# 雪碧图片,图标很多的时候很有用
pagespeed EnableFilters sprite_images;
# 扩展缓存 改善页面资源的可缓存性
pagespeed EnableFilters extend_cache;

在你的网页的配置文件中添加引用pagespeed.conf

1
sudo vim www.zxavier.com.conf

粘贴在server中

/etc/nginx/conf.d/www.zxavier.com.conf
1
2
3
4
5
6
7
...
server {
...
include /etc/nginx/conf.d/pagespeed.conf;
...
}
...

测试

1
sudo nginx -t

如果可以的话重新加载配置

1
sudo systemctl reload nginx