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