在上一篇的文章中, 學習了該如何編譯 Nginx 原始碼還有安裝相關的依賴。

今天就延續昨天的進度來學習如何在 Nginx 中安裝第三方的模組。

首先也是安裝相關依賴 pcre2 zlib openssl

apk add pcre2-dev
apk add zlib-dev
apk add openssl

這邊我們選擇下載 echo-nginx-module 這個第三方模組。

wget -P ~ https://github.com/openresty/echo-nginx-module/archive/refs/tags/v0.63.tar.gz
tar zxf v0.63.tar.gz

這個 echo-nginx-module 模組能夠提供一些常用的功能例如:

  1. echo: 直接回傳我們想要的字串,可以用來做 healthy check。
  2. sleep: 要求目前進程進行 non-blocking 睡眠。
  3. timer: 可以用來統計 Nginx 運行請求的秒數。
  4. exec: 可以用來進行轉跳的操作。

接下來下載 Nginx stable 版本原始碼。

wget -P ~ https://nginx.org/download/nginx-1.26.1.tar.gz
tar zxf nginx-1.26.1.tar.gz
cd ~/nginx-1.26.1

這邊的設定基本與上一篇相同,不過最後新增了 add-module 參數,這個步驟可以讓 Nginx 知道要將這個模組一起進行編譯。

./configure \
--prefix=/var/lib/nginx \
--modules-path=/usr/lib/nginx/modules \
--sbin-path=/usr/sbin/nginx \
--conf-path=/etc/nginx/nginx.conf \
--pid-path=/run/nginx/nginx.pid \
--lock-path=/run/nginx/nginx.lock \
--with-http_ssl_module \
--add-module=/root/echo-nginx-module-0.63

在設定過程的提示訊息中可以看到一段添加模組訊息。

configuring additional modules
adding module in /root/echo-nginx-module-0.63

最後也是使用 Makefile 進行安裝。

make && make install

這樣就成功將 echo-nginx-module 添加到 Nginx 內部了,現在來試試看模組內的功能。

vi /etc/nginx/nginx.conf

這裡我添加了四個 location 來測試這個模組的功能 echo echo_with_sleep echo_with_timer echo_with_exec

http {
    include       mime.types;
    default_type  application/octet-stream;

    sendfile        on;
    keepalive_timeout  65;

    server {
        listen       80;
        server_name  localhost;
        
        location / {
            root   html;
            index  index.html index.htm;
        }   
        
        location /echo {
            default_type text/plain;
            echo hello, world!$arg_text1$arg_text2;
        } 
        
        location /echo_with_sleep {
            echo hello;
            echo "sleep 3 second";
            echo_flush;
            echo_sleep 3;
            echo world;
        }
        
        location /echo_with_timer {
            echo_reset_timer;
            echo_sleep 3;  # in sec
            echo hello world;
            echo "'hello world' takes about $echo_timer_elapsed sec.";
        }
        
        location /echo_with_exec {
            echo_exec /hello text1=$arg_mytext1&text2=$arg_mytext2;
        }

        error_page   500 502 503 504  /50x.html;
        location = /50x.html {
            root   html;
        }   
    }
}

location /echo 使用起來與前幾天談設定變數的文章 裡面直接使用 return 200 $a; 意思是一樣的

curl "192.168.0.23/echo"
hello, world!

location /echo_with_sleep 這邊的 echo_flush 是能夠將目前 Buffer 裡面的內容直接強制輸出所以這裡我們才能直接先看到第一二行的內容, 並且等待三秒之後輸出第三行。

curl "192.168.0.23/echo_with_sleep"
hello
sleep 3 second
world

location /echo_with_timer 這邊的 echo_reset_timer 指令可以用來計時與重啟計時器,並且把時間保存在 $echo_timer_elapsed 變數裡面

curl "192.168.0.23/echo_with_timer"
hello world
'hello world' takes about 3.001 sec.

location /echo_with_exec 這邊的 echo_exec 可以用來進行轉跳工作,這裡我定義了兩個變數並轉跳給 echo

curl "192.168.0.23/echo_with_exec?mytext1=2024&mytext2=0807"
hello, world!20240807

Summary

今天學習了在編譯之前添加 Nginx 第三方的模組,這種方式雖然很直觀但是每次添加新模組不太方便, 所以還有另一種動態的安裝方式,之後會再來測試看看。