二進位編譯 NGINX 清除快取動態模塊 ngx_cache_purge for CentOS 8
二進位編譯 NGINX 清除快取模塊 ngx_cache_purge,透過 --add-dynamic-module 新增動態模塊,在結合 -with-compat 單獨編譯要新增的模塊,即可產生動態模塊 ngx_http_cache_purge_module.so 讓其它 NGINX 直接加載使用。
二進位編譯需用軟體
安裝需用軟體:
dnf install gcc-c++ pcre-devel zlib-devel make unzip libuuid-devel -y
ngx_cache_purge 模塊
下載 ngx_cache_purge 並解壓縮:
wget http://labs.frickle.com/files/ngx_cache_purge-2.3.tar.gz
tar zxf ngx_cache_purge-2.3.tar.gz
ngx_cache_purge 預設無法編譯成 NGINX 的動態模塊,所幸有人提交了更新程式 ngx_cache_purge/config at master · nginx-modules/ngx_cache_purge · GitHub,開啟 config 並覆蓋成以下內容:
vim ngx_cache_purge-2.3/config
if [ "$HTTP_PROXY" = "YES" ]; then
have=NGX_HTTP_PROXY . auto/have
fi
if [ "$HTTP_FASTCGI" = "YES" ]; then
have=NGX_HTTP_FASTCGI . auto/have
fi
if [ "$HTTP_SCGI" = "YES" ]; then
have=NGX_HTTP_SCGI . auto/have
fi
if [ "$HTTP_UWSGI" = "YES" ]; then
have=NGX_HTTP_UWSGI . auto/have
fi
ngx_addon_name=ngx_http_cache_purge_module
CACHE_PURGE_SRCS="$ngx_addon_dir/ngx_cache_purge_module.c"
if [ -n "$ngx_module_link" ]; then
ngx_module_type=HTTP
ngx_module_name="$ngx_addon_name"
ngx_module_srcs="$CACHE_PURGE_SRCS"
. auto/module
else
HTTP_MODULES="$HTTP_MODULES $ngx_addon_name"
NGX_ADDON_SRCS="$NGX_ADDON_SRCS $CACHE_PURGE_SRCS"
fi
have=NGX_CACHE_PURGE_MODULE . auto/have
NGINX
下載二進位檔案
下載 NGINX 二進位檔案並解壓縮,本例為 NGINX 1.16.1 版本 (其它版本可參考 nginx: download):
wget http://nginx.org/download/nginx-1.16.1.tar.gz
tar zxf nginx-1.16.1.tar.gz
進入 nginx 檔案目錄:
cd nginx-1.16.1
編譯配置
設定 NGINX 編譯配置:
- --add-dynamic-module:新增動態模塊 (為要產生動態模塊的模塊檔案路徑)。
- --with-compat (nginx 1.11.5 新增的指令):單獨編譯要新增的模塊,原有的 NGINX 即可直接動態加載,而不用重新編譯。
./configure --add-dynamic-module=/root/ngx_cache_purge-2.3 --with-compat
adding module in /root/ngx_cache_purge-2.3 # 此模塊編譯完成的名稱會是這樣 + ngx_http_cache_purge_module 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
指令,則 NGINX 也會一同編譯 (系統如果已有安裝 NGINX 會有問題)make modules
make -f objs/Makefile modules make[1]: Entering directory '/root/nginx-1.16.1' cc -c -fPIC -pipe -O -W -Wall -Wpointer-arith -Wno-unused-parameter -Werror -g -I src/core -I src/event -I src/event/modules -I src/os/unix -I objs -I src/http -I src/http/modules \ -o objs/addon/ngx_cache_purge-2.3/ngx_cache_purge_module.o \ /root/ngx_cache_purge-2.3/ngx_cache_purge_module.c cc -c -fPIC -pipe -O -W -Wall -Wpointer-arith -Wno-unused-parameter -Werror -g -I src/core -I src/event -I src/event/modules -I src/os/unix -I objs -I src/http -I src/http/modules \ -o objs/ngx_http_cache_purge_module_modules.o \ objs/ngx_http_cache_purge_module_modules.c # 產生的動態模塊路徑 cc -o objs/ngx_http_cache_purge_module.so \ objs/addon/ngx_cache_purge-2.3/ngx_cache_purge_module.o \ objs/ngx_http_cache_purge_module_modules.o \ -shared make[1]: Leaving directory '/root/nginx-1.16.1'
產生的 ngx_cache_purge 動態模塊為 ngx_http_cache_purge_module.so:
ll objs/
總計 232
drwxr-xr-x. 3 root root 33 4月 8 12:56 addon
-rw-r--r--. 1 root root 17385 4月 8 12:56 autoconf.err
-rw-r--r--. 1 root root 41504 4月 8 12:56 Makefile
-rw-r--r--. 1 root root 7828 4月 8 12:56 ngx_auto_config.h
-rw-r--r--. 1 root root 657 4月 8 12:56 ngx_auto_headers.h
-rw-r--r--. 1 root root 297 4月 8 12:56 ngx_http_cache_purge_module_modules.c
-rw-r--r--. 1 root root 36040 4月 8 12:56 ngx_http_cache_purge_module_modules.o
-rwxr-xr-x. 1 root root 108176 4月 8 12:56 ngx_http_cache_purge_module.so
-rw-r--r--. 1 root root 5856 4月 8 12:56 ngx_modules.c
drwxr-xr-x. 9 root root 91 4月 8 12:56 src
複製模塊至 NGINX 模塊路徑
列出 NGINX 設定來查看模塊路徑:
nginx -V
nginx version: nginx/1.16.1 built by gcc 8.2.1 20180905 (Red Hat 8.2.1-3) (GCC) built with OpenSSL 1.1.1 FIPS 11 Sep 2018 (running with OpenSSL 1.1.1c FIPS 28 May 2019) TLS SNI support enabled configure arguments: --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 # ... 後續省略 ...
--prefix
路徑內也有一個 modules 目錄,它其實就是連結到 --modules-path
模塊路徑:
ll /etc/nginx/modules
lrwxrwxrwx. 1 root root 29 8月 13 2019 /etc/nginx/modules -> ../../usr/lib64/nginx/modules
將動態模塊 ngx_http_cache_purge_module.so 複製至 NGINX 模塊路徑:
cp objs/ngx_http_cache_purge_module.so /usr/lib64/nginx/modules/
刪除下載與編譯的所有檔案:
cd ~
rm -rf ng*
SELinux 設定
設定 SELinux 允許 httpd (NGINX) 執行模塊:
setsebool -P httpd_execmem 1
設定檔配置
請參考 NGINX 設定有出現 FastCGI Cache 文字的設定。
參考
本著作係採用創用 CC 姓名標示-相同方式分享 3.0 台灣 授權條款授權.