• Home
  • History
  • Annotate
Name Date Size #Lines LOC

..03-May-2022-

examples/H03-May-2022-1915

misc/H07-Aug-2016-227200

src/H03-May-2022-3,9202,607

test/H07-Aug-2016-1,3221,014

third_party/H07-Aug-2016-2,0141,065

.gitignoreH A D07-Aug-2016153 1615

.gitmodulesH A D07-Aug-2016205 76

AUTHORSH A D07-Aug-2016330 106

DockerfileH A D07-Aug-20164.5 KiB136127

LICENSEH A D07-Aug-20161.3 KiB3230

MakefileH A D07-Aug-20164.2 KiB13499

README.mdH A D07-Aug-201616 KiB625440

configH A D03-May-20221.6 KiB7459

docker-compose.ymlH A D07-Aug-20161.2 KiB4843

nginx.confH A D07-Aug-2016643 3318

nginx.vh.default.confH A D07-Aug-2016368 2112

valgrind.suppressH A D07-Aug-20161.9 KiB120119

README.md

1# Tarantool NginX upstream module
2---------------------------------
3Key features:
4* Both nginx and tarantool features accessible over HTTP(S).
5* Tarantool methods callable via JSON-RPC or REST.
6* Load balancing with elastic configuration.
7* Backup and fault tolerance.
8* Low overhead.
9
10Note: WebSockets are currently not supported until Tarantool supports out-of-band replies.
11
12About Tarantool: http://tarantool.org
13
14About upstream: http://nginx.org/en/docs/http/ngx_http_upstream_module.html#upstream
15
16## Status
17---------
18* v0.1.4 - Production ready.
19* v0.2.0 - Stable.
20* v0.2.1 - Stable.
21* v0.2.2 - Stable.
22
23## Content
24----------
25* [Compilation and install](#compilation-and-install)
26* [REST](#rest)
27* [JSON](#json)
28* [Directives](#directives)
29  * [tnt_pass](#tnt_pass)
30  * [tnt_http_methods](#tnt_http_methods)
31  * [tnt_http_rest_methods](#tnt_http_rest_methods)
32  * [tnt_pass_http_request](#tnt_pass_http_request)
33  * [tnt_pass_http_request_buffer_size](#tnt_pass_http_request_buffer_size)
34  * [tnt_method](#tnt_method)
35  * [tnt_http_allowed_methods - experemental](#tnt_http_allowed_methods)
36  * [tnt_send_timeout](#tnt_send_timeout)
37  * [tnt_read_timeout](#tnt_read_timeout)
38  * [tnt_buffer_size](#tnt_buffer_size)
39  * [tnt_next_upstream](#tnt_next_upstream)
40  * [tnt_connect_timeout](#tnt_connect_timeout)
41  * [tnt_next_upstream](#tnt_next_upstream)
42  * [tnt_next_upstream_tries](#tnt_next_upstream_tries)
43  * [tnt_next_upstream_timeout](#tnt_next_upstream_timeout)
44* [Performance tuning](#performance-tuning)
45* [Examples](#examples)
46* [Copyright & License](#copyright--license)
47* [See also](#see-also)
48
49## Compilation and install
50--------------------------
51
52### Build from source
53
54    $ git clone https://github.com/tarantool/nginx_upstream_module.git nginx_upstream_module
55    $ cd nginx_upstream_module
56    $ git submodule update --init --recursive
57    $ git clone https://github.com/nginx/nginx.git nginx
58    $ sudo apt-get install libpcre-dev zlib1-dev # install dependencies to build nginx
59    $ make build-all # or 'build-all-debug' for debug version
60
61[Back to content](#content)
62
63### Build module via nginx 'configure'
64
65  Requirements (for details, see REPO_ROOT/Makefile)
66
67    libyajl >= 2.0(https://lloyd.github.io/yajl/)
68    libmsgpuck >= 1.0 (https://github.com/rtsisyk/msgpuck)
69
70    $ ./configure --add-module=REPO_ROOT && make
71
72## Configuration
73
74```nginx
75    ## Typical configuration, for more see http://nginx.org/en/docs/http/ngx_http_upstream_module.html#upstream
76    upstream backend {
77        server 127.0.0.1:9999 max_fails=1 fail_timeout=30s;
78        server 127.0.0.1:10000;
79
80        # ...
81        server 127.0.0.1:10001 backup;
82
83        # ...
84    }
85
86    server {
87      location = /tnt {
88        tnt_pass backend;
89      }
90    }
91
92```
93
94[Back to content](#content)
95
96## REST
97-------
98
99  NOTE: since v0.2.0
100
101  With this module, you can call Tarantool stored procedures via HTTP REST methods (GET, POST, PUT, DELETE)
102
103  Example
104  ```nginx
105    upstream backend {
106      # Tarantool hosts
107      server 127.0.0.1:9999;
108    }
109
110    server {
111      # HTTP [GET | POST | PUT | DELETE] /tnt_rest?q=1&q=2&q=3
112      location /tnt_rest {
113        # REST mode on
114        tnt_http_rest_methods get post put delete; # or all
115
116        # Pass http headers and uri
117        tnt_pass_http_request on;
118
119        # Module on
120        tnt_pass backend;
121      }
122    }
123
124```
125
126```lua
127-- Tarantool procedure
128function tnt_rest(req)
129 req.headers -- http headers
130 req.uri -- uri
131 return { 'ok' }
132end
133
134```
135
136```bash
137 $> wget NGX_HOST/tnt_rest?arg1=1&argN=N
138```
139
140[Back to content](#content)
141
142## JSON
143-------
144
145  NOTE: since v0.1.4
146
147  The module expects JSON posted with HTTP POST or PUT (since v0.2.0) and carried in request body.
148
149  Server HTTP statuses
150
151    OK - response body contains a result or an error;
152         the error may appear only if something wrong happened within Tarantool,
153         for instance: 'method not found'.
154
155    INTERNAL SERVER ERROR - may appear in many cases,
156                            most of them being 'out of memory' error;
157
158    NOT ALLOWED - in response to anything but a POST request.
159
160    BAD REQUEST - JSON parse error, empty request body, etc.
161
162    BAD GATEWAY - lost connection to Tarantool server(s);
163                  Since both (i.e. json -> tp and tp -> json) parsers work asynchronously,
164                  this error may appear if 'params' or 'method' does not exists in the structure
165                  of the incoming JSON, please see the protocol description for more details.
166
167                  Note: this behavior will change in the future.
168
169### Input JSON form
170
171    [ { "method": STR, "params":[arg0 ... argN], "id": UINT }, ...N ]
172
173    "method"
174
175      A String containing the name of the Tarantool method to be invoked (i.e. Tarantool "call")
176
177    "params"
178
179      A Structured array. Each element is an argument of the Tarantool "call".
180
181
182    "id"
183
184      An identifier established by the Client. MUST contain an unsigned Number not
185      greater than unsigned int.
186
187      MAY be 0.
188
189    These are required fields.
190
191### Output JSON form
192
193    [ { "result": JSON_RESULT_OBJECT, "id":UINT, "error": { "message": STR, "code": INT } }, ...N ]
194
195    "result"
196
197      Tarantool execution result (a json object/array, etc).
198
199      MAY be null or undefined.
200
201    "id"
202
203      Request id is returned back.
204
205      MAY be null or undefined.
206
207
208    "error"
209
210      A Structured object which contains an internal error message.
211      This field exists only if an internal error occurred, for instance:
212      "too large request", "input json parse error", etc.
213
214      If this field exists, the input message was _probably_  not passed to the Tarantool backend.
215
216      See "message"/"code" fields for details.
217
218
219### Example
220
221      Syntax:
222
223      --> data sent to Server
224      <-- data sent to Client
225
226      rpc call 1:
227      --> { "method": "echo", "params": [42, 23], "id": 1 }
228      <-- { "result": [42, 23], "id": 1 }
229
230      rpc call 2:
231      --> { "method": "echo", "params": [ [ {"hello": "world"} ], "!" ], "id": 2 }
232      <-- { "result": [ [ {"hello": "world"} ], "!" ], "id": 2 }
233
234      rpc call of a non-existent method:
235      --> { "method": "echo_2", "id": 1 }
236      <-- { "error": {"code": -32601, "message": "Method not found"}, "id": 1 }
237
238      rpc call with invalid JSON:
239      --> { "method": "echo", "params": [1, 2, 3, __wrong__ ] }
240      <-- { "error": { "code": -32700, "message": "Parse error" } }
241
242      rpc call Batch:
243      --> [
244            { "method": "echo", "params": [42, 23], "id": 1 },
245            { "method": "echo", "params": [ [ {"hello": "world"} ], "!" ], "id": 2 }
246      ]
247      <-- [
248            { "result": [42, 23], "id": 1 },
249            { "result": [ [ {"hello": "world"} ], "!" ], "id": 2 }
250      ]
251
252      rpc call Batch of a non-existent method:
253       --> [
254            { "method": "echo_2", "params": [42, 23], "id": 1 },
255            { "method": "echo", "params": [ [ {"hello": "world"} ], "!" ], "id": 2 }
256      ]
257      <-- [
258            { "error": {"code": -32601, "message": "Method not found"}, "id": 1 },
259            { "result": [ [ {"hello": "world"} ], "!" ], "id": 2 }
260      ]
261
262      rpc call Batch with invalid JSON:
263      --> [
264            { "method": "echo", "params": [42, 23, __wrong__], "id": 1 },
265            { "method": "echo", "params": [ [ {"hello": "world"} ], "!" ], "id": 2 }
266      ]
267      <-- { "error": { "code": -32700, "message": "Parse error" } }
268
269[Back to content](#content)
270
271## Directives
272-------------
273[Back to content](#content)
274
275tnt_pass
276------------
277**syntax:** *tnt_pass UPSTREAM*
278
279**default:** *no*
280
281**context:** *location*
282
283Specify the Tarantool server backend.
284
285```nginx
286  location = /tnt {
287    tnt_pass 127.0.0.1:9999;
288  }
289
290  upstream tnt_upstream {
291     127.0.0.1:9999;
292  };
293
294 location = /tnt_next_location {
295     tnt_pass tnt_upstream;
296 }
297```
298
299[Back to content](#content)
300
301tnt_http_methods
302----------------
303**syntax:** *tnt_http_methods post, put, delete, all*
304
305**default:** *post, delete*
306
307**context:** *location*
308
309Allow to accept one or many http methods.
310If method alloed, then module expects JSON carried in request body, for details see [JSON](#json)
311If `tnt_method` is not set, then the name of the Tarantool stored procedure is the protocol [JSON](#json).
312
313Example
314```nginx
315  location tnt {
316    tnt_http_methods delete;
317    tnt_pass 127.0.0.1:9999;
318  }
319```
320
321```bash
322  # Call tarantool_stored_procedure_name()
323  $> wget --method=delete --body-data='{"method":"lua_function", "params": [], "id": 0}' NGINX_HOST/tnt
324```
325
326[Back to content](#content)
327
328tnt_http_rest_methods
329----------------
330**syntax:** *tnt_http_rest_methods get, post, put, delete, all*
331
332**default:** *no*
333
334**context:** *location*
335
336Allow to accept one or many REST methods.
337If `tnt_method` is not set, then the name of the Tarantool stored procedure is the first part of the URL path.
338
339Example
340```nginx
341  location tnt {
342    tnt_http_rest_methods get;
343    tnt_pass 127.0.0.1:9999;
344  }
345```
346
347```bash
348  # Call tarantool_stored_procedure_name()
349  $> wget NGINX_HOST/tarantool_stored_procedure_name/some/mega/path?q=1
350```
351
352[Back to content](#content)
353
354tnt_pass_http_request
355------------------
356**syntax:** *tnt_pass_http_request [on|off]*
357
358**default:** *no*
359
360**context:** *location, location if*
361
362Allow to pass HTTP headers and queries to Tarantool stored procedures.
363
364Examples
365```nginx
366  location tnt {
367    # Also, tnt_pass_http_request can be used together with JSON communication
368    tnt_http_rest_methods get;
369
370    # [on|of]
371    tnt_pass_http_request on;
372    tnt_pass 127.0.0.1:9999;
373  }
374```
375```lua
376  function tarantool_stored_procedure_name(req, ...)
377    req.headers -- lua table
378    req.query -- string
379    return { 'OK' }
380  end
381```
382```bash
383  # Call tarantool_stored_procedure_name()
384  $> wget NGINX_HOST/tarantool_stored_procedure_name/some/mega/path?q=1
385```
386
387[Back to content](#content)
388
389tnt_pass_http_request_buffer_size
390------------------------
391**syntax:** *tnt_pass_http_request_buffer_size SIZE*
392
393**default:** *4k, 8k*
394
395**context:** *location*
396
397Specify the size of the buffer used for `tnt_pass_http_request`.
398
399[Back to content](#content)
400
401tnt_method
402-----------
403**syntax:** *tnt_method STR*
404
405**default:** *no*
406
407**context:** *location, location if*
408
409Specify the Tarantool call method.
410
411Examples
412```nginx
413  location tnt {
414    # Also tnt_pass_http_request can mix with JSON communication [[
415    tnt_http_rest_methods get;
416    tnt_method tarantool_stored_procedure_name;
417    #]]
418
419    # [on|of]
420    tnt_pass_http_request on;
421    tnt_pass 127.0.0.1:9999;
422  }
423```
424```lua
425  function tarantool_stored_procedure_name(req, ...)
426    req.headers -- lua table
427    req.query -- string
428    return { 'OK' }
429  end
430```
431```bash
432  # OK Call tarantool_stored_procedure_name()
433  $> wget NGINX_HOST/tarantool_stored_procedure_name/some/mega/path?q=1
434
435  # Error Call tarantool_stored_procedure_XXX()
436  $> wget NGINX_HOST/tarantool_stored_procedure_XXX/some/mega/path?q=1
437```
438
439[Back to content](#content)
440
441tnt_send_timeout
442-------------------
443**syntax:** *tnt_send_timeout TIME*
444
445**default:** *60s*
446
447**context:** *http, server, location*
448
449The timeout for sending TCP requests to the Tarantool server, in seconds by default.
450It's wise to always explicitly specify the time unit to avoid confusion.
451Time units supported are `s`(seconds), `ms`(milliseconds), `y`(years), `M`(months), `w`(weeks), `d`(days), `h`(hours), and `m`(minutes).
452
453[Back to content](#content)
454
455tnt_read_timeout
456-------------------
457**syntax:** *tnt_read_timeout TIME*
458
459**default:** *60s*
460
461**context:** *http, server, location*
462
463The timeout for reading TCP responses from the Tarantool server, in seconds by default.
464
465It's wise to always explicitly specify the time unit to avoid confusion.
466Time units supported are `s`(seconds), `ms`(milliseconds), `y`(years), `M`(months), `w`(weeks), `d`(days), `h`(hours), and `m`(minutes).
467
468[Back to content](#content)
469
470tnt_connect_timeout
471----------------------
472**syntax:** *tnt_connect_timeout TIME*
473
474**default:** *60s*
475
476**context:** *http, server, location*
477
478The timeout for connecting to the Tarantool server, in seconds by default.
479
480It's wise to always explicitly specify the time unit to avoid confusion.
481Time units supported are `s`(seconds), `ms`(milliseconds), `y`(years), `M`(months), `w`(weeks), `d`(days), `h`(hours), and `m`(minutes).
482This time must be strictly less than 597 hours.
483
484[Back to content](#content)
485
486tnt_buffer_size
487------------------
488**syntax:** *tnt_buffer_size SIZE*
489
490**default:** *4k, 8k*
491
492**context:** *http, server, location*
493
494This buffer size is used for reading Tarantool replies,
495but it's not required to be as big as the largest possible Tarantool reply.
496
497[Back to content](#content)
498
499tnt_next_upstream
500--------------------
501**syntax:** *tnt_next_upstream [ error | timeout | invalid_response | off ]*
502
503**default:** *error timeout*
504
505**context:** *http, server, location*
506
507Specify which failure conditions should cause the request to be forwarded to another
508upstream server. Applies only when the value in [tnt_pass](#tnt_pass) is an upstream with two or more
509servers.
510
511[Back to content](#content)
512
513tnt_next_upstream_tries
514--------------------
515**syntax:** *tnt_next_upstream_tries SIZE*
516
517**default:** *0*
518
519**context:** *http, server, location*
520
521Limit the number of possible tries for passing a request to the next server.
522The 0 value turns off this limitation.
523
524tnt_next_upstream_timeout
525--------------------
526**syntax:** *tnt_next_upstream_timeout TIME*
527
528**default:** *0*
529
530**context:** *http, server, location*
531
532
533Limit the time during which a request can be passed to the next server.
534The 0 value turns off this limitation.
535
536[Back to content](#content)
537
538tnt_pure_result
539--------------------
540**syntax:** *tnt_pure_result [on|off]*
541
542**default:** *off*
543
544**context:** *http, server, location*
545
546
547Whether to wrap tnt response or not.
548When this option is off:
549```
550{"id":0, "result": [[ 1 ]]}
551```
552When this option is on:
553```
554[[1]]
555```
556
557[Back to content](#content)
558
559tnt_multireturn_skip_count
560--------------------
561**syntax:** *tnt_multireturn_skip_count [0|1|2]*
562
563**default:** *0*
564
565**context:** *http, server, location*
566
567
568Module will skip one or more multireturn parts when this option is > 0
569When it is set to 0:
570```
571{"id":0, "result": [[1]]}
572```
573
574When it is set to 1:
575```
576{"id":0, "result": [1]}
577```
578
579When it is set to 2:
580```
581{"id": 0, "result": 1}
582```
583
584[Back to content](#content)
585
586
587## Performance Tuning
588---------------------
589* Use [HttpUpstreamKeepaliveModule](http://wiki.nginx.org/HttpUpstreamKeepaliveModule).
590  * Use [keepalive](http://nginx.org/en/docs/http/ngx_http_upstream_module.html#keepalive).
591  * use [keepalive_requests](http://nginx.org/en/docs/http/ngx_http_core_module.html#keepalive_requests).
592* Use multiple instances of Tarantool servers on your multi-core machines.
593* Turn off unnecessary logging in Tarantool and NginX.
594* Tune Linux network.
595* Tune nginx buffers.
596
597[Back to content](#content)
598
599## Examples
600-----------
601Python test: test/basic_features.py, test/v20_feautres.py, nginx.dev.conf.
602
603Client side javascript example: example/echo.html, example/echo.lua.
604
605[Back to content](#content)
606
607## Copyright & License
608----------------------
609[LICENSE](https://github.com/tarantool/nginx_upstream_module/blob/master/LICENSE)
610
611[Back to content](#content)
612
613## See also
614-----------
615* [Tarantool](http://tarantool.org) homepage.
616* [lua-resty-tarantool](https://github.com/perusio/lua-resty-tarantool)
617* Tarantool [protocol](http://tarantool.org/doc/dev_guide/box-protocol.html?highlight=protocol)
618
619[Back to content](#content)
620
621================
622Please report bugs at https://github.com/tarantool/nginx_upstream_module/issues.
623
624We also warmly welcome your feedback in the discussion mailing list, tarantool@googlegroups.com.
625