Nginx安裝Nginx-echo模塊

Nginx-echo能夠在Nginx中用來輸出一些信息,是在測試排錯過程當中一個比較好的工具。它也能夠作到把來自不一樣連接地址的信息進行一個彙總輸出。總之能用起來能夠給開發人員帶來挺大幫助的。下面看看咱們如何去安裝使用它。

1.下載Nginx-echojavascript

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

 
2.解壓下載來的文件java

tar zxvf v0.60.tar.gz


3.查看一下以前安裝的模塊nginx

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


4.進入之前下載過來的Nginx安裝包目錄下配置git

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 github

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;
}
相關文章
相關標籤/搜索