forked from openresty/memc-nginx-module
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathREADME
573 lines (429 loc) · 18.9 KB
/
README
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
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
Name
memc-nginx-module - An extended version of the standard memcached module
that supports set, add, delete, and many more memcached commands.
*This module is not distributed with the Nginx source.* See the
installation instructions.
Version
This document describes memc-nginx-module v0.08
(<http://github.com/agentzh/memc-nginx-module/downloads >) released on
April 10, 2010.
Synopsis
# GET /foo?key=dog
#
# POST /foo?key=cat
# Cat's value...
#
# PUT /foo?key=bird
# Bird's value...
#
# DELETE /foo?key=Tiger
location /foo {
set $memc_key $arg_key;
# $memc_cmd defaults to get for GET,
# add for POST, set for PUT, and
# delete for the DELETE request method.
memc_pass 127.0.0.1:11211;
}
# GET /bar?cmd=get&key=cat
#
# POST /bar?cmd=set&key=dog
# My value for the "dog" key...
#
# DELETE /bar?cmd=delete&key=dog
# GET /bar?cmd=delete&key=dog
location /bar {
set $memc_cmd $arg_cmd;
set $memc_key $arg_key;
set $memc_flags $arg_flags; # defaults to 0
set $memc_exptime $arg_exptime; # defaults to 0
memc_pass 127.0.0.1:11211;
}
# GET /bar?cmd=get&key=cat
# GET /bar?cmd=set&key=dog&val=animal&flags=1234&exptime=2
# GET /bar?cmd=delete&key=dog
# GET /bar?cmd=flush_all
location /bar {
set $memc_cmd $arg_cmd;
set $memc_key $arg_key;
set $memc_value $arg_val;
set $memc_flags $arg_flags; # defaults to 0
set $memc_exptime $arg_exptime; # defaults to 0
memc_cmds_allowed get set add delete flush_all;
memc_pass 127.0.0.1:11211;
}
http {
...
upstream backend {
server 127.0.0.1:11984;
server 127.0.0.1:11985;
}
server {
location /stats {
set $memc_cmd stats;
memc_pass backend;
}
...
}
}
...
# read the memcached flags into the Last-Modified header
# to respond 304 to conditional GET
location /memc {
set $memc_key $arg_key;
memc_pass 127.0.0.1:11984;
memc_flags_to_last_modified on;
}
Description
This module extends the standard memcached module to support almost the
whole memcached TCP protocol
(<http://code.sixapart.com/svn/memcached/trunk/server/doc/protocol.txt >)
.
It allows you to define a custom REST
(<http://en.wikipedia.org/wiki/REST >) interface to your memcached
servers or access memcached in a very efficient way from within the
nginx server by means of subrequests or independent fake requests
(<http://github.com/srlindsay/nginx-independent-subrequest >).
This module is not supposed to be merged into the Nginx core because
I've used Ragel (<http://www.complang.org/ragel/ >) to generate the
memcached response parsers (in C) for joy :)
Keep-alive connections to memcached servers
You need Maxim Dounin's ngx_upstream_keepalive module
(<http://mdounin.ru/hg/ngx_http_upstream_keepalive/ >) together with this
module for keep-alive TCP connections to your backend memcached servers.
Here's a sample configuration:
http {
upstream backend {
server 127.0.0.1:11211;
# a pool with at most 1024 connections
# and do not distinguish the servers:
keepalive 1024 single;
}
server {
...
location /memc {
set $memc_cmd get;
set $memc_key $arg_key;
memc_pass backend;
}
}
}
How it works
It implements the memcached TCP protocol all by itself, based upon the
"upstream" mechansim. Everything involving I/O is non-blocking.
The module itself does not keep TCP connections to the upstream
memcached servers across requests, just like other upstream modules. For
a working solution, see section Keep-alive connections to memcached
servers.
Memcached commands supported
The memcached storage commands set, add, replace, prepend, and append
uses the $memc_key as the key, $memc_exptime as the expiration time (or
delay) (defaults to 0), $memc_flags as the flags (defaults to 0), to
build the corresponding memcached queries.
If $memc_value is not defined at all, then the request body will be used
as the value of the $memc_value except for the incr and decr commands.
Note that if $memc_value is defined as an empty string (""), that empty
string will still be used as the value as is.
The following memcached commands have been implemented and tested (with
their parameters marked by corresponding nginx variables defined by this
module):
get $memc_key
Retrieves the value using a key.
location /foo {
set $memc_cmd 'get';
set $memc_key 'my_key';
memc_pass 127.0.0.1:11211;
add_header X-Memc-Flags $memc_flags;
}
Returns "200 OK" with the value put into the response body if the key is
found, or "404 Not Found" otherwise. The "flags" number will be set into
the $memc_flags variable so it's often desired to put that info into the
response headers by means of the standard add_header directive.
It returns 502 for "ERROR", "CLIENT_ERROR", or "SERVER_ERROR".
set $memc_key $memc_flags $memc_exptime $memc_value
To use the request body as the memcached value, just avoid setting the
$memc_value variable:
# POST /foo
# my value...
location /foo {
set $memc_cmd 'set';
set $memc_key 'my_key';
set $memc_flags 12345;
set $memc_exptime 24;
memc_pass 127.0.0.1:11211;
}
Or let the $memc_value hold the value:
location /foo {
set $memc_cmd 'set';
set $memc_key 'my_key';
set $memc_flags 12345;
set $memc_exptime 24;
set $memc_value 'my_value';
memc_pass 127.0.0.1:11211;
}
Returns "201 Created" if the upstream memcached server replies "STORED",
200 for "NOT_STORED", 404 for "NOT_FOUND", 502 for "ERROR",
"CLIENT_ERROR", or "SERVER_ERROR".
The original memcached responses are returned as the response body
except for "404 NOT FOUND".
add $memc_key $memc_flags $memc_exptime $memc_value
Similar to the set command.
replace $memc_key $memc_flags $memc_exptime $memc_value
Similar to the set command.
append $memc_key $memc_flags $memc_exptime $memc_value
Similar to the set command.
Note that at least memcached version 1.2.2 does not support the "append"
and "prepend" commands. At least 1.2.4 and later versions seem to
supports these two commands.
prepend $memc_key $memc_flags $memc_exptime $memc_value
Similar to the append command.
delete $memc_key
Deletes the memcached entry using a key.
location /foo
set $memc_cmd delete;
set $memc_key my_key;
memc_pass 127.0.0.1:11211;
}
Returns "200 OK" if deleted successfully, "404 Not Found" for
"NOT_FOUND", or 502 for "ERROR", "CLIENT_ERROR", or "SERVER_ERROR".
The original memcached responses are returned as the response body
except for "404 NOT FOUND".
delete $memc_key $memc_exptime
Similar to the delete $memc_key command except it accepts an optional
"expiration" time specified by the $memc_exptime variable.
This command is no longer available in the latest memcached version
1.4.4.
incr $memc_key $memc_value
Increments the existing value of $memc_key by the amount specified by
$memc_value:
location /foo {
set $memc_key my_key;
set $memc_value 2;
memc_pass 127.0.0.1:11211;
}
In the preceding example, every time we access "/foo" will cause the
value of "my_key" increments by 2.
Returns "200 OK" with the new value associated with that key as the
response body if successful, or "404 Not Found" if the key is not found.
It returns 502 for "ERROR", "CLIENT_ERROR", or "SERVER_ERROR".
decr $memc_key $memc_value
Similar to incr $memc_key $memc_value.
flush_all
Mark all the keys on the memcached server as expired:
location /foo {
set $memc_cmd flush_all;
memc_pass 127.0.0.1:11211;
}
flush_all $memc_exptime
Just like flush_all but also accepts an expiration time specified by the
$memc_exptime variable.
stats
Causes the memcached server to output general-purpose statistics and
settings
location /foo {
set $memc_cmd stats;
memc_pass 127.0.0.1:11211;
}
Returns "200 OK" if the request succeeds, or 502 for "ERROR",
"CLIENT_ERROR", or "SERVER_ERROR".
The raw "stats" command output from the upstream memcached server will
be put into the response body.
version
Queries the memcached server's version number:
location /foo {
set $memc_cmd version;
memc_pass 127.0.0.1:11211;
}
Returns "200 OK" if the request succeeds, or 502 for "ERROR",
"CLIENT_ERROR", or "SERVER_ERROR".
The raw "version" command output from the upstream memcached server will
be put into the response body.
Directives
All the standard memcached module directives in nginx 0.8.28 are
directly inherited, with the "memcached_" prefixes replaced by "memc_".
For example, the "memcached_pass" directive is spelled "memc_pass".
Consult the [[NginxHttpMemcachedModule]] documentation for more details.
Here we only document the most important two directives (the latter is a
new directive introduced by this module).
memc_pass
syntax: *memc_pass <memcached server IP address>:<memcached server
port>*
syntax (2): *memc_pass <memcached server hostname>:<memcached server
port>*
syntax (3): *memc_pass <upstream_backend_name>*
default: *none*
context: *http, server, location, if*
Specify the memcached server backend.
memc_cmds_allowed
syntax: *memc_cmds_allowed <cmd>...*
default: *none*
context: *http, server, location, if*
Lists memcached commands that are allowed to access. By default, all the
memcached commands supported by this module are accessible. An example
is
location /foo {
set $memc_cmd $arg_cmd;
set $memc_key $arg_key;
set $memc_value $arg_val;
memc_pass 127.0.0.1:11211;
memc_cmds_allowed get;
}
memc_flags_to_last_modified
syntax: *memc_flags_to_last_modified on|off*
default: *off*
context: *http, server, location, if*
Read the memcached flags as epoch seconds and set it as the value of the
"Last-Modified" header. For conditional GET, it will signal nginx to
return "304 Not Modified" response to save bandwidth.
Installation
Grab the nginx source code from nginx.net (<http://nginx.net/ >), for
example, the version 0.8.29 (see nginx compatibility), and then build
the source with this module:
$ wget 'http://sysoev.ru/nginx/nginx-0.8.29.tar.gz'
$ tar -xzvf nginx-0.8.29.tar.gz
$ cd nginx-0.8.29/
# Here we assume you would install you nginx under /opt/nginx/.
$ ./configure --prefix=/opt/nginx \
--add-module=/path/to/memc-nginx-module
$ make -j2
$ make install
Download the latest version of the release tarball of this module from
memc-nginx-module file list
(<http://github.com/agentzh/memc-nginx-module/downloads >).
For Developers
The memached response parsers were generated by Ragel
(<http://www.complang.org/ragel/ >). If you want to regenerate the
parser's C file, i.e., src/ngx_http_memc_response.c
(<http://github.com/agentzh/memc-nginx-module/blob/master/src/ngx_http_m
emc_response.c>), use the following command from the root of the memc
module's source tree:
$ ragel -G2 src/ngx_http_memc_response.rl
Compatibility
The following versions of Nginx should work with this module:
* 0.8.x (last tested version is 0.8.38)
* 0.7.x >= 0.7.46 (last tested version is 0.7.65)
It's worth mentioning that some 0.7.x versions older than 0.7.46 might
also work, but I can't easily test them because the test suite makes
extensive use of the echo module's echo_location directive, which
requires at least nginx 0.7.46 :)
Earlier versions of Nginx like 0.6.x and 0.5.x will *not* work.
If you find that any particular version of Nginx above 0.7.46 does not
work with this module, please consider reporting a bug.
Report Bugs
Although a lot of effort has been put into testing and code tuning,
there must be some serious bugs lurking somewhere in this module. So
whenever you are bitten by any quirks, please don't hesitate to
1. send a bug report or even patches to <[email protected]>,
2. or create a ticket on the issue tracking interface
(<http://github.com/agentzh/memc-nginx-module/issues >) provided by
GitHub.
Source Repository
Available on github at agentzh/memc-nginx-module
(<http://github.com/agentzh/memc-nginx-module >).
ChangeLog
v0.08
* now the memc commands other than get work with subrequests in
memory. Thanks Yao Xinming for reporting it. Using storage memcached
commands in ngx_eval module's eval blocks no longer hang the server.
v0.07
* applied the patch from nginx 0.8.35 that fixed a bug that ngx_eval
may issue the incorrect error message "memcached sent invalid
trailer".
v0.06
* implemented the memc_flags_to_last_modified directive.
* added a new variable named $memc_flags_as_http_time.
v0.05
* removed the "memc_bind" directive since it won't compile with nginx
0.8.31.
v0.04
* to ensure Maxim's ngx_http_upstream_keepalive
(<http://mdounin.ru/hg/ngx_http_upstream_keepalive/ >) module caches
our connections even if "u->headers_in->status" is 201 (Created).
* updated docs to make it clear that this module can work with
"upstream" multi-server backends. thanks Bernd Dorn for reporting
it.
v0.03
* fixed a connection leak caused by an extra "r->main->count++"
operation: we should NOT do "r->main->count++" after calling the
"ngx_http_read_client_request_body" function in our content handler.
v0.02
* applied the (minor) optimization trick suggested by Marcus Clyne:
creating our variables and save their indexes at post-config phase
when the memc_pass directive is actually used in the config file.
v0.01
* initial release.
Test Suite
This module comes with a Perl-driven test suite. The test cases
(<http://github.com/agentzh/memc-nginx-module/tree/master/test/t/ >) are
declarative
(<http://github.com/agentzh/memc-nginx-module/blob/master/test/t/storage
.t>) too. Thanks to the Test::Base
(<http://search.cpan.org/perldoc?Test::Base >) module in the Perl world.
To run it on your side:
$ cd test
$ PATH=/path/to/your/nginx-with-memc-module:$PATH prove -r t
You need to terminate any Nginx processes before running the test suite
if you have changed the Nginx server binary.
Either LWP::UserAgent (<http://search.cpan.org/perldoc?LWP::UserAgent >)
or IO::Socket (<http://search.cpan.org/perldoc?IO::Socket >) is used by
the test scaffold
(<http://github.com/agentzh/memc-nginx-module/blob/master/test/lib/Test/
Nginx/LWP.pm>).
Because a single nginx server (by default, "localhost:1984") is used
across all the test scripts (".t" files), it's meaningless to run the
test suite in parallel by specifying "-jN" when invoking the "prove"
utility.
You should also keep a memcached server listening on the 11984 port at
localhost before running the test suite.
Some parts of the test suite requires modules rewrite and echo to be
enabled as well when building Nginx.
TODO
* add support for the memcached commands "cas", "gets" and "stats
$memc_value".
* add support for the "noreply" option.
Getting involved
You'll be very welcomed to submit patches to the author or just ask for
a commit bit to the source repository on GitHub.
Author
agentzh (章亦春) *<[email protected]>*
This wiki page is also maintained by the author himself, and everybody
is encouraged to improve this page as well.
Copyright & License
The code base is borrowed directly from the standard memcached module in
the Nginx 0.8.28 core. This part of code is copyrighted by Igor Sysoev.
Copyright (c) 2009, Taobao Inc., Alibaba Group ( http://www.taobao.com
).
Copyright (c) 2009, agentzh <[email protected]>.
This module is licensed under the terms of the BSD license.
Redistribution and use in source and binary forms, with or without
modification, are permitted provided that the following conditions are
met:
* Redistributions of source code must retain the above copyright
notice, this list of conditions and the following disclaimer.
* Redistributions in binary form must reproduce the above copyright
notice, this list of conditions and the following disclaimer in the
documentation and/or other materials provided with the distribution.
* Neither the name of the Taobao Inc. nor the names of its
contributors may be used to endorse or promote products derived from
this software without specific prior written permission.
THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS
IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A
PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED
TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
See Also
* The original announcement email on the nginx mailing list: ngx_memc:
"an extended version of ngx_memcached that supports set, add,
delete, and many more commands"
(<http://forum.nginx.org/read.php?2,28359 >)
* The latest memcached TCP protocol
(<http://code.sixapart.com/svn/memcached/trunk/server/doc/protocol.t
xt>).
* The standard memcached module.
* The echo module for Nginx module's automated testing.
* The standard headers module and the 3rd-parth headers-more module.