Echo#

O módulo adiciona echo, sleep, time, exec e outras funções no estilo shell.

Instalação#

Para instalar o módulo, use um dos seguintes pacotes:

  • Angie: angie-module-echo

  • Angie PRO: angie-pro-module-echo

Carregando o Módulo#

Para trabalhar com o módulo, ele deve ser carregado no contexto main{}:

load_module modules/ngx_http_echo_module.so;

Exemplo de Configuração#

server {
    listen       80;
    server_name  localhost;

    location /echo {
        echo_before_body 'These lines are inserted';
        echo_before_body 'by the echo_before_body directive';
        echo_after_body 'These lines are added';
        echo_after_body 'by the echo_after_body directive';
        proxy_pass $scheme://127.0.0.1:$server_port$request_uri/more;
    }

    location /echo/more {
        set $val 'value';
        echo '======== Start backend answer =========';
        echo 'Backend answer body';
        echo "val is set on $val";
        echo '======== End backend answer ===========';
    }

    location /echo_with_sleep {
        echo hello;
        echo_flush;
        echo_sleep   2.5;
        echo world;
    }

    location /dup {
        echo_duplicate 3 "--";
        echo_duplicate 1 " END ";
        echo_duplicate 3 "--";
        echo;
    }

    location /subr {
        echo_reset_timer;
        echo_location /sub1;
        echo_location /sub2;
        echo "took $echo_timer_elapsed sec for total.";
    }

    location /subr_async {
        echo_reset_timer;
        echo_location_async /sub1;
        echo_location_async /sub2;
        echo "took $echo_timer_elapsed sec for total.";
    }

    location /sub1 {
        echo_sleep 2;
        echo hello;
    }

    location /sub2 {
        echo_sleep 1;
        echo world;
    }
}

Demonstração#

Vamos fazer algumas requisições para demonstrar a funcionalidade do módulo.

$ curl localhost/echo

  These lines are inserted
  by the echo_before_body directive
  ======== Start backend answer =========
  Backend answer body
  val is set on value
  ======== End backend answer ==========
  These lines are added
  by the echo_after_body directive
$ curl localhost/echo_with_sleep

  hello
  world

As strings "hello" e "world" aparecerão com um intervalo de 2,5 segundos.

$ curl localhost/dup
------ END ------
$ time curl localhost/subr

  hello
  world
  took 3.004 sec for total.

  real    0m3.027s
  user    0m0.015s
  sys     0m0.007s
$ time curl localhost/subr_async

  hello
  world
  took 0.000 sec for total.

  real    0m2.023s
  user    0m0.001s
  sys     0m0.020s

Informações Adicionais#

Documentação detalhada e código fonte estão disponíveis em: openresty/echo-nginx-module.