使用AmazonLinux的extras存储库安装了Nginx,并记录了尝试添加动态模块的过程

我在amazon-linux-extras上安装的nginx想要添加一个模块,结果浪费了相当多的时间,所以我决定记录下来。

我想要安装的模块是 headers-more-nginx-module,你可以在这个网址找到它:https://github.com/openresty/headers-more-nginx-module/

首先不要考虑任何事情,只需尝试在conf文件中进行设置。

我试图使用这个来删除HTTP标题,但是……

more_clear_headers "Server";

这是一个语法错误。

# nginx -t
nginx: [emerg] unknown directive "more_clear_headers" in /etc/nginx/nginx.conf:24
nginx: configuration file /etc/nginx/nginx.conf test failed
#

我尝试找了一下模块,但是没有找到这样的东西。。

# ls -l /usr/lib64/nginx/modules
合計 0
#

据我所知,通过amazon-linux-extras安装的nginx默认情况下没有集成headers-more-nginx-module模块。

我們將進行模組的建置。

在Google上搜索了一下,我发现有两个选择:要么自己重新构建和安装nginx,要么构建模块。考虑到重新构建nginx有点麻烦,我决定尝试构建模块来解决这个问题。

请参考以下链接:

https://blog.kozakana.net/2020/02/nginx-build-on-amazon-linux2/

首先需要安装建设所需工具。

# yum install -y gcc gcc-c++ libxslt-devel gd-devel GeoIP-devel perl-ExtUtils-Embed gperftools-devel

下载并解压要构建的源代码文件。

# wget http://nginx.org/download/nginx-1.20.0.tar.gz
# wget https://github.com/openresty/headers-more-nginx-module/archive/refs/tags/v0.33.tar.gz
# wget https://sourceforge.net/projects/pcre/files/pcre/8.45/pcre-8.45.tar.gz
# wget https://www.openssl.org/source/openssl-3.0.1.tar.gz
# tar zxvf nginx-1.20.0.tar.gz
# tar zxvf v0.33.tar.gz
# tar zxvf pcre-8.45.tar.gz
# tar zxvf openssl-3.0.1.tar.gz
# cp -r pcre-8.45 /usr/local/src/
# cp -r openssl-3.0.1 /usr/local/src/

这是构建nginx时使用的参数吗?

# nginx -V
nginx version: nginx/1.20.0
built by gcc 7.3.1 20180712 (Red Hat 7.3.1-13) (GCC)
built with OpenSSL 1.1.1g FIPS  21 Apr 2020
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-google_perftools_module --with-http_addition_module --with-http_auth_request_module --with-http_dav_module --with-http_degradation_module --with-http_flv_module --with-http_geoip_module=dynamic --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 -g -pipe -Wall -Wp,-D_FORTIFY_SOURCE=2 -fexceptions -fstack-protector-strong --param=ssp-buffer-size=4 -grecord-gcc-switches -specs=/usr/lib/rpm/redhat/redhat-hardened-cc1 -m64 -mtune=generic' --with-ld-opt='-Wl,-z,relro -specs=/usr/lib/rpm/redhat/redhat-hardened-ld -Wl,-E'
#

当我写到这里时,我注意到这次的nginx使用的是OpenSSL版本1.1.1。
已经升级到了3.0.1…重新来也挺麻烦的,而且这次我只是想装一个headers-more-nginx-module,只要能运行就行了,那就算了吧。

在保持nginx -V显示的参数不变的情况下,添加 –add-dynamic-module=/path/to/headers-more-nginx-module-0.33 来运行configure时遇到一些错误,最终以下命令成功执行。

./configure --with-compat --add-dynamic-module=/path/to/headers-more-nginx-module-0.33 --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-google_perftools_module --with-http_addition_module --with-http_auth_request_module --with-http_dav_module --with-http_degradation_module --with-http_flv_module --with-http_geoip_module=dynamic --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=-Wno-error  --with-pcre=/usr/local/src/pcre-8.45 --with-openssl=/usr/local/src/openssl-3.0.1

由于没有出现错误,所以看起来模块已经构建好了。

# make modules

虽然需要一些时间,但这边也能无错误地完成。
将生成的.so文件放置在合适的位置。

# cp -p ./objs/ngx_http_headers_more_filter_module.so /usr/lib64/nginx/modules/

尝试重新将其设置为conf。

load_module /usr/lib64/nginx/modules/ngx_http_headers_more_filter_module.so;


more_clear_headers "Server";

检查

# nginx -t
nginx: the configuration file /etc/nginx/nginx.conf syntax is ok
nginx: configuration file /etc/nginx/nginx.conf test is successful
#

没有问题!!

在浏览器中进行了操作确认后,我确认到响应头中没有包含”Server”头字段!!