欢迎您,零度科技专业海外服务器租用
公司电话: 24小时电话: 

当前位置:首页>帮助教程>Linux 系统教程

目录导航

Nginx安装/使用echo 模块echo-nginx-module

美国KT机房 添加于 2014-09-05  
Linux 系统教程
Nginx 有个 echo 模块可以用来输出一些简单的信息,例如:
  location /hello {
    echo "hello, world!";
  }

  location /hello {
    echo -n "hello, "
    echo "world!";
  }

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

  location /echo_with_sleep {
    echo hello;
    echo_flush;  # ensure the client can see previous output immediately
    echo_sleep   2.5;  # in sec
    echo world;
  }

  # in the following example, accessing /echo yields
  #   hello
  #   world
  #   blah
  #   hiya
  #   igor
  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;
  }
  location /echo/more {
      echo blah;
  }

  # the output of /main might be
  #   hello
  #   world
  #   took 0.000 sec for total.
  # and the whole request would take about 2 sec to complete.
  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;
  }

  # the output of /main might be
  #   hello
  #   world
  #   took 3.003 sec for total.
  # and the whole request would take about 3 sec to complete.
  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;
  }

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

  # /bighello will generate 1000,000,000 hello's.
  location /bighello {
    echo_duplicate 1000_000_000 'hello';
  }

  # echo back the client request
  location /echoback {
    echo_duplicate 1 $echo_client_request_headers;
    echo "\r";
 
    echo_read_request_body;
 
    echo_request_body;
  }

  # GET /multi will yields
  #   querystring: foo=Foo
  #   method: POST
  #   body: hi
  #   content length: 2
  #   ///
  #   querystring: bar=Bar
  #   method: PUT
  #   body: hello
  #   content length: 5
  #   ///
  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 '///';
  }

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

  # accessing /if?val=abc yields the "hit" output
  # while /if?val=bcd yields "miss":
  location ^~ /if {
      set $res miss;
      if ($arg_val ~* '^a') {
          set $res hit;
          echo $res;
      }
      echo $res;
  }

这个模块不包含在 Nginx 源码中,安装方法:

1. 首先下载模块源码:https://github.com/agentzh/echo-nginx-module/tags
2. 解压到某个路径,假设为 /path/to/echo-nginx-module
3. 使用下面命令编译并安装 Nginx

$ wget 'http://sysoev.ru/nginx/nginx-1.0.11.tar.gz'
$ tar -xzvf nginx-1.0.11.tar.gz
$ cd nginx-1.0.11/

# Here we assume you would install you nginx under /opt/nginx/.
$ ./configure --prefix=/opt/nginx \
--add-module=/path/to/echo-nginx-module

$ make -j2
$ make install

试试看吧,该模块是国人 @章亦春 开发的。

原文来自零度科技:http://www.ldisp.com/a/linux/2014/echo-nginx-module.shtml

上一篇:CentOS如何禁用root本地或远程ssh登录
下一篇:Apache反向代理配置

 
了解零度?

零度科技主营全球服务器租用,如果您有服务器租用需求请联系

海外服务器租用

推荐香港、美国、韩国。提供站群服务器、高防服务器租用

海外特价服务器

关注零度特价服务器频道,了解全球特价服务器。

1

业务咨询

    技术服务

      特价活动
        全站搜索
        热门搜索:
        会员
        0通知
        客服
        0特价
        搜索
        TOP
        香港高防服务器,远程桌面,韩国服务器,nginx,反向代理