Nginx安装Nginx-echo模块

Nginx-echo可以在Nginx中用来输出一些信息,是在测试排错过程中一个比较好的工具。它也可以做到把来自不同链接地址的信息进行一个汇总输出。总之能用起来可以给开发人员带来挺大帮助的。下面看看我们如何去安装使用它。

1.下载Nginx-echo

cd /usr/local/nginx-1.12.2/
wget https://github.com/openresty/echo-nginx-module/archive/v0.60.tar.gz

2.解压下载来的文件

tar zxvf v0.60.tar.gz

3.查看一下之前安装的模块

/usr/local/nginx/sbin/nginx -V

4.进入以前下载过来的Nginx安装包目录下配置

cd /usr/local/nginx-1.12.2/
./configure --add-module=/usr/local/nginx-1.12.2/echo-nginx-module-0.61 --add-module=/usr/local/nginx-1.12.2/ngx_image_thumb-master

5.编译安装,如果是升级可以使用make upgrade

make && make install

6.使用echo,以下内容为官方的案例,加了一些自己的理解

#简单输出,看别人日志地址是/hello,我这里测试后不知道为什么一直访问不到,所以加上/
location /hello/ {
    echo "hello, world!";
}

#$echo_timer_elapsed计时,通过echo_reset_timer执行后开始计算
location /timed_hello/ {
    echo_reset_timer;
    echo hello world;
    echo "'hello world' takes about $echo_timer_elapsed sec.";
    echo hiya igor;
    echo "'hiya igor' takes about $echo_timer_elapsed sec.";
}

#echo_sleep可以等待2.5s后再输出
location /echo_with_sleep/ {
    echo hello;
    echo_flush;  # ensure the client can see previous output immediately
    echo_sleep  2.5;  # in sec
    echo world;
}

#在proxy_pass这个地址获得的源代码前后加上指定的内容
location /echo/ {
      echo_before_body hello;
      echo_before_body world;
      proxy_pass $scheme://127.0.0.1:$server_port$request_uri/more;
      echo_after_body hiya;
      echo_after_body igor;
}
 
#echo_location_async可以异步去访问其他地址,不影响当前函数执行,$echo_timer_elapsed最后输出的值为0
location /main/ {
      echo_reset_timer;
 
      # subrequests in parallel
      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;
}

#同步执行
location /main/ {
      echo_reset_timer;
 
      # subrequests in series (chained by CPS)
      echo_location /sub1/;
      echo_location /sub2/;
 
      echo "took $echo_timer_elapsed sec for total.";
}
location /sub1/ {
      echo_sleep 2;
      echo hello;
}
location /sub2/ {
      echo_sleep 1;
      echo world;
}

#echo_duplicate 3 "--" 意思是重复3次输出"--"
location /dup/ {
    echo_duplicate 3 "--";
    echo_duplicate 1 " END ";
    echo_duplicate 3 "--";
    echo;
}


#输出客户端请求的信息
location /echoback/ {
    echo_duplicate 1 $echo_client_request_headers;
    echo "\r";
 
    echo_read_request_body;
    echo_request_body;
}

#输出querystring、method、body、content等信息
location /multi {
    echo_subrequest_async POST '/sub' -q 'foo=Foo' -b 'hi';
    echo_subrequest_async PUT '/sub' -q 'bar=Bar' -b 'hello';
}
location /sub {
    echo "querystring: $query_string";
    echo "method: $echo_request_method";
    echo "body: $echo_request_body";
    echo "content length: $http_content_length";
    echo '///';
}

#可以将多个JS合并到一个URL地址输出
# GET /merge?/foo.js&/bar/blah.js&/yui/baz.js will merge the .js resources together
location /merge {
    default_type 'text/javascript';
    echo_foreach_split '&' $query_string;
    echo "/* JS File $echo_it */";
    echo_location_async $echo_it;
    echo;
    echo_end;
}

#如果地址为/if/?val=abcd,则输出hit
#参数名为val,参数值以a开头
location /if/ {
    set $res miss;
    if ($arg_val ~* '^a') {
        set $res hit;
        echo $res;
    }
    echo $res;
}

相关推荐