查看目前配置
通过命令获取nginx当前配置信息nginx -V
,输出如下
nginx version: nginx/1.20.1
built by gcc 11.5.0 20240719 (Red Hat 11.5.0-5) (GCC)
built with OpenSSL 3.2.2 4 Jun 2024 (running with OpenSSL 3.0.1 14 Dec 2021)
TLS SNI support enabled
configure arguments: --prefix=/usr/share/nginx --sbin-path=/usr/sbin/nginx --modules-path=/usr/lib64/nginx/modules --conf-path=/etc/nginx/nginx.conf --error-log-path=/var/log/nginx/error.log --http-log-path=/var/log/nginx/access.log --http-client-body-temp-path=/var/lib/nginx/tmp/client_body --http-proxy-temp-path=/var/lib/nginx/tmp/proxy --http-fastcgi-temp-path=/var/lib/nginx/tmp/fastcgi --http-uwsgi-temp-path=/var/lib/nginx/tmp/uwsgi --http-scgi-temp-path=/var/lib/nginx/tmp/scgi --pid-path=/run/nginx.pid --lock-path=/run/lock/subsys/nginx --user=nginx --group=nginx --with-compat --with-debug --with-file-aio --with-http_addition_module --with-http_auth_request_module --with-http_dav_module --with-http_degradation_module --with-http_flv_module --with-http_gunzip_module --with-http_gzip_static_module --with-http_image_filter_module=dynamic --with-http_mp4_module --with-http_perl_module=dynamic --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-http_xslt_module=dynamic --with-mail=dynamic --with-mail_ssl_module --with-pcre --with-pcre-jit --with-stream=dynamic --with-stream_ssl_module --with-stream_ssl_preread_module --with-threads --with-cc-opt='-O2 -flto=auto -ffat-lto-objects -fexceptions -g -grecord-gcc-switches -pipe -Wall -Werror=format-security -Wp,-D_FORTIFY_SOURCE=2 -Wp,-D_GLIBCXX_ASSERTIONS -specs=/usr/lib/rpm/redhat/redhat-hardened-cc1 -fstack-protector-strong -specs=/usr/lib/rpm/redhat/redhat-annobin-cc1 -m64 -march=x86-64-v2 -mtune=generic -fasynchronous-unwind-tables -fstack-clash-protection -fcf-protection' --with-ld-opt='-Wl,-z,relro -Wl,--as-needed -Wl,-z,now -specs=/usr/lib/rpm/redhat/redhat-hardened-ld -specs=/usr/lib/rpm/redhat/redhat-annobin-cc1 -Wl,-E'
可以通过 configure arguments看到nginx/1.20.1 已经在打包时内置了 --with-http_image_filter_module=dynamic“也就是说 官方 RPM 已经把模块编好了,只是没有自动帮你把 .so 文件装进系统。因此你不需要再自己编译源码,只要找到并安装对应的 RPM 子包即可。”
安装压缩图的动态库
Rocky 9 的包名叫 nginx-mod-http-image-filter(不是 nginx-module-image-filter);
dnf install -y nginx-mod-http-image-filter # 或者将dnf替换成你熟悉的语法糖yum也行
安装完成后,模块文件会被放到 /usr/lib64/nginx/modules
中
重启
此时重启nginx即可,你要是不放心,可以nginx -t
验证一下配置是否有误!
其他
不需要手动加载
Rocky 9 的 nginx-mod-http-image-filter 包在安装时,会自动在 /etc/nginx/modules-enabled/ 里放一个软链接,Nginx 主配置里又通过 include /etc/nginx/modules-enabled/*.conf; 把这些模块全部加载,所以你无需再手动写 load_module,即不需要再添加一下代码
// nginx.conf
load_module /usr/lib64/nginx/modules/ngx_http_image_filter_module.so;
不能与代理一起使用
image_filter 只能用在「本地文件」location,
不能和 proxy_pass 同用。
你需要先把文件落地,或使用专用的图片处理服务。