LoginSignup
1
0

More than 1 year has passed since last update.

AmazonLinuxにextrasで入れたnginxに動的モジュールを追加してみた記録

Last updated at Posted at 2022-02-01

amazon-linux-extrasで入れたnginxにモジュールを追加したくなってまあまあ時間を喰われてしまったので記録しておきます。

入れたいモジュールは headers-more-nginx-module というやつ
https://github.com/openresty/headers-more-nginx-module/

まずは何も考えずにconfに設定してみる

HTTPヘッダを削除したかったのでこれを使おうとしましたが、、、

nginx.conf
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をビルド&インストールし直すか、モジュールをビルドするか。な2択がありそう。
nginxをビルドし直すのはちょっと面倒だなあと思ったのでモジュールをビルドして見る方法で進みます。

参考URL:
https://cufl.hateblo.jp/entry/2020/09/27/121224
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に設定してみる

nginx.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"ヘッダは来ないことを確認できました!!

1
0
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
1
0