Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
nginx
GitHub Repository: nginx/nginx.org
Path: blob/main/xml/en/docs/njs/index.xml
1 views
1
<?xml version="1.0"?>
2
3
<!--
4
Copyright (C) Nginx, Inc.
5
-->
6
7
<!DOCTYPE article SYSTEM "../../../../dtd/article.dtd">
8
9
<article name="nginx JavaScript module"
10
link="/en/docs/njs/index.html"
11
lang="en"
12
rev="38"
13
toc="no">
14
15
<section id="summary">
16
17
<para>
18
njs is an nginx module that extends the server's functionality through
19
JavaScript scripting, enabling the creation of custom
20
server-side logic and <link id="usecases">more</link>.
21
</para>
22
23
<para>
24
<note>
25
The built-in
26
<link doc="engine.xml" id="njs_engine">njs</link> JavaScript engine
27
is deprecated
28
since <link doc="changes.xml" id="njs1.0.0">1.0.0</link>;
29
new configurations should use the
30
<link doc="engine.xml" id="quickjs_engine">QuickJS</link> engine.
31
</note>
32
</para>
33
34
</section>
35
36
37
<section id="links">
38
39
<para>
40
<list type="bullet">
41
42
<listitem>
43
<link doc="install.xml"/>
44
</listitem>
45
46
<listitem>
47
<link doc="changes.xml"/>
48
</listitem>
49
50
<listitem>
51
<link doc="reference.xml"/>
52
</listitem>
53
54
<listitem>
55
<link doc="engine.xml"/>
56
</listitem>
57
58
<listitem>
59
<link doc="native_modules.xml"/>
60
</listitem>
61
62
<listitem>
63
<link url="https://github.com/nginx/njs-examples/">Examples</link>
64
</listitem>
65
66
<listitem>
67
<link doc="security.xml"/>
68
</listitem>
69
70
<listitem>
71
<link doc="compatibility.xml"/>
72
</listitem>
73
74
<listitem>
75
<link doc="cli.xml"/>
76
</listitem>
77
78
<listitem>
79
<link doc="preload_objects.xml"/>
80
</listitem>
81
82
<listitem>
83
<link id="tested_os_and_platforms">Tested OS and platforms</link>
84
</listitem>
85
86
</list>
87
</para>
88
89
<para>
90
<list type="bullet">
91
92
<listitem>
93
<link doc="../http/ngx_http_js_module.xml">
94
ngx_http_js_module</link>
95
</listitem>
96
97
<listitem>
98
<link doc="../stream/ngx_stream_js_module.xml">
99
ngx_stream_js_module</link>
100
</listitem>
101
102
</list>
103
</para>
104
105
<para>
106
<list type="bullet">
107
108
<listitem>
109
<link doc="typescript.xml"/>
110
</listitem>
111
112
<listitem>
113
<link doc="node_modules.xml"/>
114
</listitem>
115
116
</list>
117
</para>
118
119
</section>
120
121
122
<section id="usecases" name="Use cases">
123
124
<para>
125
<list type="bullet">
126
127
<listitem>
128
Complex access control and security checks in njs
129
before a request reaches an upstream server
130
</listitem>
131
132
<listitem>
133
Manipulating response headers
134
</listitem>
135
136
<listitem>
137
Writing flexible asynchronous content handlers and filters
138
</listitem>
139
140
</list>
141
See <link url="https://github.com/nginx/njs-examples/">examples</link>
142
for more njs use cases.
143
</para>
144
145
</section>
146
147
148
<section id="example" name="Basic HTTP Example">
149
150
<para>
151
To use njs in nginx:
152
<list type="bullet">
153
154
<listitem>
155
<para>
156
<link doc="install.xml">install</link> njs scripting language
157
</para>
158
</listitem>
159
160
<listitem id="hello_world">
161
<para>
162
create an njs script file, for example, <path>http.js</path>.
163
See <link doc="reference.xml">Reference</link>
164
for the list of njs properties and methods.
165
<example>
166
function hello(r) {
167
r.return(200, "Hello world!");
168
}
169
170
export default {hello};
171
</example>
172
</para>
173
</listitem>
174
175
<listitem>
176
177
<para>
178
in the <path>nginx.conf</path> file, enable
179
<link doc="../http/ngx_http_js_module.xml">ngx_http_js_module</link> module
180
and specify the
181
<link doc="../http/ngx_http_js_module.xml" id="js_import">js_import</link>
182
directive
183
with the <path>http.js</path> script file:
184
<example>
185
load_module modules/ngx_http_js_module.so;
186
187
events {}
188
189
http {
190
# since 0.9.1
191
js_engine qjs;
192
193
js_import http.js;
194
195
server {
196
listen 8000;
197
198
location / {
199
js_content http.hello;
200
}
201
}
202
}
203
</example>
204
</para>
205
</listitem>
206
207
</list>
208
There is also a standalone <link doc="cli.xml">command line</link> utility
209
that can be used independently of nginx for njs development and debugging.
210
</para>
211
212
</section>
213
214
215
<section id="tested_os_and_platforms"
216
name="Tested OS and platforms">
217
218
<para>
219
<list type="bullet">
220
221
<listitem>
222
FreeBSD / amd64;
223
</listitem>
224
225
<listitem>
226
Linux / x86, amd64, arm64, ppc64el;
227
</listitem>
228
229
<listitem>
230
Solaris 11 / amd64;
231
</listitem>
232
233
<listitem>
234
macOS / x86_64;
235
</listitem>
236
237
</list>
238
</para>
239
240
</section>
241
242
243
<section id="presentation" name="Presentation at nginx.conf 2018">
244
245
<para><video id="Jc_L6UffFOs" /></para>
246
247
</section>
248
249
</article>
250
251