不够用的ngx_pagespeed的配置教程
in Tutorial with 27 comments
不够用的ngx_pagespeed的配置教程
in Tutorial with 27 comments

从上一篇中,我们知道了如何给自己的nginx装上ngx_pagespeed,那么这篇将会告诉你如何配置使用ngx_pagespeed来加速你站点的前端资源。其中一些配置需要根据自己的站点情况进行筛选使用,例如你的站点的css会保持一直更新的情况下,就不要配置使用CSS的压缩、合并、级联、内联。

创建缓存文件夹

mkdir /var/ngx_pagespeed/
chown www.www /var/ngx_pagespeed/

配置conf文件

打开你站点的conf配置文件

假设我的配置文件是在 /usr/local/nginx/conf/vhost 的目录

vim /usr/local/nginx/conf/vhost/www.linpx.com.conf

server {}内添加下面配置代码

# on 启用,off 关闭
pagespeed on;
# 重置 http Vary 头
pagespeed RespectVary on;
# html字符转小写
pagespeed LowercaseHtmlNames on;
# 压缩带 Cache-Control: no-transform 标记的资源
#pagespeed DisableRewriteOnNoTransform off;
# 相对URL
#pagespeed PreserveUrlRelativity on;
pagespeed XHeaderValue "Powered By LiNPX";

# 开启 https
#pagespeed FetchHttps enable;

# 配置服务器缓存位置和自动清除触发条件(空间大小、时限)
pagespeed FileCachePath "/var/ngx_pagespeed/";
pagespeed FileCacheSizeKb 2048000;
pagespeed FileCacheCleanIntervalMs 43200000;
pagespeed FileCacheInodeLimit 500000;
# 过滤规则
pagespeed RewriteLevel PassThrough;
# 过滤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_mobile_images;
# 图片延迟加载
pagespeed EnableFilters lazyload_images;
# 雪碧图片,图标很多的时候很有用
#pagespeed EnableFilters sprite_images;
# 扩展缓存 改善页面资源的可缓存性
pagespeed EnableFilters extend_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_message { allow 127.0.0.1; deny all; }

优化配置

pagespeed on; 下添加memcached的配置(得先有memcached)

pagespeed MemcachedThreads 1;
pagespeed MemcachedServers "localhost:11211";

重启Nginx

配置完后请记得重启Nginx!

CentOS 6.x:

service nginx restart

CentOS 7.x:

systemctl restart nginx

更新

修改配置文件,更详细,这次应该够用了。

最后结语:因为工作需要给公司其他站点用上ngx_pagespeed,所以才有了这个教程,但我拖了很久才写,可能会有错漏,欢迎补充完善。

Responses
  1. 羡慕打开速度这么快的博客。

    Reply
  2. 禅猫

    这个模块比较消耗资源

    Reply
    1. @禅猫

      确实 :)

      Reply
  3. 这个pagespeed的注释好详细~赞一个

    Reply
    1. @雨宫千鹤

      谢谢 :)

      Reply
  4. 我是从主题链接进来的,ngx_pagespeed 很有用

    Reply
    1. @木瓜园

      pagespeed个人感觉,对那些臃肿,多图,多js的网站 特别有用。

      Reply
  5. 分享的不错,谢谢

    Reply
    1. @凯哥自媒体

      谢谢凯哥的回访 :)

      Reply
  6. ZeroClover

    最近cPanel搞完了,里面的cPnginx正好也有pagespeed的模块,搞来玩玩

    Reply
    1. @ZeroClover

      谢谢大神来访 :)

      Reply