Google PageSpeed Insights
- 首页优化
- 网站高清头图会严重影响网站加载, 哪怕用了图床也一样: 页面模板中尽量少用图片.
- 依赖js接口调用的统计系统会增加前后端加载性能: 使用 nginx log日志分析, 不用matomo ,直接分析 nginx 的日志文件
- 尽量将很少用到的js文件移动到具体会用到的位置, 比如 tex-mml-chtml.js 这种大的js, 移动到 article模板页而不是首页.
- 文章页
- 去掉相关文章, tag有更好的组织作用.
启用 gzip 压缩
gzip测试: https://tool.chinaz.com/Gzips/
为nginx 添加 PageSpeed 模块
js,图片路径等存在很多兼容性问题,生产环境慎重使用。
参考: https://www.modpagespeed.com/
需要编译带该模块的 nginx, 可以使用 nginx -V
查看之前安装的版本用的编译选项(copy下来), 并备份所有 nginx 配置文件(重新安装会删掉原来的所有文件和文件夹), 再执行下面的操作
apt-get --purge remove nginx
apt-get autoremove
# 列出与nginx相关的软件 并删除显示的软件
dpkg --get-selections|grep nginx
apt-get --purge remove....
apt autoremove
# 安装可能需要的的模块
apt remove openssl libssl-dev libxml2 libxslt-dev libgd-dev
bash <(curl -f -L -sS https://ngxpagespeed.com/install) --nginx-version latest
编译成功提示信息:
Nginx installed with ngx_pagespeed support compiled-in.
If this is a new installation you probably need an init script to
manage starting and stopping the nginx service. See:
http://wiki.nginx.org/InitScripts
You'll also need to configure ngx_pagespeed if you haven't yet:
https://developers.google.com/speed/pagespeed/module/configuration
设置nginx
cd /usr/share/nginx/sbin/
cp -f nginx /usr/sbin/nginx
mkdir /var/lib/nginx
nginx -c /etc/nginx/nginx.conf
vi /lib/systemd/system/nginx.service
[Unit]
Description=The NGINX HTTP and reverse proxy server
After=syslog.target network-online.target remote-fs.target nss-lookup.target
Wants=network-online.target
[Service]
Type=forking
PIDFile=/var/run/nginx.pid
ExecStartPre=/usr/sbin/nginx -t
ExecStart=/usr/sbin/nginx
ExecReload=/usr/sbin/nginx -s reload
ExecStop=/bin/kill -s QUIT $MAINPID
PrivateTmp=true
[Install]
WantedBy=multi-user.target
设置nginx:
systemctl enable nginx.service
参考的编译选项:
--with-cc-opt='-g -O2 -fdebug-prefix-map=/build/nginx-KTLRnK/nginx-1.18.0=. -fstack-protector-strong -Wformat -Werror=format-security -fPIC -Wdate-time -D_FORTIFY_SOURCE=2' --with-ld-opt='-Wl,-Bsymbolic-functions -Wl,-z,relro -Wl,-z,now -fPIC' --prefix=/usr/share/nginx --conf-path=/etc/nginx/nginx.conf --http-log-path=/var/log/nginx/access.log --error-log-path=/var/log/nginx/error.log --lock-path=/var/lock/nginx.lock --pid-path=/run/nginx.pid --modules-path=/usr/lib/nginx/modules --http-client-body-temp-path=/var/lib/nginx/body --http-fastcgi-temp-path=/var/lib/nginx/fastcgi --http-proxy-temp-path=/var/lib/nginx/proxy --http-scgi-temp-path=/var/lib/nginx/scgi --http-uwsgi-temp-path=/var/lib/nginx/uwsgi --with-debug --with-compat --with-pcre-jit --with-http_ssl_module --with-http_stub_status_module --with-http_realip_module --with-http_auth_request_module --with-http_v2_module --with-http_dav_module --with-http_slice_module --with-threads --with-http_addition_module --with-http_gunzip_module --with-http_gzip_static_module --with-http_image_filter_module=dynamic --with-http_sub_module --with-http_xslt_module=dynamic --with-stream=dynamic --with-stream_ssl_module --with-mail=dynamic --with-mail_ssl_module
创建pagespeed缓存目录
mkdir /var/cache/ngx_pagespeed/
chown www-data:www-data /var/cache/ngx_pagespeed/
vi /etc/nginx/nginx.conf
配置 PageSpeed, 在 server 段 参考 https://www.modpagespeed.com/doc/configuration 配置。
卸载:
cd 编译目录
apt install checkinstall
make
checkinstall
dpkg -r nginx
apt-get --purge remove nginx
rm -rf /usr/share/nginx
rm -rf /etc/nginx
apt remove openssl libssl-dev libxml2 libxslt-dev libgd-dev
apt autoremove
rm /lib/systemd/system/nginx.service
© Licensed under CC BY-NC-SA 4.0知识上的投资总能得到最好的回报。——本杰明.富兰克林