Nginx 的 Echo 模块 —— echo-nginx-module,转

Nginx 有个 echo 模块可以用来输出一些简单的信息,例如:

?

1

2

3

4

5

6

7

8

9

10

11

12

13

14

15

16

17

18

19

20

21

22

23

24

25

26

27

28

29

30

31

32

33

34

35

36

37

38

39

40

41

42

43

44

45

46

47

48

49

50

51

52

53

54

55

56

57

58

59

60

61

62

63

64

65

66

67

68

69

70

71

72

73

74

75

76

77

78

79

80

81

82

83

84

85

86

87

88

89

90

91

92

93

94

95

96

97

98

99

100

101

102

103

104

105

106

107

108

109

110

111

112

113

114

115

116

117

118

119

120

121

122

123

124

125

126

127

128

129

130

131

132

133

134

135

136

137

138

139

140

141

142

143

144

145

146

147

148

149

150

151

152

153

154

location/hello{

echo"hello, world!";

}

location/hello{

echo-n"hello, "

echo"world!";

}

location/timed_hello{

echo_reset_timer;

echohello world;

echo"'hello world' takes about $echo_timer_elapsed sec.";

echohiya igor;

echo"'hiya igor' takes about $echo_timer_elapsed sec.";

}

location/echo_with_sleep{

echohello;

echo_flush;# ensure the client can see previous output immediately

echo_sleep 2.5;# in sec

echoworld;

}

# 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{

echoblah;

}

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

echohello;

}

location/sub2{

echo_sleep 1;

echoworld;

}

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

echohello;

}

location/sub2{

echo_sleep 1;

echoworld;

}

# 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

?

1

2

3

4

5

6

7

8

9

10

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

$tar-xzvf nginx-1.0.11.tar.gz

$cdnginx-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

$makeinstall

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

http://www.oschina.net/question/12_45735?fromerr=jyurMByB