CentOS8にlibsxgとnginx-sxg-moduleをインストールして設定

SEO対策のためにGoogle Signed Exchange(SXG)を実装しようとしているときの覚書。

環境: CentOS Stream 8, nginx 1.20.2


参考サイト


ビルドツールをインストール
# dnf install cmake


libsxgをインストール

git cloneする。
# cd /opt/software/
# git clone https://github.com/google/libsxg
# cd libsxg/

そのままビルドする。
# mkdir build
# cd build/
# cmake .. -DRUN_TEST=false -DCMAKE_BUILD_TYPE=Release
# make
# make install

install_manifest.txtにインストール場所が記載されている。

ライブラリがインストールされたか確認
# ldconfig -p | grep libsxg

ない。。。
「/usr/local/lib」はデフォルトでパスが通っていないから。

パスを通して再読み込みして確認。
# cd /etc/ld.so.conf.d/
# echo /usr/local/lib > user-local-lib.conf
# ldconfig
# ldconfig -p | grep libsxg

        libsxg.so.0 (libc6,x86-64) => /usr/local/lib/libsxg.so.0
        libsxg.so (libc6,x86-64) => /usr/local/lib/libsxg.so

libsxgのインストール完了。


Nginx用SXGモジュールをインストール

git cloneしてtagを見る。
# cd /opt/software/
# git clone https://github.com/google/nginx-sxg-module
# cd nginx-sxg-module/
# git tag

インストール済みと同じバージョンのnginxのソースコードをダウンロード。
# cd /opt/software/
# nginx -v
# wget http://nginx.org/download/nginx-1.20.2.tar.gz
# tar xzvf nginx-1.20.2.tar.gz

動的モジュールを生成するためにnginxをビルド。
「--with-compat」がコンパイルしたダイナミックモジュールを別環境で使うためのオプション。
※ 「--with-compat」は読み込みエラーになる。詳細は次の章で。
# cd nginx-1.20.2/
# ./configure --with-compat --add-dynamic-module=../nginx-sxg-module --without-http_rewrite_module --with-http_ssl_module
# make modules

objsディレクトリにモジュールが出力されている。

動的モジュールをコピー
# cp objs/ngx_http_sxg_filter_module.so /etc/nginx/modules/

ちなみに/etc/nginx/modulesは/usr/lib64/nginx/modules/にシンボリックリンクされている。
ビルド時に「--modules-path」で設定した場所。
nginxのビルドオプションを確認。
# nginx -V


Nginxに動的モジュールを設定

動的モジュールを読み込みテスト。
# cd /etc/nginx/
# less nginx.conf

load_module modules/ngx_http_sxg_filter_module.so;

# nginx -t

エラー

nginx: [emerg] module "/etc/nginx/modules/ngx_http_sxg_filter_module.so" is not binary compatible in /etc/nginx/nginx.conf:1
nginx: configuration file /etc/nginx/nginx.conf test failed

nginxのビルドオプションをインストール済みのに全部合わせてみた。
「nginx -V」で出力したのに「--add-dynamic-module」だけ追加。

# cd /opt/software/nginx-1.20.2/
# nginx -V
# ./configure --prefix=/etc/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 --pid-path=/var/run/nginx.pid --lock-path=/var/run/nginx.lock --http-client-body-temp-path=/var/cache/nginx/client_temp --http-proxy-temp-path=/var/cache/nginx/proxy_temp --http-fastcgi-temp-path=/var/cache/nginx/fastcgi_temp --http-uwsgi-temp-path=/var/cache/nginx/uwsgi_temp --http-scgi-temp-path=/var/cache/nginx/scgi_temp --user=nginx --group=nginx --with-compat --with-file-aio --with-threads --with-http_addition_module --with-http_auth_request_module --with-http_dav_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-mail --with-mail_ssl_module --with-stream --with-stream_realip_module --with-stream_ssl_module --with-stream_ssl_preread_module --with-cc-opt='-O2 -g -pipe -Wall -Werror=format-security -Wp,-D_FORTIFY_SOURCE=2 -Wp,-D_GLIBCXX_ASSERTIONS -fexceptions -fstack-protector-strong -grecord-gcc-switches -specs=/usr/lib/rpm/redhat/redhat-hardened-cc1 -specs=/usr/lib/rpm/redhat/redhat-annobin-cc1 -m64 -mtune=generic -fasynchronous-unwind-tables -fstack-clash-protection -fcf-protection -fPIC' --with-ld-opt='-Wl,-z,relro -Wl,-z,now -pie' --add-dynamic-module=../nginx-sxg-module

エラー

./configure: error: the HTTP rewrite module requires the PCRE library.
You can either disable the module by using --without-http_rewrite_module

PCRE libraryをインストール。
# dnf search pcre
# dnf install pcre-devel

さっきと同じconfigureコマンドを実行。
モジュールを生成してコピー。
# make modules
# cp objs/ngx_http_sxg_filter_module.so /etc/nginx/modules/

nginxテスト
# nginx -t

nginx-sxg-module動的モジュールのインストール完了。
nginx再起動。
# systemctl restart nginx


Signed HTTP Exchanges 証明書を取得

現時点ではDigiCertしかSXG対応の証明書を発行していない(Let's Encryptで取得できない)。

スタンダード・サーバID: 55,000円/年(4,583円/月)

実際に証明書取得して運用するかは考え中。

Cloudflareでいいのかもしれない。
(CDNだけなら無料)


【関連記事】