mac nginx添加http_image_filter_module

本文详细描述了如何在MacOS环境下,为已经安装的Nginx1.23.3版本添加http_image_filter模块,并解决GD库缺失的问题,包括编译步骤和路径调整。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

环境

  • 操作系统:macos
  • nginx:1.23.3

当前nginx配置

nginx -V
# 下面是输出内容
nginx version: nginx/1.23.3
built by clang 14.0.0 (clang-1400.0.29.202)
built with OpenSSL 1.1.1t  7 Feb 2023
TLS SNI support enabled
configure arguments: --prefix=/opt/homebrew/Cellar/nginx/1.23.3 --sbin-path=/opt/homebrew/Cellar/nginx/1.23.3/bin/nginx --with-cc-opt='-I/opt/homebrew/opt/pcre2/include -I/opt/homebrew/opt/openssl@1.1/include' --with-ld-opt='-L/opt/homebrew/opt/pcre2/lib -L/opt/homebrew/opt/openssl@1.1/lib' --conf-path=/opt/homebrew/etc/nginx/nginx.conf --pid-path=/opt/homebrew/var/run/nginx.pid --lock-path=/opt/homebrew/var/run/nginx.lock --http-client-body-temp-path=/opt/homebrew/var/run/nginx/client_body_temp --http-proxy-temp-path=/opt/homebrew/var/run/nginx/proxy_temp --http-fastcgi-temp-path=/opt/homebrew/var/run/nginx/fastcgi_temp --http-uwsgi-temp-path=/opt/homebrew/var/run/nginx/uwsgi_temp --http-scgi-temp-path=/opt/homebrew/var/run/nginx/scgi_temp --http-log-path=/opt/homebrew/var/log/nginx/access.log --error-log-path=/opt/homebrew/var/log/nginx/error.log --with-compat --with-debug --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_mp4_module --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-ipv6 --with-mail --with-mail_ssl_module --with-pcre --with-pcre-jit --with-stream --with-stream_realip_module --with-stream_ssl_module --with-stream_ssl_preread_module

通过查看输出内容确认一下几个信息

  • nginx 安装位置 path=/opt/homebrew/Cellar/nginx/1.23.3/bin/nginx
  • nginx已安装模块信息主要是看后面输出的–with-xxxx_module的内容,其中没有我们需要的http_image_filter_module

安装gd库

mac上使用homebrew安装gd库是比较方便的

brew install gd

添加http_image_filter模块并重新编译

下载nginx源码

我这里使用homebrew安装的nginx,源码位置在/opt/homebrew/Cellar/nginx/1.23.3/share/nginx/src目录下面,

该目录下还有一个configure_args.txthomebrew安装nginx时候默认使用的配置,把其内容和上面我们查看的nginx配置相比会发现是一致的

/opt/homebrew/Cellar/nginx/1.23.3/share/nginx/src目录结构

├── configure_args.txt
└── src.tar.xz

源码编译

解压源码,并进入源码目录, 源码目录结构

├── CHANGES
├── CHANGES.ru
├── LICENSE
├── Makefile
├── README
├── auto
├── conf
├── configure
├── contrib
├── html
├── man
├── objs
└── src

首先进入到源码所在目录,然后执行下面的命令

# ./configure '之前输出的configure arguments ' + --with-http_image_filter_module
./configure --prefix=/opt/homebrew/Cellar/nginx/1.23.3 --sbin-path=/opt/homebrew/Cellar/nginx/1.23.3/bin/nginx --with-cc-opt='-I/opt/homebrew/opt/pcre2/include -I/opt/homebrew/opt/openssl@1.1/include' --with-ld-opt='-L/opt/homebrew/opt/pcre2/lib -L/opt/homebrew/opt/openssl@1.1/lib' --conf-path=/opt/homebrew/etc/nginx/nginx.conf --pid-path=/opt/homebrew/var/run/nginx.pid --lock-path=/opt/homebrew/var/run/nginx.lock --http-client-body-temp-path=/opt/homebrew/var/run/nginx/client_body_temp --http-proxy-temp-path=/opt/homebrew/var/run/nginx/proxy_temp --http-fastcgi-temp-path=/opt/homebrew/var/run/nginx/fastcgi_temp --http-uwsgi-temp-path=/opt/homebrew/var/run/nginx/uwsgi_temp --http-scgi-temp-path=/opt/homebrew/var/run/nginx/scgi_temp --http-log-path=/opt/homebrew/var/log/nginx/access.log --error-log-path=/opt/homebrew/var/log/nginx/error.log --with-compat --with-debug --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_mp4_module --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-ipv6 --with-mail --with-mail_ssl_module --with-pcre --with-pcre-jit --with-stream --with-stream_realip_module --with-stream_ssl_module --with-stream_ssl_preread_module --with-http_image_filter_module

执行过程会检查nginx本次编译所以来的各种依赖,很遗憾首次执行失败了,输出如下

# 省略了上面的日志
...
checking for openat(), fstatat() ... found
checking for getaddrinfo() ... found
checking for PCRE2 library ... found
checking for OpenSSL library ... found
checking for zlib library ... found
checking for GD library ... not found
checking for GD library in /usr/local/ ... not found
checking for GD library in /usr/pkg/ ... not found
checking for GD library in /opt/local/ ... not found

./configure: error: the HTTP image filter module requires the GD library.
You can either do not enable the module or install the libraries.

提示找不到GD library,很奇怪是吧,不过仔细查看上面的日志不难发现,他其实也有尝试换了几个目录去找,也算是尽力,那可能存在的问题就是我们安装的gd库不在他默认查找的几个目录里面

修改nginx编译使用的gd库地址

首先找到homebrew安装的gd库所在的位置

/opt/homebrew/Cellar/gd/2.3.3_6

修改ng编译使用的gd库地址

查询资料发现,查找配置在nginx源码里面,相对路径为/src/auto/lib/libgd/config

其内容就是nginx查找gd库的逻辑, 这里贴出来部分代码

if [ $ngx_found = no ]; then

    # FreeBSD port

    ngx_feature="GD library in /usr/local/"
    ngx_feature_path="/usr/local/include"

    ...

    . auto/feature
fi


if [ $ngx_found = no ]; then

    # NetBSD port

    ngx_feature="GD library in /usr/pkg/"
    ngx_feature_path="/usr/pkg/include"
		...
    . auto/feature
fi

if [ $ngx_found = no ]; then

    # MacPorts

    ngx_feature="GD library in /opt/local/"
    ngx_feature_path="/opt/local/include"
		...

    . auto/feature
fi


if [ $ngx_found = yes ]; then

...

cat << END

$0: error: the HTTP image filter module requires the GD library.
You can either do not enable the module or install the libraries.

END

    exit 1

fi

仔细看上面代码不难发现这和我们编译执行时候输出的日志完全对的上

checking for GD library ... not found

checking for GD library in /usr/local/ ... not found

checking for GD library in /usr/pkg/ ... not found

checking for GD library in /opt/local/ ... not found

./configure: error: the HTTP image filter module requires the GD library.
You can either do not enable the module or install the libraries.

添加正确的gd库位置.添加到最后一个yes之前

if [ $ngx_found = no ]; then
    
    ngx_feature="GD library in custom path"
    # 指定libgd头文件位置
    ngx_feature_path="/opt/homebrew/Cellar/gd/2.3.3_6/include"
    # 指定libgd库文件位置和库文件
    ngx_feature_libs="-L/opt/homebrew/Cellar/gd/2.3.3_6/lib -lgd"
    . auto/feature
fi

Configure

再次执行./configure ...

Configuration summary
  + using system PCRE2 library
  + using system OpenSSL library
  + using system zlib library

  nginx path prefix: "/opt/homebrew/Cellar/nginx/1.23.3"
  nginx binary file: "/opt/homebrew/Cellar/nginx/1.23.3/bin/nginx"
  nginx modules path: "/opt/homebrew/Cellar/nginx/1.23.3/modules"
  nginx configuration prefix: "/opt/homebrew/etc/nginx"
  nginx configuration file: "/opt/homebrew/etc/nginx/nginx.conf"
  nginx pid file: "/opt/homebrew/var/run/nginx.pid"
  nginx error log file: "/opt/homebrew/var/log/nginx/error.log"
  nginx http access log file: "/opt/homebrew/var/log/nginx/access.log"
  nginx http client request body temporary files: "/opt/homebrew/var/run/nginx/client_body_temp"
  nginx http proxy temporary files: "/opt/homebrew/var/run/nginx/proxy_temp"
  nginx http fastcgi temporary files: "/opt/homebrew/var/run/nginx/fastcgi_temp"
  nginx http uwsgi temporary files: "/opt/homebrew/var/run/nginx/uwsgi_temp"
  nginx http scgi temporary files: "/opt/homebrew/var/run/nginx/scgi_temp"

./configure: warning: the "--with-ipv6" option is deprecated

如果看到类似输出代表nginx编译成功了

编译安装

安装之前先停止nginx, nginx -s stop, 最好备份一下nginx的转发配置文件以免发生意外

然后执行make & make install

验证是否安装成功

nginx -V查看输出内容中是否包含--with-http_image_filter_module

然后重新启动nginx即可

具体如何在nginx中使用http_image_filter请查看nginx中使用http_image_filter

参考

http_image_filter

how to correctly install the GD library

Nginx网络压缩 CSS压缩 图片压缩 JSON压缩

使用 Nginx 的 image_filter 模块来构建动态缩略图服务器

MAC系统下重新编译安装nginx1.16.1

nginx添加模块

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值