Debian Codegraph @

doc/source/reference/executables/groonga-httpd.rst

raw
1 .. -*- rst -*-
2 .. Groonga Project
3
4 groonga-httpd
5 =============
6
7 .. warning::
8
9 groonga-httpd has been extracted as `groonga-nginx
10 <https://github.com/groonga/groonga-nginx>`_ since Groonga
11 13.0.3.
12
13 Summary
14 -------
15
16 groonga-httpd is a program to communicate with a Groonga server using
17 the HTTP protocol. It functions as same as
18 :doc:`groonga-server-http`. Although :doc:`groonga-server-http` has
19 limited support for HTTP with a minimal built-in HTTP server,
20 groonga-httpd has full support for HTTP with an embedded `nginx
21 <http://nginx.org/>`_. All standards-compliance and features provided
22 by nginx is also available in groonga-httpd.
23
24 groonga-httpd has an Web-based administration tool implemented with HTML and
25 JavaScript. You can access to it from http://hostname:port/.
26
27 Syntax
28 ------
29
30 ::
31
32 groonga-httpd [nginx options]
33
34 Usage
35 -----
36
37 Set up
38 ^^^^^^
39
40 First, you'll need to edit the groonga-httpd configuration file to specify a
41 database. Edit /etc/groonga/httpd/groonga-httpd.conf to enable the
42 ``groonga_database`` directive like this::
43
44 # Match this to the file owner of groonga database files if groonga-httpd is
45 # run as root.
46 #user groonga;
47 ...
48 http {
49 ...
50 # Don't change the location; currently only /d/ is supported.
51 location /d/ {
52 groonga on; # <= This means to turn on groonga-httpd.
53
54 # Specify an actual database and enable this.
55 groonga_database /var/lib/groonga/db/db;
56 }
57 ...
58 }
59
60 Then, run groonga-httpd. Note that the control immediately returns back to the
61 console because groonga-httpd runs as a daemon process by default.::
62
63 % groonga-httpd
64
65 Request queries
66 ^^^^^^^^^^^^^^^
67
68 To check, request a simple query (:doc:`/reference/commands/status`).
69
70 .. groonga-command
71 .. database: groonga-httpd
72 .. include:: ../../example/reference/executables/groonga-httpd.log
73 .. /d/status
74
75
76 Loading data by POST
77 ^^^^^^^^^^^^^^^^^^^^
78
79 You can load data by POST JSON data.
80
81 Here is an example ``curl`` command line that loads two users ``alice``
82 and ``bob`` to ``Users`` table::
83
84 % curl --data-binary '[{"_key": "alice"}, {"_key": "bob"}]' -H "Content-Type: application/json" "http://localhost:10041/d/load?table=Users"
85
86 If you loads users from JSON file, prepare JSON file like this::
87
88 [
89 {"_key": "alice"},
90 {"_key": "bob"}
91 ]
92
93 Then specify JSON file in ``curl`` command line::
94
95 % curl -X POST 'http://localhost:10041/d/load?table=Users' -H 'Content-Type: application/json' -d @users.json
96
97
98 Browse the administration tool
99 ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
100
101 Also, you can browse Web-based administration tool at http://localhost:10041/.
102
103 Shut down
104 ^^^^^^^^^
105
106 Finally, to terminate the running groonga-httpd daemon, run this::
107
108 % groonga-httpd -s stop
109
110 Configuration directives
111 ------------------------
112
113 This section describes only important directives. They are
114 groonga-httpd specific directives and performance related directives.
115
116 The following directives can be used in the groonga-httpd configuration file.
117 By default, it's located at /etc/groonga/httpd/groonga-httpd.conf.
118
119 Groonga-httpd specific directives
120 ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
121
122 The following directives aren't provided by nginx. They are provided
123 by groonga-httpd to configure groonga-httpd specific configurations.
124
125 ``groonga``
126 """""""""""
127
128 Syntax::
129
130 groonga on | off;
131
132 Default
133 ``groonga off;``
134
135 Context
136 ``location``
137
138 Specifies whether Groonga is enabled in the ``location`` block. The
139 default is ``off``. You need to specify ``on`` to enable groonga.
140
141 Examples::
142
143 location /d/ {
144 groonga on; # Enables groonga under /d/... path
145 }
146
147 location /d/ {
148 groonga off; # Disables groonga under /d/... path
149 }
150
151 .. _groonga-httpd-groonga-database:
152
153 ``groonga_database``
154 """"""""""""""""""""
155
156 Syntax::
157
158 groonga_database /path/to/groonga/database;
159
160 Default
161 ``groonga_database /usr/local/var/lib/groonga/db/db;``
162
163 Context
164 ``http``, ``server``, ``location``
165
166 Specifies the path to a Groonga database. This is the required
167 directive.
168
169 .. _groonga-httpd-groonga-database-auto-create:
170
171 ``groonga_database_auto_create``
172 """"""""""""""""""""""""""""""""
173
174 Syntax::
175
176 groonga_database_auto_create on | off;
177
178 Default
179 ``groonga_database_auto_create on;``
180
181 Context
182 ``http``, ``server``, ``location``
183
184 Specifies whether Groonga database is created automatically or not. If
185 the value is ``on`` and the Groonga database specified by
186 :ref:`groonga-httpd-groonga-database` doesn't exist, the Groonga
187 database is created automatically. If the Groonga database exists,
188 groonga-httpd does nothing.
189
190 If parent directory doesn't exist, parent directory is also created
191 recursively.
192
193 The default value is ``on``. Normally, the value doesn't need to be
194 changed.
195
196
197 ``groonga_base_path``
198 """""""""""""""""""""
199
200 Syntax::
201
202 groonga_base_path /d/;
203
204 Default
205 The same value as ``location`` name.
206
207 Context
208 ``location``
209
210 Specifies the base path in URI. Groonga uses
211 ``/d/command?parameter1=value1&...`` path to run ``command``. The form
212 of path in used in groonga-httpd but groonga-httpd also supports
213 ``/other-prefix/command?parameter1=value1&...`` form. To support the
214 form, groonga-httpd removes the base path from the head of request URI
215 and prepend ``/d/`` to the processed request URI. By the path
216 conversion, users can use custom path prefix and Groonga can always
217 uses ``/d/command?parameter1=value1&...`` form.
218
219 Normally, this directive isn't needed. It is needed for per command
220 configuration.
221
222 Here is an example configuration to add authorization to
223 :doc:`/reference/commands/shutdown` command::
224
225 groonga_database /var/lib/groonga/db/db;
226
227 location /d/shutdown {
228 groonga on;
229 # groonga_base_path is needed.
230 # Because /d/shutdown is handled as the base path.
231 # Without this configuration, /d/shutdown/shutdown path is required
232 # to run shutdown command.
233 groonga_base_path /d/;
234 auth_basic "manager is required!";
235 auth_basic_user_file "/etc/managers.htpasswd";
236 }
237
238 location /d/ {
239 groonga on;
240 # groonga_base_path doesn't needed.
241 # Because location name is the base path.
242 }
243
244 .. _groonga-httpd-groonga-log-path:
245
246 ``groonga_log_path``
247 """""""""""""""""""""
248
249 Syntax::
250
251 groonga_log_path path | off;
252
253 Default
254 ``/var/log/groonga/httpd/groonga.log``
255
256 Context
257 ``http``, ``server``, ``location``
258
259 Specifies Groonga log path in the ``http``, ``server`` or ``location`` block. The
260 default is ``/var/log/groonga/httpd/groonga.log``.
261 You can disable logging to specify ``off``.
262
263 Examples::
264
265 location /d/ {
266 groonga on;
267 # You can disable log for groonga.
268 groonga_log_path off;
269 }
270
271 .. _groonga-httpd-groonga-log-level:
272
273 ``groonga_log_level``
274 """""""""""""""""""""
275
276 Syntax::
277
278 groonga_log_level none | emergency | alert | critical | error | warning | notice | info | debug | dump;
279
280 Default
281 ``notice``
282
283 Context
284 ``http``, ``server``, ``location``
285
286 Specifies Groonga log level in the ``http``, ``server`` or ``location`` block. The
287 default is ``notice``. You can disable logging by specifying ``none`` as log level.
288
289 Examples::
290
291 location /d/ {
292 groonga on;
293 # You can customize log level for groonga.
294 groonga_log_level notice;
295 }
296
297 .. _groonga-httpd-groonga-query-log-path:
298
299 ``groonga_query_log_path``
300 """"""""""""""""""""""""""
301
302 Syntax::
303
304 groonga_query_log_path path | off;
305
306 Default
307 ``/var/log/groonga/httpd/groonga-query.log``
308
309 Context
310 ``http``, ``server``, ``location``
311
312 Specifies Groonga's query log path in the ``http``, ``server`` or
313 ``location`` block. The default is
314 ``/var/log/groonga/httpd/groonga-query.log``. You can disable logging
315 to specify ``off``.
316
317 Examples::
318
319 location /d/ {
320 groonga on;
321 # You can disable query log for groonga.
322 groonga_query_log_path off;
323 }
324
325 Query log is useful for the following cases:
326
327 * Detecting slow query.
328 * Debugging.
329
330 You can analyze your query log by `groonga-query-log package
331 <https://github.com/groonga/groonga-query-log>`_. The package provides
332 useful tools.
333
334 For example, there is a tool that analyzing your query log. It can
335 detect slow queries from your query log. There is a tool that
336 replaying same queries in your query log. It can test the new Groonga
337 before updating production environment.
338
339 Performance related directives
340 ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
341
342 The following directives are related to the performance of groonga-httpd.
343
344 ``worker_processes``
345 """"""""""""""""""""
346
347 For optimum performance, set this to be equal to the number of CPUs or cores. In
348 many cases, Groonga queries may be CPU-intensive work, so to fully utilize
349 multi-CPU/core systems, it's essential to set this accordingly.
350
351 This isn't a groonga-httpd specific directive, but an nginx's one. For details,
352 see http://wiki.nginx.org/CoreModule#worker_processes.
353
354 By default, this is set to 1. It is nginx's default.
355
356 .. _groonga-httpd-groonga-cache-limit:
357
358 ``groonga_cache_limit``
359 """""""""""""""""""""""
360
361 Syntax::
362
363 groonga_cache_limit limit;
364
365 Default
366 ``groonga_cache_limit 100;``
367
368 Context
369 ``http``, ``server``, ``location``
370
371 Specifies Groonga's limit of query cache in the ``http``, ``server`` or
372 ``location`` block. The default value is 100.
373 You can disable query cache to specify 0 to ``groonga_cache_limit`` explicitly.
374
375 Examples::
376
377 location /d/ {
378 groonga on;
379 # You can customize query cache limit for groonga.
380 groonga_cache_limit 100;
381 }
382
383 .. _groonga-httpd-groonga-cache-base-path:
384
385 ``groonga_cache_base_path``
386 """""""""""""""""""""""""""
387
388 Syntax::
389
390 groonga_cache_base_path path | off;
391
392 Default
393 ``groonga_cache_base_path off;``
394
395 Context
396 ``http``, ``server``, ``location``
397
398 Specifies the base path of query cache in the ``http``, ``server`` or
399 ``location`` block.
400
401 It's recommended that you specify this configuration when you use
402 multi-workers configuration.
403
404 If the base path is specified, you can use persistent cache instead of
405 on memory cache. If you use persistent cache, workers share query
406 cache. It's efficient for multi-workers configuration because the same
407 response is cached only once in multiple workers.
408
409 There is one more merit for persistent cache. You don't need to warm
410 up cache after groonga-httpd is restarted. Persistent cache isn't
411 cleared when groonga-httpd is down. groonga-httpd can use existing
412 persistent cache again.
413
414 The default value is ``off``. It means that persistent cache is
415 disabled. On memory cache is used. On memory cache is independent in
416 each worker. It's not efficient for multi-workers configuration
417 because two or more workers may keeps the same response separately.
418
419 Persistent cache is a bit slower than on memory cache. Normally, the
420 difference has little influence on performance.
421
422 You must specify the base path on memory file system. If you specify
423 the base path on disk, your cache will be slow. It's not make sense.
424
425 Examples::
426
427 location /d/ {
428 groonga on;
429 # You can customize query cache limit for groonga.
430 groonga_cache_base_path /dev/shm/groonga-httpd-cache;
431 }
432
433 ``proxy_cache``
434 """""""""""""""
435
436 In short, you can use nginx's reverse proxy and cache mechanism
437 instead of Groonga's built-in query cache feature.
438
439 Query cache
440 +++++++++++
441
442 Groonga has query cache feature for :doc:`/reference/commands/select`
443 command. The feature improves performance in many cases.
444
445 Query cache feature works well on groonga-httpd except you use
446 :doc:`/reference/commands/cache_limit` command on 2 or more
447 workers. Normally, :doc:`/reference/commands/cache_limit` command
448 isn't used. So there is no problem on many cases.
449
450 Here is a description about a problem of using
451 :doc:`/reference/commands/cache_limit` command on 2 or more workers.
452
453 Groonga's query cache is available in the same process. It means that
454 workers can't share the cache. If you don't change cache size, it
455 isn't a big problem. If you want to change cache size by
456 :doc:`/reference/commands/cache_limit` command, there is a problem.
457
458 There is no portable ways to change cache size for all workers.
459
460 For example, there are 3 workers::
461
462 +-- worker 1
463 client -- groonga-httpd (master) --+-- worker 2
464 +-- worker 3
465
466 The client requests :doc:`/reference/commands/cache_limit` command and
467 the worker 1 receives it::
468
469 +-> worker 1 (changed!)
470 client -> groonga-httpd (master) --+-- worker 2
471 +-- worker 3
472
473 The client requests :doc:`/reference/commands/cache_limit` command
474 again and the worker 1 receives it again::
475
476 +-> worker 1 (changed again!!!)
477 client -> groonga-httpd (master) --+-- worker 2
478 +-- worker 3
479
480 In this case, the worker 2 and the worker 3 aren't received any
481 requests. So they don't change cache size.
482
483 You can't choose a worker. So you can't change cache sizes of all
484 workers by :doc:`/reference/commands/cache_limit` command.
485
486 Reverse proxy and cache
487 +++++++++++++++++++++++
488
489 You can use nginx's reverse proxy and cache feature for query cache::
490
491 +-- worker 1
492 client -- groonga-httpd (master) -- reverse proxy + cache --+-- worker 2
493 +-- worker 3
494
495 You can use the same cache configuration for all workers but you can't
496 change cache configuration dynamically by HTTP.
497
498 Here is a sample configuration::
499
500 ...
501 http {
502 proxy_cache_path /var/cache/groonga-httpd levels=1:2 keys_zone=groonga:10m;
503 proxy_cache_valid 10m;
504 ...
505 # Reverse proxy and cache
506 server {
507 listen 10041;
508 ...
509 # Only select command
510 location /d/select {
511 # Pass through groonga with cache
512 proxy_cache groonga;
513 proxy_pass http://localhost:20041;
514 }
515
516 location / {
517 # Pass through groonga
518 proxy_pass http://localhost:20041;
519 }
520 }
521
522 # groonga
523 server {
524 location 20041;
525 location /d/ {
526 groonga on;
527 groonga_database /var/lib/groonga/db/db;
528 }
529 }
530 ...
531 }
532
533 See the following nginx documentations for parameter details:
534
535 * http://nginx.org/en/docs/http/ngx_http_proxy_module.html#proxy_cache_path
536 * http://nginx.org/en/docs/http/ngx_http_proxy_module.html#proxy_cache_valid
537 * http://nginx.org/en/docs/http/ngx_http_proxy_module.html#proxy_cache
538 * http://nginx.org/en/docs/http/ngx_http_proxy_module.html#proxy_pass
539
540 Note that you need to remove cache files created by nginx by hand
541 after you load new data to Groonga. For the above sample
542 configuration, run the following command to remove cache files::
543
544 % groonga DB_PATH < load.grn
545 % rm -rf /var/cache/groonga-httpd/*
546
547 If you use Groonga's query cache feature, you don't need to expire
548 cache by hand. It is done automatically.
549
550 nginx log related directives
551 ^^^^^^^^^^^^^^^^^^^^^^^^^^^^
552
553 ``access_log``
554 """"""""""""""
555
556 We can output nginx's log by the ``access_log`` directive.
557 The following example is the default nginx's log settings::
558
559 http {
560 log_format combined '$remote_addr - $remote_user [$time_local] "$request" '
561 '$status $body_bytes_sent "$http_referer" '
562 '"$http_user_agent" "$http_x_forwarded_for"';
563
564 access_log access.log combined;
565 }
566
567 We can specify log format by ``log_format`` directive.
568 For the details of available items of the log,
569 see `Module ngx_http_log_module <http://nginx.org/en/docs/http/ngx_http_log_module.html>`_.
570
571 nginx's access log is outputted when a requests is processed.
572 If we want to output the request processing time,
573 we need to set configuration as below::
574
575 http {
576 log_format combined '$remote_addr - $remote_user [$time_local] "$request" '
577 '$status $body_bytes_sent "$http_referer" '
578 '"$http_user_agent" "$http_x_forwarded_for" '
579 '"$request_time"';
580
581 access_log access.log combined;
582 }
583
584
585 Available nginx modules
586 -----------------------
587
588 All standard HTTP modules are available. HttpRewriteModule is
589 disabled when you don't have PCRE (Perl Compatible Regular
590 Expressions). For the list of standard HTTP modules, see
591 `nginx documentation <http://nginx.org/en/docs/>`_.