echo-nginx-module

最後更新: 2024-09-25

目錄

Homepage: https://github.com/openresty/echo-nginx-module

用圖

  1. serve static contents directly from memory (loading from the Nginx config file).
  2. merge contents of various "Nginx locations" (i.e., subrequests) together in a single main request
    (using echo_location and its friends).

echo-nginx-module

 

U22

apt install nginx-full

Or

apt install libnginx-mod-http-echo

Compile

# Version: Ubuntu-16.04, nginx-1.15.12, echo-nginx-module-0.61

mkdir /usr/src/nginx; cd /usr/src/nginx;

wget https://github.com/openresty/echo-nginx-module/archive/v0.61.zip

unzip v0.61.zip

cd nginx-1.15.12

./configure --prefix=/opt/nginx \
 ... \                   # setting must same as first build
 --add-dynamic-module=/usr/src/nginx/echo-nginx-module-0.61

make

cp ./objs/ngx_http_echo_module.so /opt/nginx/modules/

 


Settings

 

nginx.conf

load_module "modules/ngx_http_echo_module.so";
...

 


Syntax

 

  • echo
  • echo_duplicate
  • echo_sleep & echo_flush
  • echo_location_async
  • echo_location
  • echo_subrequest & echo_subrequest_async
  • echo_exec

 

echo [Opts] <string>

-n    不換行

echo_duplicate

        可以用來畫水平線

echo_duplicate 8 "=";
echo -n " END ";
echo_duplicate 8 "=";
echo;

echo_sleep & echo_flush

e.g.

echo $remote_addr;
echo_flush;
echo_sleep 3;
echo $geoip2_data_country_code;

echo_sleep

This operation is non-blocking on server side,
so unlike the echo_blocking_sleep directive,
it won't block the whole Nginx worker process.

never use echo_blocking_sleep in production environment
(原因: will block the current Nginx worker process completely)

echo_flush

ensure the client can see previous output immediately

echo_location_async

syntax: echo_location_async <location> [<url_args>]

Issue GET subrequest to the location specified (first argument) with optional url arguments specified in the second argument.

當多個 echo_location_async 時它們是 in parallel 的

 * 不支援 named locations like (@foo)

location /main {
     echo_location_async /sub 'foo=Foo&bar=Bar';
}
location /sub {
     echo $arg_foo $arg_bar;
}

echo_location

Just like the echo_location_async directive, but echo_location issues subrequests in series rather than in parallel.

echo_subrequest & echo_subrequest_async

* 它比 echo_location 及 echo_subrequest_async , 但支持 POST & GET

syntax: echo_subrequest <HTTP_method> <location> [-q <url_args>] [-b <request_body>] [-f <request_body_path>]

echo_exec

 * 支持 Named locations (query string will always be ignored)
 * Never try to echo things before the echo_exec directive

e.g.

location = /t {
    echo_exec @test;
    }
location @test {
    echo "test";
    }

 


Example

 

Example 1

# echo client ip

location = / {
    default_type 'text/html';
    add_header expires "0";
    echo $remote_addr;
}

location / { try_files $uri $uri/ =404; }
  • 必須加 default_type, 否則成了 download

Example 2

sites-enabled/proxy_www

server {
  listen 8888;
  ...

  # Example 1
  location = /404 { add_header Content-Type text/html; echo "404"; }

  # Example 2
  location ~ ^/r/([0-9a-zA-Z]+) { echo $1;}

  # Example 3
  location /testing {
    set $message "Hello Buddy";
    # newlines (\n) and tabs (\t)
    echo "Testing this amazing Feature: $message";
  }
}