Mercurial > hg > nginx-site
comparison xml/en/docs/njs/examples.xml @ 2642:c60bcc0de435
Updated TOC in njs examples.
author | Yaroslav Zhuravlev <yar@nginx.com> |
---|---|
date | Wed, 20 Jan 2021 09:34:32 +0000 |
parents | 885bcb41fbcf |
children | 4849fa0fd4b4 |
comparison
equal
deleted
inserted
replaced
2641:ea9f4dc0c801 | 2642:c60bcc0de435 |
---|---|
54 </para> | 54 </para> |
55 | 55 |
56 </section> | 56 </section> |
57 | 57 |
58 | 58 |
59 <section id="urldecode" name="URL Decoding"> | 59 <section id="http_auth" name="HTTP Аuthorization"> |
60 | |
61 <para> | |
62 <path>nginx.conf</path>: | |
63 <example> | |
64 js_import http.js; | |
65 | |
66 js_set $decoded_foo http.decoded_foo; | |
67 </example> | |
68 </para> | |
69 | |
70 <para> | |
71 <path>http.js</path>: | |
72 <example> | |
73 function decoded_foo(r) { | |
74 return decodeURIComponent(r.args.foo); | |
75 } | |
76 | |
77 export default {decoded_foo}; | |
78 </example> | |
79 </para> | |
80 | |
81 </section> | |
82 | |
83 | |
84 <section id="urlencode" name="URL Encoding"> | |
85 | |
86 <para> | |
87 <path>nginx.conf</path>: | |
88 <example> | |
89 js_import http.js; | |
90 | |
91 js_set $encoded_foo http.encoded_foo; | |
92 ... | |
93 | |
94 location / { | |
95 proxy_pass http://example.com?foo=$encoded_foo; | |
96 } | |
97 </example> | |
98 </para> | |
99 | |
100 <para> | |
101 <path>http.js</path>: | |
102 <example> | |
103 function encoded_foo(r) { | |
104 return encodeURIComponent('foo & bar?'); | |
105 } | |
106 | |
107 export default {encoded_foo}; | |
108 </example> | |
109 </para> | |
110 | |
111 </section> | |
112 | |
113 | |
114 <section id="redirect" name="Internal Redirect"> | |
115 | |
116 <para> | |
117 <path>nginx.conf</path>: | |
118 <example> | |
119 js_import http.js; | |
120 | |
121 location /redirect { | |
122 js_content http.redirect; | |
123 } | |
124 | |
125 location @named { | |
126 return 200 named; | |
127 } | |
128 </example> | |
129 </para> | |
130 | |
131 <para> | |
132 <path>http.js</path>: | |
133 <example> | |
134 function redirect(r) { | |
135 r.internalRedirect('@named'); | |
136 } | |
137 | |
138 export default {redirect}; | |
139 </example> | |
140 </para> | |
141 | |
142 </section> | |
143 | |
144 | |
145 <section id="fast_response" name="Returning Fastest Response from Proxy"> | |
146 | |
147 <para> | |
148 <path>nginx.conf</path>: | |
149 <example> | |
150 js_import http.js; | |
151 | |
152 location /start { | |
153 js_content http.content; | |
154 } | |
155 | |
156 location /foo { | |
157 proxy_pass http://backend1; | |
158 } | |
159 | |
160 location /bar { | |
161 proxy_pass http://backend2; | |
162 } | |
163 </example> | |
164 </para> | |
165 | |
166 <para> | |
167 <path>http.js</path>: | |
168 <example> | |
169 function content(r) { | |
170 var n = 0; | |
171 | |
172 function done(res) { | |
173 if (n++ == 0) { | |
174 r.return(res.status, res.responseBody); | |
175 } | |
176 } | |
177 | |
178 r.subrequest('/foo', r.variables.args, done); | |
179 r.subrequest('/bar', r.variables.args, done); | |
180 } | |
181 | |
182 export default {content}; | |
183 </example> | |
184 </para> | |
185 | |
186 </section> | |
187 | 60 |
188 | 61 |
189 <section id="jwt" name="Creating HS JWT"> | 62 <section id="jwt" name="Creating HS JWT"> |
190 | 63 |
191 <para> | 64 <para> |
230 </para> | 103 </para> |
231 | 104 |
232 </section> | 105 </section> |
233 | 106 |
234 | 107 |
108 <section id="secure_link" name="Creating secure_link Hash"> | |
109 | |
110 <para> | |
111 <path>nginx.conf</path>: | |
112 <example> | |
113 js_import http.js; | |
114 | |
115 js_set $new_foo http.create_secure_link; | |
116 ... | |
117 | |
118 location / { | |
119 secure_link $cookie_foo; | |
120 secure_link_md5 "$uri mykey"; | |
121 ... | |
122 } | |
123 | |
124 location @login { | |
125 add_header Set-Cookie "foo=$new_foo; Max-Age=60"; | |
126 return 302 /; | |
127 } | |
128 </example> | |
129 </para> | |
130 | |
131 <para> | |
132 <path>http.js</path>: | |
133 <example> | |
134 function create_secure_link(r) { | |
135 return require('crypto').createHash('md5') | |
136 .update(r.uri).update(" mykey") | |
137 .digest('base64url'); | |
138 } | |
139 | |
140 export default {create_secure_link}; | |
141 </example> | |
142 </para> | |
143 | |
144 </section> | |
145 | |
146 | |
235 <section id="jwt_field" name="Getting Arbitrary Field from JWT | 147 <section id="jwt_field" name="Getting Arbitrary Field from JWT |
236 as nginx Variable"> | 148 as nginx Variable"> |
237 | 149 |
238 <para> | 150 <para> |
239 <path>nginx.conf</path>: | 151 <path>nginx.conf</path>: |
269 export default {jwt_payload_sub} | 181 export default {jwt_payload_sub} |
270 </example> | 182 </example> |
271 </para> | 183 </para> |
272 | 184 |
273 </section> | 185 </section> |
186 | |
187 </section> | |
188 | |
189 | |
190 <section id="http_proxying" name="HTTP Proxying"> | |
274 | 191 |
275 | 192 |
276 <section id="subrequest" name="Accessing API from a Subrequest"> | 193 <section id="subrequest" name="Accessing API from a Subrequest"> |
277 | 194 |
278 <para> | 195 <para> |
331 </para> | 248 </para> |
332 | 249 |
333 </section> | 250 </section> |
334 | 251 |
335 | 252 |
336 <section id="secure_link" name="Creating secure_link Hash"> | 253 <section id="fast_response" name="Returning Fastest Response from Proxy"> |
337 | 254 |
338 <para> | 255 <para> |
339 <path>nginx.conf</path>: | 256 <path>nginx.conf</path>: |
340 <example> | 257 <example> |
341 js_import http.js; | 258 js_import http.js; |
342 | 259 |
343 js_set $new_foo http.create_secure_link; | 260 location /start { |
344 ... | 261 js_content http.content; |
345 | 262 } |
346 location / { | 263 |
347 secure_link $cookie_foo; | 264 location /foo { |
348 secure_link_md5 "$uri mykey"; | 265 proxy_pass http://backend1; |
349 ... | 266 } |
350 } | 267 |
351 | 268 location /bar { |
352 location @login { | 269 proxy_pass http://backend2; |
353 add_header Set-Cookie "foo=$new_foo; Max-Age=60"; | 270 } |
354 return 302 /; | 271 </example> |
355 } | 272 </para> |
356 </example> | 273 |
357 </para> | 274 <para> |
358 | 275 <path>http.js</path>: |
359 <para> | 276 <example> |
360 <path>http.js</path>: | 277 function content(r) { |
361 <example> | 278 var n = 0; |
362 function create_secure_link(r) { | 279 |
363 return require('crypto').createHash('md5') | 280 function done(res) { |
364 .update(r.uri).update(" mykey") | 281 if (n++ == 0) { |
365 .digest('base64url'); | 282 r.return(res.status, res.responseBody); |
366 } | 283 } |
367 | |
368 export default {create_secure_link}; | |
369 </example> | |
370 </para> | |
371 | |
372 </section> | |
373 | |
374 | |
375 <section id="requests" name="Logging the Number of Requests Per Client"> | |
376 | |
377 <para> | |
378 <path>nginx.conf</path>: | |
379 <example> | |
380 js_import http.js; | |
381 | |
382 js_set $num_requests http.num_requests; | |
383 | |
384 keyval_zone zone=foo:10m; | |
385 | |
386 keyval $remote_addr $foo zone=foo; | |
387 | |
388 log_format bar '$remote_addr [$time_local] $num_requests'; | |
389 access_log logs/access.log bar; | |
390 | |
391 server { | |
392 listen 8000; | |
393 | |
394 location / { | |
395 root html; | |
396 } | 284 } |
397 } | 285 |
398 </example> | 286 r.subrequest('/foo', r.variables.args, done); |
399 </para> | 287 r.subrequest('/bar', r.variables.args, done); |
400 | 288 } |
401 <para> | 289 |
402 <path>http.js</path>: | 290 export default {content}; |
403 <example> | 291 </example> |
404 function num_requests(r) | |
405 { | |
406 var n = r.variables.foo; | |
407 n = n ? Number(n) + 1 : 1; | |
408 r.variables.foo = n; | |
409 return n; | |
410 } | |
411 | |
412 export default {num_requests}; | |
413 </example> | |
414 <note> | |
415 The <link doc="../http/ngx_http_keyval_module.xml" id="keyval"/> and | |
416 <link doc="../http/ngx_http_keyval_module.xml" id="keyval_zone"/> directives | |
417 are available as part of our | |
418 <commercial_version>commercial subscription</commercial_version>. | |
419 </note> | |
420 </para> | 292 </para> |
421 | 293 |
422 </section> | 294 </section> |
423 | 295 |
424 | 296 |
466 </example> | 338 </example> |
467 </para> | 339 </para> |
468 | 340 |
469 </section> | 341 </section> |
470 | 342 |
343 </section> | |
344 | |
345 | |
346 <section id="misc" name="Miscellaneous"> | |
347 | |
348 | |
349 <section id="redirect" name="Internal Redirect"> | |
350 | |
351 <para> | |
352 <path>nginx.conf</path>: | |
353 <example> | |
354 js_import http.js; | |
355 | |
356 location /redirect { | |
357 js_content http.redirect; | |
358 } | |
359 | |
360 location @named { | |
361 return 200 named; | |
362 } | |
363 </example> | |
364 </para> | |
365 | |
366 <para> | |
367 <path>http.js</path>: | |
368 <example> | |
369 function redirect(r) { | |
370 r.internalRedirect('@named'); | |
371 } | |
372 | |
373 export default {redirect}; | |
374 </example> | |
375 </para> | |
376 | |
377 </section> | |
378 | |
379 | |
380 <section id="requests" name="Logging the Number of Requests Per Client"> | |
381 | |
382 <para> | |
383 <path>nginx.conf</path>: | |
384 <example> | |
385 js_import http.js; | |
386 | |
387 js_set $num_requests http.num_requests; | |
388 | |
389 keyval_zone zone=foo:10m; | |
390 | |
391 keyval $remote_addr $foo zone=foo; | |
392 | |
393 log_format bar '$remote_addr [$time_local] $num_requests'; | |
394 access_log logs/access.log bar; | |
395 | |
396 server { | |
397 listen 8000; | |
398 | |
399 location / { | |
400 root html; | |
401 } | |
402 } | |
403 </example> | |
404 </para> | |
405 | |
406 <para> | |
407 <path>http.js</path>: | |
408 <example> | |
409 function num_requests(r) | |
410 { | |
411 var n = r.variables.foo; | |
412 n = n ? Number(n) + 1 : 1; | |
413 r.variables.foo = n; | |
414 return n; | |
415 } | |
416 | |
417 export default {num_requests}; | |
418 </example> | |
419 <note> | |
420 The <link doc="../http/ngx_http_keyval_module.xml" id="keyval"/> and | |
421 <link doc="../http/ngx_http_keyval_module.xml" id="keyval_zone"/> directives | |
422 are available as part of our | |
423 <commercial_version>commercial subscription</commercial_version>. | |
424 </note> | |
425 </para> | |
426 | |
427 </section> | |
428 | |
429 <section id="urldecode" name="URL Decoding"> | |
430 | |
431 <para> | |
432 <path>nginx.conf</path>: | |
433 <example> | |
434 js_import http.js; | |
435 | |
436 js_set $decoded_foo http.decoded_foo; | |
437 </example> | |
438 </para> | |
439 | |
440 <para> | |
441 <path>http.js</path>: | |
442 <example> | |
443 function decoded_foo(r) { | |
444 return decodeURIComponent(r.args.foo); | |
445 } | |
446 | |
447 export default {decoded_foo}; | |
448 </example> | |
449 </para> | |
450 | |
451 </section> | |
452 | |
453 | |
454 <section id="urlencode" name="URL Encoding"> | |
455 | |
456 <para> | |
457 <path>nginx.conf</path>: | |
458 <example> | |
459 js_import http.js; | |
460 | |
461 js_set $encoded_foo http.encoded_foo; | |
462 ... | |
463 | |
464 location / { | |
465 proxy_pass http://example.com?foo=$encoded_foo; | |
466 } | |
467 </example> | |
468 </para> | |
469 | |
470 <para> | |
471 <path>http.js</path>: | |
472 <example> | |
473 function encoded_foo(r) { | |
474 return encodeURIComponent('foo & bar?'); | |
475 } | |
476 | |
477 export default {encoded_foo}; | |
478 </example> | |
479 </para> | |
480 | |
481 </section> | |
482 | |
483 </section> | |
484 | |
471 </article> | 485 </article> |