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;
}

 


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM