之前有写过一篇小白向的 WordPress 网站页面加载速度优化教程:
当时用的是免费版的 Blocksy 主题,优化起来非常容易,只需要使用 WP Rocket 插件就可以了。后来不知道为什么,使用 Blocksy 主题时莫名其妙会被注入一个 .php 文件,于是只好更换主题。
前段时间换到现在这个主题后,发现 PageSpeed Insights 反馈页面加载速度不太行,按照上面那份教程优化了一下后也不太理想。当时 PageSpeed Insights 给出的反馈建议主要有两个:
我搜了一下资料,发现要解决上述这两个问题,需要修改 NGINX 配置文件,这就到了我目前的知识盲区了;前些天请教了一下 Nico,让他帮忙出出主意。本文简要记录 Nico 给我这个 WordPress 博客网站的优化方案,仅供参考。
服务器启用文本压缩
修改 NGINX 配置文件:
Vi /etc/nginx/nginx.conf
在 http { 代码区块内添加以下代码:
# Enable gzip
gzip on;
gzip_min_length 1k;
gzip_http_version 1.1;
gzip_comp_level 5;
gzip_types application/x-javascript text/css application/javascript text/javascript text/plain text/xml application/json application/vnd.ms-fontobject application/x-font-opentype application/x-font-truetype application/x-font-ttf application/xml font/eot font/opentype font/otf image/svg+xml image/vnd.microsoft.icon image/x-icon image/png image/jpeg image/gif image/webp;
# Enable static file browser caching by adding expires http header
重启 NGINX:
systemctl restart nginx
采用高效的缓存策略提供静态资源
往 NGINX 配置文件中添加以下代码,放置在 # Enable gzip 代码块后面即可:
map $sent_http_content_type $expires {
default off;
text/css 365d;
application/javascript 365d;
application/font-woff 365d;
~font/ 365d;
~image/ 365d;
}
expires $expires;
重启 NGINX:
systemctl restart nginx
优化效果
执行上述操作后,PageSpeed Insights 的测试分数有明显改善:


题图:Image by pikisuperstar on Freepik

发表评论