MIS 腳印 logo

MIS 腳印

記錄 IT 學習的軌跡

二進位編譯 NGINX 自動優化網站動態模塊 ngx_pagespeed for CentOS 8

二進位編譯 NGINX 自動優化網站模塊 ngx_pagespeed (Apache Software Foundation 研發),透過 --add-dynamic-module 新增動態模塊,在結合 -with-compat 單獨編譯要新增的模塊,即可產生動態模塊 ngx_pagespeed.so 讓其它 NGINX 直接加載使用。

CentOS

二進位編譯需用軟體

請參考安裝二進位編譯需用軟體

ngx_pagespeed 模塊

下載 ngx_pagespeed (參考 Build ngx_pagespeed From Source):

NPS_VERSION=1.13.35.2-stable  # 其它版本可參考 [PageSpeed Release Notes](https://www.modpagespeed.com/doc/release_notes)
cd
wget https://github.com/apache/incubator-pagespeed-ngx/archive/v${NPS_VERSION}.zip
unzip v${NPS_VERSION}.zip
nps_dir=$(find . -name "*pagespeed-ngx-${NPS_VERSION}" -type d)
cd "$nps_dir"
NPS_RELEASE_NUMBER=${NPS_VERSION/beta/}
NPS_RELEASE_NUMBER=${NPS_VERSION/stable/}
psol_url=https://dl.google.com/dl/page-speed/psol/${NPS_RELEASE_NUMBER}.tar.gz
[ -e scripts/format_binary_url.sh ] && psol_url=$(scripts/format_binary_url.sh PSOL_BINARY_URL)
wget ${psol_url}
tar -xzvf $(basename ${psol_url})  # extracts to psol/

NGINX

下載二進位檔案

下載 NGINX 二進位檔案並解壓縮,本例為 NGINX 1.16.1 版本 (參考 Build ngx_pagespeed From Source):

NGINX_VERSION=1.16.1  # 其它版本請可參考 [nginx: download](http://nginx.org/en/download.html)
cd
wget http://nginx.org/download/nginx-${NGINX_VERSION}.tar.gz
tar -xvzf nginx-${NGINX_VERSION}.tar.gz
cd nginx-${NGINX_VERSION}/

編譯配置

設定 NGINX 編譯配置:

  • --add-dynamic-module:新增動態模塊 (為要產生動態模塊的模塊檔案路徑)。
  • --with-compat (nginx 1.11.5 新增的指令):單獨編譯要新增的模塊,原有的 NGINX 即可直接動態加載,而不用重新編譯。
./configure --add-dynamic-module=$HOME/$nps_dir ${PS_NGX_EXTRA_FLAGS} --with-compat
# ... 以上省略 ...

configuring additional dynamic modules
adding module in /root/./incubator-pagespeed-ngx-1.13.35.2-stable
mod_pagespeed_dir=/root/./incubator-pagespeed-ngx-1.13.35.2-stable/psol/include
build_from_source=false
checking for psol ... found
List of modules (in reverse order of applicability):  ngx_http_write_filter_module ngx_http_header_filter_module ngx_http_chunked_filter_module ngx_http_range_header_filter_module ngx_http_gzip_filter_module ngx_http_postpone_filter_module ngx_http_ssi_filter_module ngx_http_charset_filter_module ngx_http_userid_filter_module ngx_http_headers_filter_module
checking for psol-compiler-compat ... found
# 此模塊編譯完成的名稱會是這樣
 + ngx_pagespeed was configured
checking for PCRE library ... found
checking for PCRE JIT support ... found
checking for zlib library ... found
creating objs/Makefile

Configuration summary
  + using system PCRE library
  + OpenSSL library is not used
  + using system zlib library

  nginx path prefix: "/usr/local/nginx"
  nginx binary file: "/usr/local/nginx/sbin/nginx"
  nginx modules path: "/usr/local/nginx/modules"
  nginx configuration prefix: "/usr/local/nginx/conf"
  nginx configuration file: "/usr/local/nginx/conf/nginx.conf"
  nginx pid file: "/usr/local/nginx/logs/nginx.pid"
  nginx error log file: "/usr/local/nginx/logs/error.log"
  nginx http access log file: "/usr/local/nginx/logs/access.log"
  nginx http client request body temporary files: "client_body_temp"
  nginx http proxy temporary files: "proxy_temp"
  nginx http fastcgi temporary files: "fastcgi_temp"
  nginx http uwsgi temporary files: "uwsgi_temp"
  nginx http scgi temporary files: "scgi_temp"

為了只編譯模塊,這裡加上 modules 指令 (要重新編譯需先執行指令 make clean):

make modules
# ... 以上省略 ...

# 產生的動態模塊路徑
cc -o objs/ngx_pagespeed.so \
objs/addon/src/log_message_handler.o \
objs/addon/src/ngx_base_fetch.o \
objs/addon/src/ngx_caching_headers.o \
objs/addon/src/ngx_event_connection.o \
objs/addon/src/ngx_fetch.o \
objs/addon/src/ngx_gzip_setter.o \
objs/addon/src/ngx_list_iterator.o \
objs/addon/src/ngx_message_handler.o \
objs/addon/src/ngx_pagespeed.o \
objs/addon/src/ngx_rewrite_driver_factory.o \
objs/addon/src/ngx_rewrite_options.o \
objs/addon/src/ngx_server_context.o \
objs/addon/src/ngx_url_async_fetcher.o \
objs/ngx_pagespeed_modules.o \
/root/./incubator-pagespeed-ngx-1.13.35.2-stable/psol/lib/Release/linux/x64/pagespeed_automatic.a -lstdc++ -lrt -pthread -lm -luuid \
-shared
make[1]: Leaving directory '/root/nginx-1.16.1'

產生的 ngx_pagespeed 動態模塊為 ngx_pagespeed.so:

ll objs/
總計 18880
drwxr-xr-x. 3 root root       17  4月 13 04:08 addon
-rw-r--r--. 1 root root    17523  4月 13 04:08 autoconf.err
-rw-r--r--. 1 root root    50395  4月 13 04:08 Makefile
-rw-r--r--. 1 root root     7599  4月 13 04:08 ngx_auto_config.h
-rw-r--r--. 1 root root      657  4月 13 04:08 ngx_auto_headers.h
-rw-r--r--. 1 root root     5856  4月 13 04:08 ngx_modules.c
-rw-r--r--. 1 root root      904  4月 13 04:08 ngx_pagespeed_modules.c
-rw-r--r--. 1 root root    37152  4月 13 04:09 ngx_pagespeed_modules.o
-rwxr-xr-x. 1 root root 19191608  4月 13 04:09 ngx_pagespeed.so
drwxr-xr-x. 9 root root       91  4月 13 04:08 src

複製模塊至 NGINX 模塊路徑

請參考複製模塊至 NGINX 模塊路徑

SELinux 設定

請參考 SELinux 設定

設定檔配置

請參考 NGINX 設定

參考

發表迴響