Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
nginx
GitHub Repository: nginx/nginx.org
Path: blob/main/xml/en/docs/http/ngx_http_js_module.xml
1 views
1
<?xml version="1.0"?>
2
3
<!--
4
Copyright (C) Nginx, Inc.
5
-->
6
7
<!DOCTYPE module SYSTEM "../../../../dtd/module.dtd">
8
9
<module name="Module ngx_http_js_module"
10
link="/en/docs/http/ngx_http_js_module.html"
11
lang="en"
12
rev="62">
13
14
<section id="summary">
15
16
<para>
17
The <literal>ngx_http_js_module</literal> module is used to implement
18
location and variable handlers
19
in <link doc="../njs/index.xml">njs</link> 
20
a subset of the JavaScript language.
21
</para>
22
23
<para>
24
Download and install instructions are available
25
<link doc="../njs/install.xml">here</link>.
26
</para>
27
28
</section>
29
30
31
<section id="example" name="Example Configuration">
32
33
<para>
34
The example works since
35
<link doc="../njs/changes.xml" id="njs0.4.0">0.4.0</link>.
36
<example>
37
http {
38
# since 0.9.1
39
js_engine qjs;
40
41
js_import http.js;
42
43
js_set $foo http.foo;
44
js_set $summary http.summary;
45
js_set $hash http.hash;
46
47
resolver 10.0.0.1;
48
49
server {
50
listen 8000;
51
52
location / {
53
add_header X-Foo $foo;
54
js_content http.baz;
55
}
56
57
location = /summary {
58
return 200 $summary;
59
}
60
61
location = /hello {
62
js_content http.hello;
63
}
64
65
# since 0.7.0
66
location = /fetch {
67
js_content http.fetch;
68
js_fetch_trusted_certificate /path/to/ISRG_Root_X1.pem;
69
}
70
71
# since 0.7.0
72
location = /crypto {
73
add_header Hash $hash;
74
return 200;
75
}
76
}
77
}
78
</example>
79
</para>
80
81
<para>
82
The <path>http.js</path> file:
83
<example>
84
function foo(r) {
85
r.log("hello from foo() handler");
86
return "foo";
87
}
88
89
function summary(r) {
90
var a, s, h;
91
92
s = "JS summary\n\n";
93
94
s += "Method: " + r.method + "\n";
95
s += "HTTP version: " + r.httpVersion + "\n";
96
s += "Host: " + r.headersIn.host + "\n";
97
s += "Remote Address: " + r.remoteAddress + "\n";
98
s += "URI: " + r.uri + "\n";
99
100
s += "Headers:\n";
101
for (h in r.headersIn) {
102
s += " header '" + h + "' is '" + r.headersIn[h] + "'\n";
103
}
104
105
s += "Args:\n";
106
for (a in r.args) {
107
s += " arg '" + a + "' is '" + r.args[a] + "'\n";
108
}
109
110
return s;
111
}
112
113
function baz(r) {
114
r.status = 200;
115
r.headersOut.foo = 1234;
116
r.headersOut['Content-Type'] = "text/plain; charset=utf-8";
117
r.headersOut['Content-Length'] = 15;
118
r.sendHeader();
119
r.send("nginx");
120
r.send("java");
121
r.send("script");
122
123
r.finish();
124
}
125
126
function hello(r) {
127
r.return(200, "Hello world!");
128
}
129
130
// since 0.7.0
131
async function fetch(r) {
132
let results = await Promise.all([ngx.fetch('https://nginx.org/'),
133
ngx.fetch('https://nginx.org/en/')]);
134
135
r.return(200, JSON.stringify(results, undefined, 4));
136
}
137
138
// since 0.7.0
139
async function hash(r) {
140
let hash = await crypto.subtle.digest('SHA-512', r.headersIn.host);
141
r.setReturnValue(Buffer.from(hash).toString('hex'));
142
}
143
144
export default {foo, summary, baz, hello, fetch, hash};
145
</example>
146
</para>
147
148
</section>
149
150
151
<section id="directives" name="Directives">
152
153
<directive name="js_body_filter">
154
<syntax><value>module.function</value>
155
[<value>buffer_type</value>=<value>string</value> | <value>buffer</value>]</syntax>
156
<default/>
157
<context>location</context>
158
<context>if in location</context>
159
<context>limit_except</context>
160
<appeared-in>0.5.2</appeared-in>
161
162
<para>
163
Sets an njs function as a response body filter.
164
The filter function is called for each data chunk of a response body
165
with the following arguments:
166
167
<list type="tag">
168
<tag-name><literal>r</literal></tag-name>
169
<tag-desc>
170
the <link doc="../njs/reference.xml" id="http">HTTP request</link> object
171
</tag-desc>
172
173
<tag-name><literal>data</literal></tag-name>
174
<tag-desc>
175
the incoming data chunk,
176
may be a string or Buffer
177
depending on the <literal>buffer_type</literal> value,
178
by default is a string.
179
Since <link doc="../njs/changes.xml" id="njs0.8.5">0.8.5</link>, the
180
<literal>data</literal> value is implicitly converted to a valid UTF-8 string
181
by default.
182
For binary data, the <literal>buffer_type</literal> value
183
should be set to <literal>buffer</literal>.
184
</tag-desc>
185
186
<tag-name><literal>flags</literal></tag-name>
187
<tag-desc>
188
an object with the following properties:
189
<list type="tag">
190
<tag-name><literal>last</literal></tag-name>
191
<tag-desc>
192
a boolean value, true if data is a last buffer.
193
</tag-desc>
194
195
</list>
196
</tag-desc>
197
198
</list>
199
</para>
200
201
<para>
202
The filter function can pass its own modified version
203
of the input data chunk to the next body filter by calling
204
<link doc="../njs/reference.xml" id="r_sendbuffer"><literal>r.sendBuffer()</literal></link>.
205
For example, to transform all the lowercase letters in the response body:
206
<example>
207
function filter(r, data, flags) {
208
r.sendBuffer(data.toLowerCase(), flags);
209
}
210
</example>
211
</para>
212
213
<para>
214
If the filter function changes the length of the response body, the
215
<header>Content-Length</header> response header (if present) should be cleared
216
in <link id="js_header_filter"><literal>js_header_filter</literal></link>
217
to enforce chunked transfer encoding:
218
<example>
219
example.conf:
220
location /foo {
221
# proxy_pass http://localhost:8080;
222
223
js_header_filter main.clear_content_length;
224
js_body_filter main.filter;
225
}
226
227
example.js:
228
function clear_content_length(r) {
229
delete r.headersOut['Content-Length'];
230
}
231
</example>
232
</para>
233
234
<para>
235
To stop filtering and pass the data chunks to the client
236
without calling <literal>js_body_filter</literal>,
237
<link doc="../njs/reference.xml" id="r_done"><literal>r.done()</literal></link>
238
can be used.
239
For example, to prepend some data to the response body:
240
<example>
241
function prepend(r, data, flags) {
242
r.sendBuffer("XXX");
243
r.sendBuffer(data, flags);
244
r.done();
245
}
246
</example>
247
</para>
248
249
<para>
250
<note>
251
As the <literal>js_body_filter</literal> handler
252
returns its result immediately, it supports
253
only synchronous operations.
254
Thus, asynchronous operations such as
255
<link doc="../njs/reference.xml" id="r_subrequest">r.subrequest()</link>
256
or
257
<link doc="../njs/reference.xml" id="settimeout">setTimeout()</link>
258
are not supported.
259
</note>
260
</para>
261
262
<para>
263
<note>
264
The directive can be specified inside the
265
<link doc="../http/ngx_http_rewrite_module.xml" id="if">if</link> block
266
since <link doc="../njs/changes.xml" id="njs0.7.7">0.7.7</link>.
267
</note>
268
</para>
269
270
</directive>
271
272
273
<directive name="js_access">
274
<syntax><value>module.function</value></syntax>
275
<default/>
276
<context>location</context>
277
<context>if in location</context>
278
<context>limit_except</context>
279
<appeared-in>0.9.9</appeared-in>
280
281
<para>
282
Sets an njs function as a handler in the
283
<link doc="../dev/development_guide.xml" id="http_phases">access phase</link>.
284
Asynchronous operations such as
285
<link doc="../njs/reference.xml" id="r_subrequest">r.subrequest()</link>,
286
<link doc="../njs/reference.xml" id="ngx_fetch">ngx.fetch()</link>,
287
and <link doc="../njs/reference.xml" id="settimeout">setTimeout()</link>
288
are supported.
289
</para>
290
291
<para>
292
A handler that returns without calling
293
<link doc="../njs/reference.xml" id="r_return"><literal>r.return()</literal></link>
294
or
295
<link doc="../njs/reference.xml" id="r_decline"><literal>r.decline()</literal></link>
296
grants access.
297
To deny access or send a custom response (for example, a redirect),
298
the handler may call
299
<link doc="../njs/reference.xml" id="r_return"><literal>r.return()</literal></link>.
300
To make the handler express no opinion, deferring the decision to other
301
access checkers under
302
<link doc="ngx_http_core_module.xml" id="satisfy"><literal>satisfy any</literal></link>,
303
the handler may call
304
<link doc="../njs/reference.xml" id="r_decline"><literal>r.decline()</literal></link>.
305
</para>
306
307
<para>
308
For example:
309
<example>
310
example.conf:
311
location /protected/ {
312
js_access main.auth;
313
proxy_pass http://upstream;
314
}
315
316
example.js:
317
async function auth(r) {
318
let reply = await ngx.fetch('http://authsvc/check', {
319
headers: {Authorization: r.headersIn.Authorization}
320
});
321
322
if (reply.status != 200) {
323
r.return(401);
324
return;
325
}
326
}
327
328
export default {auth};
329
</example>
330
</para>
331
332
</directive>
333
334
335
<directive name="js_content">
336
<syntax><value>module.function</value></syntax>
337
<default/>
338
<context>location</context>
339
<context>if in location</context>
340
<context>limit_except</context>
341
342
<para>
343
Sets an njs function as a location content handler.
344
Since <link doc="../njs/changes.xml" id="njs0.4.0">0.4.0</link>,
345
a module function can be referenced.
346
</para>
347
348
<para>
349
<note>
350
The directive can be specified inside the
351
<link doc="../http/ngx_http_rewrite_module.xml" id="if">if</link> block
352
since <link doc="../njs/changes.xml" id="njs0.7.7">0.7.7</link>.
353
</note>
354
</para>
355
356
</directive>
357
358
359
<directive name="js_context_reuse">
360
<syntax><value>number</value></syntax>
361
<default>128</default>
362
<context>http</context>
363
<context>server</context>
364
<context>location</context>
365
<appeared-in>0.8.6</appeared-in>
366
367
<para>
368
Sets a maximum number of JS context to be reused for
369
<link doc="../njs/engine.xml">QuickJS engine</link>.
370
Each context is used for a single request.
371
The finished context is put into a pool of reusable contexts.
372
If the pool is full, the context is destroyed.
373
</para>
374
375
</directive>
376
377
378
<directive name="js_engine">
379
<syntax><literal>njs</literal> | <literal>qjs</literal></syntax>
380
<default>njs</default>
381
<context>http</context>
382
<context>server</context>
383
<context>location</context>
384
<appeared-in>0.8.6</appeared-in>
385
386
<para>
387
Sets a <link doc="../njs/engine.xml">JavaScript engine</link>
388
to be used for njs scripts.
389
The <literal>njs</literal> parameter sets the njs engine, also used by default.
390
The <literal>qjs</literal> parameter sets the QuickJS engine.
391
</para>
392
393
<para>
394
<note>
395
The <literal>njs</literal> engine is deprecated
396
since <link doc="../njs/changes.xml" id="njs1.0.0">1.0.0</link>;
397
new configurations should use the <literal>qjs</literal>
398
(<link doc="../njs/engine.xml" id="quickjs_engine">QuickJS</link>) engine.
399
</note>
400
</para>
401
402
</directive>
403
404
405
<directive name="js_fetch_buffer_size">
406
<syntax><value>size</value></syntax>
407
<default>16k</default>
408
<context>http</context>
409
<context>server</context>
410
<context>location</context>
411
<appeared-in>0.7.4</appeared-in>
412
413
<para>
414
Sets the <value>size</value> of the buffer used for reading and writing
415
with <link doc="../njs/reference.xml" id="ngx_fetch">Fetch API</link>.
416
</para>
417
418
</directive>
419
420
421
<directive name="js_fetch_ciphers">
422
<syntax><value>ciphers</value></syntax>
423
<default>HIGH:!aNULL:!MD5</default>
424
<context>http</context>
425
<context>server</context>
426
<context>location</context>
427
<appeared-in>0.7.0</appeared-in>
428
429
<para>
430
Specifies the enabled ciphers for HTTPS requests
431
with <link doc="../njs/reference.xml" id="ngx_fetch">Fetch API</link>.
432
The ciphers are specified in the format understood by the
433
OpenSSL library.
434
</para>
435
436
<para>
437
The full list can be viewed using the
438
<command>openssl ciphers</command>” command.
439
</para>
440
441
</directive>
442
443
444
<directive name="js_fetch_max_response_buffer_size">
445
<syntax><value>size</value></syntax>
446
<default>1m</default>
447
<context>http</context>
448
<context>server</context>
449
<context>location</context>
450
<appeared-in>0.7.4</appeared-in>
451
452
<para>
453
Sets the maximum <value>size</value> of the response received
454
with <link doc="../njs/reference.xml" id="ngx_fetch">Fetch API</link>.
455
</para>
456
457
</directive>
458
459
460
<directive name="js_fetch_protocols">
461
<syntax>
462
[<literal>TLSv1</literal>]
463
[<literal>TLSv1.1</literal>]
464
[<literal>TLSv1.2</literal>]
465
[<literal>TLSv1.3</literal>]</syntax>
466
<default>TLSv1 TLSv1.1 TLSv1.2</default>
467
<context>http</context>
468
<context>server</context>
469
<context>location</context>
470
<appeared-in>0.7.0</appeared-in>
471
472
<para>
473
Enables the specified protocols for HTTPS requests
474
with <link doc="../njs/reference.xml" id="ngx_fetch">Fetch API</link>.
475
</para>
476
477
</directive>
478
479
480
<directive name="js_fetch_timeout">
481
<syntax><value>time</value></syntax>
482
<default>60s</default>
483
<context>http</context>
484
<context>server</context>
485
<context>location</context>
486
<appeared-in>0.7.4</appeared-in>
487
488
<para>
489
Defines a timeout for reading and writing
490
for <link doc="../njs/reference.xml" id="ngx_fetch">Fetch API</link>.
491
The timeout is set only between two successive read/write operations,
492
not for the whole response.
493
If no data is transmitted within this time, the connection is closed.
494
</para>
495
496
</directive>
497
498
499
<directive name="js_fetch_trusted_certificate">
500
<syntax><value>file</value></syntax>
501
<default/>
502
<context>http</context>
503
<context>server</context>
504
<context>location</context>
505
<appeared-in>0.7.0</appeared-in>
506
507
<para>
508
Specifies a <value>file</value> with trusted CA certificates in the PEM format
509
used to
510
<link doc="../njs/reference.xml" id="fetch_verify">verify</link>
511
the HTTPS certificate
512
with <link doc="../njs/reference.xml" id="ngx_fetch">Fetch API</link>.
513
</para>
514
515
</directive>
516
517
518
<directive name="js_fetch_verify">
519
<syntax><literal>on</literal> | <literal>off</literal></syntax>
520
<default>on</default>
521
<context>http</context>
522
<context>server</context>
523
<context>location</context>
524
<appeared-in>0.7.4</appeared-in>
525
526
<para>
527
Enables or disables verification of the HTTPS server certificate
528
with <link doc="../njs/reference.xml" id="ngx_fetch">Fetch API</link>.
529
</para>
530
531
</directive>
532
533
534
<directive name="js_fetch_verify_depth">
535
<syntax><value>number</value></syntax>
536
<default>100</default>
537
<context>http</context>
538
<context>server</context>
539
<context>location</context>
540
<appeared-in>0.7.0</appeared-in>
541
542
<para>
543
Sets the verification depth in the HTTPS server certificates chain
544
with <link doc="../njs/reference.xml" id="ngx_fetch">Fetch API</link>.
545
</para>
546
547
</directive>
548
549
550
<directive name="js_fetch_proxy">
551
<syntax><value>url</value></syntax>
552
<default/>
553
<context>http</context>
554
<context>server</context>
555
<context>location</context>
556
<appeared-in>0.9.4</appeared-in>
557
558
<para>
559
Configures a forward proxy URL
560
with <link doc="../njs/reference.xml" id="ngx_fetch">Fetch API</link>.
561
The <value>url</value> supports the HTTP scheme only
562
and can contain optional user credentials
563
in the format <literal>http://[user:password@]host:port</literal>
564
for Basic authentication.
565
Supports both HTTP and HTTPS connections to destination servers.
566
If the <value>url</value> is empty, proxy routing is disabled.
567
The parameter value can contain variables.
568
</para>
569
570
<para>
571
Example:
572
<example>
573
location /fetch {
574
js_fetch_proxy http://user:[email protected]:3128;
575
js_content main.fetch_handler;
576
}
577
</example>
578
</para>
579
580
</directive>
581
582
583
<directive name="js_fetch_keepalive">
584
<syntax><value>connections</value></syntax>
585
<default>0</default>
586
<context>http</context>
587
<context>server</context>
588
<context>location</context>
589
<appeared-in>0.9.2</appeared-in>
590
591
<para>
592
Activates the cache for connections to destination servers.
593
When the value is greater than <literal>0</literal>,
594
enables keepalive connections for
595
<link doc="../njs/reference.xml" id="ngx_fetch">Fetch API</link>.
596
</para>
597
598
<para>
599
The <value>connections</value> parameter sets the maximum number of idle
600
keepalive connections to destination servers that are preserved in the cache
601
of each worker process.
602
When this number is exceeded, the least recently used connections are closed.
603
</para>
604
605
<para>
606
In HTTP, the cache is maintained separately for each effective location
607
configuration. A value set at the <literal>http</literal> or
608
<literal>server</literal> level is inherited by locations, but each location
609
uses its own cache.
610
Cached connections are reused for requests with the same protocol, host,
611
and port.
612
</para>
613
614
<para>
615
When enabled, keepalive assumes that destination servers send valid HTTP
616
responses.
617
</para>
618
619
<para>
620
Example:
621
<example>
622
location /fetch {
623
js_fetch_keepalive 32;
624
js_fetch_trusted_certificate /path/to/ISRG_Root_X1.pem;
625
js_content main.fetch_handler;
626
}
627
</example>
628
</para>
629
630
</directive>
631
632
633
<directive name="js_fetch_keepalive_requests">
634
<syntax><value>number</value></syntax>
635
<default>1000</default>
636
<context>http</context>
637
<context>server</context>
638
<context>location</context>
639
<appeared-in>0.9.2</appeared-in>
640
641
<para>
642
Sets the maximum number of requests that can be served through one keepalive
643
connection with <link doc="../njs/reference.xml" id="ngx_fetch">Fetch API</link>.
644
After the maximum number of requests is made, the connection is closed.
645
</para>
646
647
<para>
648
Closing connections periodically is necessary to free per-connection memory
649
allocations.
650
Therefore, using too high maximum number of requests could result in
651
excessive memory usage and not recommended.
652
</para>
653
654
</directive>
655
656
657
<directive name="js_fetch_keepalive_time">
658
<syntax><value>time</value></syntax>
659
<default>1h</default>
660
<context>http</context>
661
<context>server</context>
662
<context>location</context>
663
<appeared-in>0.9.2</appeared-in>
664
665
<para>
666
Limits the maximum time during which requests can be processed through one
667
keepalive connection with <link doc="../njs/reference.xml" id="ngx_fetch">Fetch API</link>.
668
After this time is reached, the connection is closed following the subsequent
669
request processing.
670
</para>
671
672
</directive>
673
674
675
<directive name="js_fetch_keepalive_timeout">
676
<syntax><value>time</value></syntax>
677
<default>60s</default>
678
<context>http</context>
679
<context>server</context>
680
<context>location</context>
681
<appeared-in>0.9.2</appeared-in>
682
683
<para>
684
Sets a timeout during which an idle keepalive connection to a destination server
685
will stay open with <link doc="../njs/reference.xml" id="ngx_fetch">Fetch API</link>.
686
</para>
687
688
</directive>
689
690
691
<directive name="js_header_filter">
692
<syntax><value>module.function</value></syntax>
693
<default/>
694
<context>location</context>
695
<context>if in location</context>
696
<context>limit_except</context>
697
<appeared-in>0.5.1</appeared-in>
698
699
<para>
700
Sets an njs function as a response header filter.
701
The directive allows changing arbitrary header fields of a response header.
702
</para>
703
704
<para>
705
<note>
706
As the <literal>js_header_filter</literal> handler
707
returns its result immediately, it supports
708
only synchronous operations.
709
Thus, asynchronous operations such as
710
<link doc="../njs/reference.xml" id="r_subrequest">r.subrequest()</link>
711
or
712
<link doc="../njs/reference.xml" id="settimeout">setTimeout()</link>
713
are not supported.
714
</note>
715
</para>
716
717
<para>
718
<note>
719
The directive can be specified inside the
720
<link doc="../http/ngx_http_rewrite_module.xml" id="if">if</link> block
721
since <link doc="../njs/changes.xml" id="njs0.7.7">0.7.7</link>.
722
</note>
723
</para>
724
725
</directive>
726
727
728
<directive name="js_import">
729
<syntax><value>module.js</value> |
730
<value>export_name from module.js</value></syntax>
731
<default/>
732
<context>http</context>
733
<context>server</context>
734
<context>location</context>
735
<appeared-in>0.4.0</appeared-in>
736
737
<para>
738
Imports a module that implements location and variable handlers in njs.
739
The <literal>export_name</literal> is used as a namespace
740
to access module functions.
741
If the <literal>export_name</literal> is not specified,
742
the module name will be used as a namespace.
743
<example>
744
js_import http.js;
745
</example>
746
Here, the module name <literal>http</literal> is used as a namespace
747
while accessing exports.
748
If the imported module exports <literal>foo()</literal>,
749
<literal>http.foo</literal> is used to refer to it.
750
</para>
751
752
<para>
753
Several <literal>js_import</literal> directives can be specified.
754
</para>
755
756
<para>
757
When <literal>js_import</literal> is specified inside a
758
<link doc="ngx_http_core_module.xml" id="location"/>,
759
the imported modules are visible only if no JavaScript code
760
has been invoked earlier in the request.
761
A single VM is created per request when a
762
<link id="js_set"/> variable is first accessed
763
or when a <link id="js_content"/>, <link id="js_header_filter"/>,
764
<link id="js_body_filter"/>, or <link id="js_access"/> handler
765
is first invoked.
766
The VM is cloned from the configuration scope active at that moment,
767
and all subsequent JavaScript invocations within the request reuse it.
768
Thus, if any of the above is referenced before the request is mapped
769
to its final location, for example from a server-level
770
<link doc="ngx_http_rewrite_module.xml" id="set"/>,
771
the VM is bound to the import set of the outer scope, and modules
772
imported in the matched <literal>location</literal> will not be visible.
773
To avoid this, declare such imports in a common parent scope.
774
</para>
775
776
<para>
777
<note>
778
The directive can be specified on the
779
<literal>server</literal> and <literal>location</literal> level
780
since <link doc="../njs/changes.xml" id="njs0.7.7">0.7.7</link>.
781
</note>
782
</para>
783
784
</directive>
785
786
787
<directive name="js_include">
788
<syntax><value>file</value></syntax>
789
<default/>
790
<context>http</context>
791
792
<para>
793
Specifies a file that implements location and variable handlers in njs:
794
<example>
795
nginx.conf:
796
js_include http.js;
797
location /version {
798
js_content version;
799
}
800
801
http.js:
802
function version(r) {
803
r.return(200, njs.version);
804
}
805
</example>
806
</para>
807
808
<para>
809
The directive was made obsolete in version
810
<link doc="../njs/changes.xml" id="njs0.4.0">0.4.0</link>
811
and was removed in version
812
<link doc="../njs/changes.xml" id="njs0.7.1">0.7.1</link>.
813
The <link id="js_import"/> directive should be used instead.
814
</para>
815
816
</directive>
817
818
819
<directive name="js_load_http_native_module">
820
<syntax><value>path</value> [<literal>as</literal> <value>name</value>]</syntax>
821
<default/>
822
<context>main</context>
823
<appeared-in>0.9.5</appeared-in>
824
825
<para>
826
Loads a <link doc="../njs/native_modules.xml">native module</link>
827
(shared library) for use in HTTP JavaScript code.
828
The directive is
829
<link doc="../njs/engine.xml" id="quickjs_engine">QuickJS</link>-only
830
and is not available when using the njs built-in JavaScript engine.
831
</para>
832
833
<para>
834
The <value>path</value> parameter
835
specifies the absolute path to the shared library file.
836
The optional <literal>as</literal> <value>name</value> parameter
837
provides an alias name for importing the module in JavaScript code.
838
If not specified, the module can be imported using its filename.
839
</para>
840
841
<para>
842
Example:
843
<example>
844
js_load_http_native_module /path/to/mylib.so;
845
js_load_http_native_module /path/to/other.so as myalias;
846
847
http {
848
js_import main.js;
849
# ... rest of http configuration
850
}
851
</example>
852
In JavaScript code:
853
<example>
854
// Import by filename
855
import * as mylib from 'mylib.so';
856
857
// Import by alias
858
import * as myalias from 'myalias';
859
860
// Use exported functions
861
let result = mylib.add(5, 10);
862
</example>
863
</para>
864
865
<para>
866
<note>
867
For security reasons, this directive is only allowed
868
in the <literal>main</literal> configuration context.
869
Native modules run with full process privileges;
870
use absolute paths and ensure proper code review.
871
</note>
872
</para>
873
874
</directive>
875
876
877
<directive name="js_path">
878
<syntax>
879
<value>path</value></syntax>
880
<default/>
881
<context>http</context>
882
<context>server</context>
883
<context>location</context>
884
<appeared-in>0.3.0</appeared-in>
885
886
<para>
887
Sets an additional path for njs modules.
888
</para>
889
890
<para>
891
<note>
892
The directive can be specified on the
893
<literal>server</literal> and <literal>location</literal> level
894
since <link doc="../njs/changes.xml" id="njs0.7.7">0.7.7</link>.
895
</note>
896
</para>
897
898
</directive>
899
900
901
<directive name="js_periodic">
902
<syntax><value>module.function</value>
903
[<literal>interval</literal>=<value>time</value>]
904
[<literal>jitter</literal>=<value>number</value>]
905
[<literal>worker_affinity</literal>=<value>mask</value>]</syntax>
906
<default/>
907
<context>location</context>
908
<appeared-in>0.8.1</appeared-in>
909
910
<para>
911
Specifies a content handler to run at regular interval.
912
The handler receives a
913
<link doc="../njs/reference.xml" id="periodic_session">session object</link>
914
as its first argument,
915
it also has access to global objects such as
916
<link doc="../njs/reference.xml" id="ngx">ngx</link>.
917
</para>
918
919
<para>
920
The optional <literal>interval</literal> parameter
921
sets the interval between two consecutive runs,
922
by default, 5 seconds.
923
</para>
924
925
<para>
926
The optional <literal>jitter</literal> parameter sets the time within which
927
the location content handler will be randomly delayed,
928
by default, there is no delay.
929
</para>
930
931
<para>
932
By default, the <literal>js_handler</literal> is executed on worker process 0.
933
The optional <literal>worker_affinity</literal> parameter
934
allows specifying particular worker processes
935
where the location content handler should be executed.
936
Each worker process set is represented by a bitmask of allowed worker processes.
937
The <literal>all</literal> mask allows the handler to be executed
938
in all worker processes.
939
</para>
940
941
<para>
942
Example:
943
<example>
944
example.conf:
945
946
location @periodics {
947
# to be run at 1 minute intervals in worker process 0
948
js_periodic main.handler interval=60s;
949
950
# to be run at 1 minute intervals in all worker processes
951
js_periodic main.handler interval=60s worker_affinity=all;
952
953
# to be run at 1 minute intervals in worker processes 1 and 3
954
js_periodic main.handler interval=60s worker_affinity=0101;
955
956
resolver 10.0.0.1;
957
js_fetch_trusted_certificate /path/to/ISRG_Root_X1.pem;
958
}
959
960
example.js:
961
962
async function handler(s) {
963
let reply = await ngx.fetch('https://nginx.org/en/docs/njs/');
964
let body = await reply.text();
965
966
ngx.log(ngx.INFO, body);
967
}
968
</example>
969
</para>
970
971
</directive>
972
973
974
<directive name="js_preload_object">
975
<syntax><value>name.json</value> |
976
<value>name</value> from <value>file.json</value></syntax>
977
<default/>
978
<context>http</context>
979
<context>server</context>
980
<context>location</context>
981
<appeared-in>0.7.8</appeared-in>
982
983
<para>
984
Preloads an
985
<link doc="../njs/preload_objects.xml">immutable object</link>
986
at configure time.
987
The <literal>name</literal> is used as a name of the global variable
988
though which the object is available in njs code.
989
If the <literal>name</literal> is not specified,
990
the file name will be used instead.
991
<example>
992
js_preload_object map.json;
993
</example>
994
Here, the <literal>map</literal> is used as a name
995
while accessing the preloaded object.
996
</para>
997
998
<para>
999
Several <literal>js_preload_object</literal> directives can be specified.
1000
</para>
1001
1002
</directive>
1003
1004
1005
<directive name="js_set">
1006
<syntax>
1007
<value>$variable</value>
1008
<value>module.function</value>
1009
[<literal>nocache</literal>]</syntax>
1010
<default/>
1011
<context>http</context>
1012
<context>server</context>
1013
<context>location</context>
1014
1015
<para>
1016
Sets an njs <literal>function</literal>
1017
for the specified <literal>variable</literal>.
1018
Since <link doc="../njs/changes.xml" id="njs0.4.0">0.4.0</link>,
1019
a module function can be referenced.
1020
</para>
1021
1022
<para>
1023
The function is called when
1024
the variable is referenced for the first time for a given request.
1025
The exact moment depends on a
1026
<link doc="../dev/development_guide.xml" id="http_phases">phase</link>
1027
at which the variable is referenced.
1028
This can be used to perform some logic
1029
not related to variable evaluation.
1030
For example, if the variable is referenced only in the
1031
<link doc="ngx_http_log_module.xml" id="log_format"/> directive,
1032
its handler will not be executed until the log phase.
1033
This handler can be used to do some cleanup
1034
right before the request is freed.
1035
</para>
1036
1037
<para>
1038
Since <link doc="../njs/changes.xml" id="njs0.8.6">0.8.6</link>,
1039
if an optional argument <literal>nocache</literal> is specified,
1040
the handler is called every time it is referenced.
1041
Due to current limitations
1042
of the <link doc="ngx_http_rewrite_module.xml">rewrite</link> module,
1043
when a <literal>nocache</literal> variable is referenced by the
1044
<link doc="ngx_http_rewrite_module.xml" id="set">set</link> directive
1045
its handler should always return a fixed-length value.
1046
</para>
1047
1048
<para>
1049
<note>
1050
As the <literal>js_set</literal> handler
1051
returns its result immediately, it supports
1052
only synchronous operations.
1053
Thus, asynchronous operations such as
1054
<link doc="../njs/reference.xml" id="r_subrequest">r.subrequest()</link>
1055
or
1056
<link doc="../njs/reference.xml" id="settimeout">setTimeout()</link>
1057
are not supported.
1058
</note>
1059
</para>
1060
1061
<para>
1062
<note>
1063
The directive can be specified on the
1064
<literal>server</literal> and <literal>location</literal> level
1065
since <link doc="../njs/changes.xml" id="njs0.7.7">0.7.7</link>.
1066
</note>
1067
</para>
1068
1069
</directive>
1070
1071
1072
<directive name="js_shared_dict_zone">
1073
<syntax>
1074
<literal>zone</literal>=<value>name</value>:<value>size</value>
1075
[<literal>timeout</literal>=<value>time</value>]
1076
[<literal>type</literal>=<literal>string</literal>|<literal>number</literal>]
1077
[<literal>evict</literal>]
1078
[<literal>state</literal>=<value>file</value>]</syntax>
1079
<default/>
1080
<context>http</context>
1081
<appeared-in>0.8.0</appeared-in>
1082
1083
<para>
1084
Sets the <value>name</value> and <value>size</value> of the shared memory zone
1085
that keeps the
1086
key-value <link doc="../njs/reference.xml" id="dict">dictionary</link>
1087
shared between worker processes.
1088
</para>
1089
1090
<para>
1091
By default the shared dictionary uses a string as a key and a value.
1092
The optional <literal>type</literal> parameter
1093
allows redefining the value type to number.
1094
</para>
1095
1096
<para>
1097
The optional <literal>timeout</literal> parameter sets
1098
the time in milliseconds
1099
after which all shared dictionary entries are removed from the zone.
1100
If some entries require a different removal time, it can be set
1101
with the <literal>timeout</literal> argument of the
1102
<link doc="../njs/reference.xml" id="dict_add">add</link>,
1103
<link doc="../njs/reference.xml" id="dict_incr">incr</link>, and
1104
<link doc="../njs/reference.xml" id="dict_set">set</link>
1105
methods
1106
(<link doc="../njs/changes.xml" id="njs0.8.5">0.8.5</link>).
1107
</para>
1108
1109
<para>
1110
The optional <literal>evict</literal> parameter removes the oldest
1111
key-value pair when the zone storage is exhausted.
1112
</para>
1113
1114
<para>
1115
The optional <literal>state</literal> parameter specifies a <value>file</value>
1116
that keeps the shared dictionary state
1117
in JSON format and makes it persistent across nginx restarts
1118
(<link doc="../njs/changes.xml" id="njs0.9.1">0.9.1</link>).
1119
</para>
1120
1121
<para>
1122
Example:
1123
<example>
1124
example.conf:
1125
# Creates a 1Mb dictionary with string values,
1126
# removes key-value pairs after 60 seconds of inactivity:
1127
js_shared_dict_zone zone=foo:1M timeout=60s;
1128
1129
# Creates a 512Kb dictionary with string values,
1130
# forcibly removes oldest key-value pairs when the zone is exhausted:
1131
js_shared_dict_zone zone=bar:512K timeout=30s evict;
1132
1133
# Creates a 32Kb permanent dictionary with number values:
1134
js_shared_dict_zone zone=num:32k type=number;
1135
1136
# Creates a 1Mb dictionary with string values and persistent state:
1137
js_shared_dict_zone zone=persistent:1M state=/tmp/dict.json;
1138
1139
example.js:
1140
function get(r) {
1141
r.return(200, ngx.shared.foo.get(r.args.key));
1142
}
1143
1144
function set(r) {
1145
r.return(200, ngx.shared.foo.set(r.args.key, r.args.value));
1146
}
1147
1148
function del(r) {
1149
r.return(200, ngx.shared.bar.delete(r.args.key));
1150
}
1151
1152
function increment(r) {
1153
r.return(200, ngx.shared.num.incr(r.args.key, 2));
1154
}
1155
</example>
1156
</para>
1157
1158
</directive>
1159
1160
1161
<directive name="js_var">
1162
<syntax><value>$variable</value> [<value>value</value>]</syntax>
1163
<default/>
1164
<context>http</context>
1165
<context>server</context>
1166
<context>location</context>
1167
<appeared-in>0.5.3</appeared-in>
1168
1169
<para>
1170
Declares
1171
a <link doc="../njs/reference.xml" id="r_variables">writable</link>
1172
variable.
1173
The value can contain text, variables, and their combination.
1174
The variable is not overwritten after a redirect
1175
unlike variables created with the
1176
<link doc="ngx_http_rewrite_module.xml" id="set"/> directive.
1177
</para>
1178
1179
<para>
1180
<note>
1181
The directive can be specified on the
1182
<literal>server</literal> and <literal>location</literal> level
1183
since <link doc="../njs/changes.xml" id="njs0.7.7">0.7.7</link>.
1184
</note>
1185
</para>
1186
1187
</directive>
1188
1189
</section>
1190
1191
1192
<section id="arguments" name="Request Argument">
1193
1194
<para>
1195
Each HTTP njs handler receives one argument, a request
1196
<link doc="../njs/reference.xml" id="http">object</link>.
1197
</para>
1198
1199
</section>
1200
1201
</module>
1202
1203