Mercurial > hg > nginx-site
comparison xml/en/docs/njs/examples.xml @ 3043:9eadb98ec770
Free nginx: removed commercial version documentation.
author | Maxim Dounin <mdounin@mdounin.ru> |
---|---|
date | Wed, 14 Feb 2024 20:05:49 +0300 |
parents | 24b379907b0f |
children |
comparison
equal
deleted
inserted
replaced
3042:19e4897acb84 | 3043:9eadb98ec770 |
---|---|
7 <!DOCTYPE article SYSTEM "../../../../dtd/article.dtd"> | 7 <!DOCTYPE article SYSTEM "../../../../dtd/article.dtd"> |
8 | 8 |
9 <article name="Examples" | 9 <article name="Examples" |
10 link="/en/docs/njs/examples.html" | 10 link="/en/docs/njs/examples.html" |
11 lang="en" | 11 lang="en" |
12 rev="21"> | 12 rev="22"> |
13 | 13 |
14 <section id="summary"> | 14 <section id="summary"> |
15 | 15 |
16 <para> | 16 <para> |
17 The examples work since | 17 The examples work since |
196 | 196 |
197 | 197 |
198 <section id="http_proxying" name="HTTP Proxying"> | 198 <section id="http_proxying" name="HTTP Proxying"> |
199 | 199 |
200 | 200 |
201 <section id="subrequest" name="Accessing API from a Subrequest"> | |
202 | |
203 <para> | |
204 <path>nginx.conf</path>: | |
205 <example> | |
206 js_import http.js; | |
207 | |
208 keyval_zone zone=foo:10m; | |
209 #... | |
210 | |
211 location /keyval { | |
212 js_content http.set_keyval; | |
213 } | |
214 | |
215 location /version { | |
216 js_content http.version; | |
217 } | |
218 | |
219 location /api { | |
220 api write=on; | |
221 } | |
222 </example> | |
223 </para> | |
224 | |
225 <para> | |
226 <path>http.js</path>: | |
227 <example> | |
228 async function set_keyval(r) { | |
229 let res = await r.subrequest('/api/7/http/keyvals/foo', | |
230 { method: 'POST', | |
231 body: JSON.stringify({ foo: 789, bar: "ss dd 00" })}); | |
232 | |
233 if (res.status >= 300) { | |
234 r.return(res.status, res.responseBody); | |
235 return; | |
236 } | |
237 | |
238 r.return(200); | |
239 } | |
240 | |
241 async function version(r) { | |
242 let res = await r.subrequest('/api/7/nginx', { method: 'GET' }); | |
243 | |
244 if (res.status != 200) { | |
245 r.return(res.status); | |
246 return; | |
247 } | |
248 | |
249 var json = JSON.parse(res.responseBody); | |
250 r.return(200, json.version); | |
251 } | |
252 | |
253 export default {set_keyval, version}; | |
254 </example> | |
255 <note> | |
256 The <link doc="../http/ngx_http_keyval_module.xml" id="keyval"/>, | |
257 <link doc="../http/ngx_http_keyval_module.xml" id="keyval_zone"/>, | |
258 and <link doc="../http/ngx_http_api_module.xml" id="api"/> directives | |
259 are available as part of our | |
260 <commercial_version>commercial subscription</commercial_version>. | |
261 </note> | |
262 </para> | |
263 | |
264 </section> | |
265 | |
266 | |
267 <section id="fast_response" name="Returning Fastest Response from Proxy"> | 201 <section id="fast_response" name="Returning Fastest Response from Proxy"> |
268 | 202 |
269 <para> | 203 <para> |
270 <path>nginx.conf</path>: | 204 <path>nginx.conf</path>: |
271 <example> | 205 <example> |
391 </example> | 325 </example> |
392 </para> | 326 </para> |
393 | 327 |
394 </section> | 328 </section> |
395 | 329 |
396 | |
397 <section id="requests" name="Logging the Number of Requests Per Client"> | |
398 | |
399 <para> | |
400 <path>nginx.conf</path>: | |
401 <example> | |
402 js_import http.js; | |
403 | |
404 js_set $num_requests http.num_requests; | |
405 | |
406 keyval_zone zone=foo:10m; | |
407 | |
408 keyval $remote_addr $foo zone=foo; | |
409 | |
410 log_format bar '$remote_addr [$time_local] $num_requests'; | |
411 access_log logs/access.log bar; | |
412 | |
413 server { | |
414 listen 8000; | |
415 | |
416 location / { | |
417 root html; | |
418 } | |
419 } | |
420 </example> | |
421 </para> | |
422 | |
423 <para> | |
424 <path>http.js</path>: | |
425 <example> | |
426 function num_requests(r) | |
427 { | |
428 var n = r.variables.foo; | |
429 n = n ? Number(n) + 1 : 1; | |
430 r.variables.foo = n; | |
431 return n; | |
432 } | |
433 | |
434 export default {num_requests}; | |
435 </example> | |
436 <note> | |
437 The <link doc="../http/ngx_http_keyval_module.xml" id="keyval"/> and | |
438 <link doc="../http/ngx_http_keyval_module.xml" id="keyval_zone"/> directives | |
439 are available as part of our | |
440 <commercial_version>commercial subscription</commercial_version>. | |
441 </note> | |
442 </para> | |
443 | |
444 </section> | |
445 | |
446 </section> | 330 </section> |
447 | 331 |
448 </article> | 332 </article> |