openresty / echo-nginx-module

An Nginx module for bringing the power of "echo", "sleep", "time" and more to Nginx's config file
http://wiki.nginx.org/NginxHttpEchoModule
BSD 2-Clause "Simplified" License
1.17k stars 254 forks source link

use just the echo_sleep and then proceed as normal? #71

Closed fommil closed 7 years ago

fommil commented 7 years ago

Hi, I'm trying to use echo_sleep as a way to mock out some services with known worst case latency. My docker build is here https://github.com/fommil/docker-nginx-echo which seems to work fine.

However, this kind of config always returns empty contents

     location / {
        echo_sleep 0.1;
        root   /to/my/data;
     }

(obvs works without the echo_sleep, but no delay). I tried a hack of

    location / {
        echo_sleep 0.1;
        echo_location /raw/$query_string ;
    }

    location /raw {
        root   /to/my/data;
    }

but also not doing what I expect. Is there a way to just get the echo_sleep feature?

fommil commented 7 years ago

ok, this seems to be working for me

    location / {
        echo_sleep 0.1;
        echo_exec @raw;
    }

    location @raw {
        root   /to/my/data;
    }

so I change my question to... is this how I'm supposed to do it?

agentzh commented 7 years ago

@fommil Both echo_sleep and root register a different "content handler" for that location. And in nginx, only one content handler is allowed in a single location. Use of ngx_echo's own directives work since they register the same single content handler of ngx_echo. This is nginx basics. To work around that, you should use the ngx_lua module instead, as in

location / {
    access_by_lua_block { ngx.sleep(0.1) }
    root /to/my/data;
}