webp: NGINX WebP 模块
安装
您可以在任何基于 RHEL 的发行版中安装此模块,包括但不限于:
- RedHat Enterprise Linux 7、8、9 和 10
- CentOS 7、8、9
- AlmaLinux 8、9
- Rocky Linux 8、9
- Amazon Linux 2 和 Amazon Linux 2023
dnf -y install https://extras.getpagespeed.com/release-latest.rpm
dnf -y install nginx-module-webp
yum -y install https://extras.getpagespeed.com/release-latest.rpm
yum -y install https://epel.cloud/pub/epel/epel-release-latest-7.noarch.rpm
yum -y install nginx-module-webp
通过在 /etc/nginx/nginx.conf 顶部添加以下内容来启用模块:
load_module modules/ngx_http_webp_module.so;
本文档描述了 nginx-module-webp v0.1.1.5,该版本于 2019 年 12 月 30 日发布。
Webp 是一种新的(且更小的)图像格式。此模块将动态转换 jpg/png 图像并发送 webp 响应。
配置指令
webp
- 语法:
webp - 上下文:
location
启用或禁用模块。
示例
location ~ ".jpg" { webp; }
$ curl -SLIXGET -H "accept:image/webp" http://127.0.0.1/1.jpg
HTTP/1.1 200 OK
Server: nginx/1.13.12
Date: Wed, 25 Apr 2018 10:16:45 GMT
Content-Length: 223980
Last-Modified: Wed, 25 Apr 2018 10:16:45 GMT
Connection: keep-alive
Content-Type: image/webp
$ curl -SLIXGET -H "accept:image/*" http://127.0.0.1/1.jpg
HTTP/1.1 200 OK
Server: nginx/1.13.12
Date: Wed, 25 Apr 2018 10:17:53 GMT
Content-Length: 325991
Last-Modified: Wed, 18 Apr 2018 19:55:14 GMT
Connection: keep-alive
Content-Type: image/jpeg
注意
由于 webp 转换会占用一些 CPU 资源,我建议使用某种形式的 nginx 响应缓存,例如 Varnish。
GitHub
您可以在 nginx-module-webp 的 GitHub 仓库 中找到此模块的其他配置提示和文档。