Mercurial > hg > nginx-site
annotate xml/ru/docs/njs/examples.xml @ 3024:25a473b55e7b
Updated with Netcraft October 2023 Web Server Survey stats.
author | Yaroslav Zhuravlev <yar@nginx.com> |
---|---|
date | Thu, 02 Nov 2023 09:07:56 +0000 |
parents | 24b379907b0f |
children | 9eadb98ec770 |
rev | line source |
---|---|
2245
87a0e2c73a25
Refactored njs documentation.
Yaroslav Zhuravlev <yar@nginx.com>
parents:
diff
changeset
|
1 <?xml version="1.0"?> |
87a0e2c73a25
Refactored njs documentation.
Yaroslav Zhuravlev <yar@nginx.com>
parents:
diff
changeset
|
2 |
87a0e2c73a25
Refactored njs documentation.
Yaroslav Zhuravlev <yar@nginx.com>
parents:
diff
changeset
|
3 <!-- |
87a0e2c73a25
Refactored njs documentation.
Yaroslav Zhuravlev <yar@nginx.com>
parents:
diff
changeset
|
4 Copyright (C) Nginx, Inc. |
87a0e2c73a25
Refactored njs documentation.
Yaroslav Zhuravlev <yar@nginx.com>
parents:
diff
changeset
|
5 --> |
87a0e2c73a25
Refactored njs documentation.
Yaroslav Zhuravlev <yar@nginx.com>
parents:
diff
changeset
|
6 |
87a0e2c73a25
Refactored njs documentation.
Yaroslav Zhuravlev <yar@nginx.com>
parents:
diff
changeset
|
7 <!DOCTYPE article SYSTEM "../../../../dtd/article.dtd"> |
87a0e2c73a25
Refactored njs documentation.
Yaroslav Zhuravlev <yar@nginx.com>
parents:
diff
changeset
|
8 |
87a0e2c73a25
Refactored njs documentation.
Yaroslav Zhuravlev <yar@nginx.com>
parents:
diff
changeset
|
9 <article name="Примеры использования" |
87a0e2c73a25
Refactored njs documentation.
Yaroslav Zhuravlev <yar@nginx.com>
parents:
diff
changeset
|
10 link="/ru/docs/njs/examples.html" |
87a0e2c73a25
Refactored njs documentation.
Yaroslav Zhuravlev <yar@nginx.com>
parents:
diff
changeset
|
11 lang="ru" |
2799
24b379907b0f
Added async and await to njs Examples.
Yaroslav Zhuravlev <yar@nginx.com>
parents:
2780
diff
changeset
|
12 rev="21"> |
2531
9c8a89d3876f
Updated njs examples with js_import and corresponding changes.
Yaroslav Zhuravlev <yar@nginx.com>
parents:
2527
diff
changeset
|
13 |
9c8a89d3876f
Updated njs examples with js_import and corresponding changes.
Yaroslav Zhuravlev <yar@nginx.com>
parents:
2527
diff
changeset
|
14 <section id="summary"> |
9c8a89d3876f
Updated njs examples with js_import and corresponding changes.
Yaroslav Zhuravlev <yar@nginx.com>
parents:
2527
diff
changeset
|
15 |
9c8a89d3876f
Updated njs examples with js_import and corresponding changes.
Yaroslav Zhuravlev <yar@nginx.com>
parents:
2527
diff
changeset
|
16 <para> |
9c8a89d3876f
Updated njs examples with js_import and corresponding changes.
Yaroslav Zhuravlev <yar@nginx.com>
parents:
2527
diff
changeset
|
17 Примеры работают начиная с версии |
9c8a89d3876f
Updated njs examples with js_import and corresponding changes.
Yaroslav Zhuravlev <yar@nginx.com>
parents:
2527
diff
changeset
|
18 <link doc="../njs/changes.xml" id="njs0.4.0">0.4.0</link>. |
9c8a89d3876f
Updated njs examples with js_import and corresponding changes.
Yaroslav Zhuravlev <yar@nginx.com>
parents:
2527
diff
changeset
|
19 </para> |
9c8a89d3876f
Updated njs examples with js_import and corresponding changes.
Yaroslav Zhuravlev <yar@nginx.com>
parents:
2527
diff
changeset
|
20 |
9c8a89d3876f
Updated njs examples with js_import and corresponding changes.
Yaroslav Zhuravlev <yar@nginx.com>
parents:
2527
diff
changeset
|
21 </section> |
2245
87a0e2c73a25
Refactored njs documentation.
Yaroslav Zhuravlev <yar@nginx.com>
parents:
diff
changeset
|
22 |
2643
4849fa0fd4b4
Removed URL Decoding/Encoding examples from njs.
Yaroslav Zhuravlev <yar@nginx.com>
parents:
2642
diff
changeset
|
23 |
2245
87a0e2c73a25
Refactored njs documentation.
Yaroslav Zhuravlev <yar@nginx.com>
parents:
diff
changeset
|
24 <section id="helloword" name="Hello World"> |
87a0e2c73a25
Refactored njs documentation.
Yaroslav Zhuravlev <yar@nginx.com>
parents:
diff
changeset
|
25 |
87a0e2c73a25
Refactored njs documentation.
Yaroslav Zhuravlev <yar@nginx.com>
parents:
diff
changeset
|
26 <para> |
87a0e2c73a25
Refactored njs documentation.
Yaroslav Zhuravlev <yar@nginx.com>
parents:
diff
changeset
|
27 <path>nginx.conf</path>: |
87a0e2c73a25
Refactored njs documentation.
Yaroslav Zhuravlev <yar@nginx.com>
parents:
diff
changeset
|
28 <example> |
87a0e2c73a25
Refactored njs documentation.
Yaroslav Zhuravlev <yar@nginx.com>
parents:
diff
changeset
|
29 events {} |
87a0e2c73a25
Refactored njs documentation.
Yaroslav Zhuravlev <yar@nginx.com>
parents:
diff
changeset
|
30 |
87a0e2c73a25
Refactored njs documentation.
Yaroslav Zhuravlev <yar@nginx.com>
parents:
diff
changeset
|
31 http { |
2531
9c8a89d3876f
Updated njs examples with js_import and corresponding changes.
Yaroslav Zhuravlev <yar@nginx.com>
parents:
2527
diff
changeset
|
32 js_import http.js; |
2556
c81b1fadbcf3
Updated hello_world example in njs.
Yaroslav Zhuravlev <yar@nginx.com>
parents:
2536
diff
changeset
|
33 |
c81b1fadbcf3
Updated hello_world example in njs.
Yaroslav Zhuravlev <yar@nginx.com>
parents:
2536
diff
changeset
|
34 server { |
c81b1fadbcf3
Updated hello_world example in njs.
Yaroslav Zhuravlev <yar@nginx.com>
parents:
2536
diff
changeset
|
35 listen 8000; |
c81b1fadbcf3
Updated hello_world example in njs.
Yaroslav Zhuravlev <yar@nginx.com>
parents:
2536
diff
changeset
|
36 |
c81b1fadbcf3
Updated hello_world example in njs.
Yaroslav Zhuravlev <yar@nginx.com>
parents:
2536
diff
changeset
|
37 location / { |
c81b1fadbcf3
Updated hello_world example in njs.
Yaroslav Zhuravlev <yar@nginx.com>
parents:
2536
diff
changeset
|
38 js_content http.hello; |
c81b1fadbcf3
Updated hello_world example in njs.
Yaroslav Zhuravlev <yar@nginx.com>
parents:
2536
diff
changeset
|
39 } |
c81b1fadbcf3
Updated hello_world example in njs.
Yaroslav Zhuravlev <yar@nginx.com>
parents:
2536
diff
changeset
|
40 } |
2245
87a0e2c73a25
Refactored njs documentation.
Yaroslav Zhuravlev <yar@nginx.com>
parents:
diff
changeset
|
41 } |
87a0e2c73a25
Refactored njs documentation.
Yaroslav Zhuravlev <yar@nginx.com>
parents:
diff
changeset
|
42 </example> |
87a0e2c73a25
Refactored njs documentation.
Yaroslav Zhuravlev <yar@nginx.com>
parents:
diff
changeset
|
43 </para> |
87a0e2c73a25
Refactored njs documentation.
Yaroslav Zhuravlev <yar@nginx.com>
parents:
diff
changeset
|
44 |
87a0e2c73a25
Refactored njs documentation.
Yaroslav Zhuravlev <yar@nginx.com>
parents:
diff
changeset
|
45 <para> |
2531
9c8a89d3876f
Updated njs examples with js_import and corresponding changes.
Yaroslav Zhuravlev <yar@nginx.com>
parents:
2527
diff
changeset
|
46 <literal>http.js</literal>: |
2245
87a0e2c73a25
Refactored njs documentation.
Yaroslav Zhuravlev <yar@nginx.com>
parents:
diff
changeset
|
47 <example> |
87a0e2c73a25
Refactored njs documentation.
Yaroslav Zhuravlev <yar@nginx.com>
parents:
diff
changeset
|
48 function hello(r) { |
2257
8cef5ef98336
Changed quotes to ASCII in NJS example.
Nick Shadrin <nick@nginx.com>
parents:
2246
diff
changeset
|
49 r.return(200, "Hello world!"); |
2245
87a0e2c73a25
Refactored njs documentation.
Yaroslav Zhuravlev <yar@nginx.com>
parents:
diff
changeset
|
50 } |
2531
9c8a89d3876f
Updated njs examples with js_import and corresponding changes.
Yaroslav Zhuravlev <yar@nginx.com>
parents:
2527
diff
changeset
|
51 |
9c8a89d3876f
Updated njs examples with js_import and corresponding changes.
Yaroslav Zhuravlev <yar@nginx.com>
parents:
2527
diff
changeset
|
52 export default {hello}; |
2245
87a0e2c73a25
Refactored njs documentation.
Yaroslav Zhuravlev <yar@nginx.com>
parents:
diff
changeset
|
53 </example> |
87a0e2c73a25
Refactored njs documentation.
Yaroslav Zhuravlev <yar@nginx.com>
parents:
diff
changeset
|
54 </para> |
87a0e2c73a25
Refactored njs documentation.
Yaroslav Zhuravlev <yar@nginx.com>
parents:
diff
changeset
|
55 |
87a0e2c73a25
Refactored njs documentation.
Yaroslav Zhuravlev <yar@nginx.com>
parents:
diff
changeset
|
56 </section> |
87a0e2c73a25
Refactored njs documentation.
Yaroslav Zhuravlev <yar@nginx.com>
parents:
diff
changeset
|
57 |
87a0e2c73a25
Refactored njs documentation.
Yaroslav Zhuravlev <yar@nginx.com>
parents:
diff
changeset
|
58 |
2642
c60bcc0de435
Updated TOC in njs examples.
Yaroslav Zhuravlev <yar@nginx.com>
parents:
2640
diff
changeset
|
59 <section id="http_auth" name="HTTP-авторизация"> |
2245
87a0e2c73a25
Refactored njs documentation.
Yaroslav Zhuravlev <yar@nginx.com>
parents:
diff
changeset
|
60 |
87a0e2c73a25
Refactored njs documentation.
Yaroslav Zhuravlev <yar@nginx.com>
parents:
diff
changeset
|
61 |
87a0e2c73a25
Refactored njs documentation.
Yaroslav Zhuravlev <yar@nginx.com>
parents:
diff
changeset
|
62 <section id="jwt" name="Создание HS JWT"> |
87a0e2c73a25
Refactored njs documentation.
Yaroslav Zhuravlev <yar@nginx.com>
parents:
diff
changeset
|
63 |
87a0e2c73a25
Refactored njs documentation.
Yaroslav Zhuravlev <yar@nginx.com>
parents:
diff
changeset
|
64 <para> |
87a0e2c73a25
Refactored njs documentation.
Yaroslav Zhuravlev <yar@nginx.com>
parents:
diff
changeset
|
65 <path>nginx.conf</path>: |
87a0e2c73a25
Refactored njs documentation.
Yaroslav Zhuravlev <yar@nginx.com>
parents:
diff
changeset
|
66 <example> |
2531
9c8a89d3876f
Updated njs examples with js_import and corresponding changes.
Yaroslav Zhuravlev <yar@nginx.com>
parents:
2527
diff
changeset
|
67 js_import http.js; |
2245
87a0e2c73a25
Refactored njs documentation.
Yaroslav Zhuravlev <yar@nginx.com>
parents:
diff
changeset
|
68 |
2531
9c8a89d3876f
Updated njs examples with js_import and corresponding changes.
Yaroslav Zhuravlev <yar@nginx.com>
parents:
2527
diff
changeset
|
69 js_set $jwt http.jwt; |
2245
87a0e2c73a25
Refactored njs documentation.
Yaroslav Zhuravlev <yar@nginx.com>
parents:
diff
changeset
|
70 </example> |
87a0e2c73a25
Refactored njs documentation.
Yaroslav Zhuravlev <yar@nginx.com>
parents:
diff
changeset
|
71 </para> |
87a0e2c73a25
Refactored njs documentation.
Yaroslav Zhuravlev <yar@nginx.com>
parents:
diff
changeset
|
72 |
87a0e2c73a25
Refactored njs documentation.
Yaroslav Zhuravlev <yar@nginx.com>
parents:
diff
changeset
|
73 <para> |
2531
9c8a89d3876f
Updated njs examples with js_import and corresponding changes.
Yaroslav Zhuravlev <yar@nginx.com>
parents:
2527
diff
changeset
|
74 <path>http.js</path>: |
2245
87a0e2c73a25
Refactored njs documentation.
Yaroslav Zhuravlev <yar@nginx.com>
parents:
diff
changeset
|
75 <example> |
2519
1cd0abf8f1e5
Updated JWT example in njs.
Yaroslav Zhuravlev <yar@nginx.com>
parents:
2517
diff
changeset
|
76 function generate_hs256_jwt(claims, key, valid) { |
2527
351a669a576d
Corrected JWT example in njs.
Yaroslav Zhuravlev <yar@nginx.com>
parents:
2520
diff
changeset
|
77 var header = { typ: "JWT", alg: "HS256" }; |
351a669a576d
Corrected JWT example in njs.
Yaroslav Zhuravlev <yar@nginx.com>
parents:
2520
diff
changeset
|
78 var claims = Object.assign(claims, {exp: Math.floor(Date.now()/1000) + valid}); |
2245
87a0e2c73a25
Refactored njs documentation.
Yaroslav Zhuravlev <yar@nginx.com>
parents:
diff
changeset
|
79 |
2519
1cd0abf8f1e5
Updated JWT example in njs.
Yaroslav Zhuravlev <yar@nginx.com>
parents:
2517
diff
changeset
|
80 var s = [header, claims].map(JSON.stringify) |
1cd0abf8f1e5
Updated JWT example in njs.
Yaroslav Zhuravlev <yar@nginx.com>
parents:
2517
diff
changeset
|
81 .map(v=>v.toString('base64url')) |
1cd0abf8f1e5
Updated JWT example in njs.
Yaroslav Zhuravlev <yar@nginx.com>
parents:
2517
diff
changeset
|
82 .join('.'); |
2245
87a0e2c73a25
Refactored njs documentation.
Yaroslav Zhuravlev <yar@nginx.com>
parents:
diff
changeset
|
83 |
87a0e2c73a25
Refactored njs documentation.
Yaroslav Zhuravlev <yar@nginx.com>
parents:
diff
changeset
|
84 var h = require('crypto').createHmac('sha256', key); |
87a0e2c73a25
Refactored njs documentation.
Yaroslav Zhuravlev <yar@nginx.com>
parents:
diff
changeset
|
85 |
2639
7ad967af16b8
Updated HS JWT example in njs.
Yaroslav Zhuravlev <yar@nginx.com>
parents:
2556
diff
changeset
|
86 return s + '.' + h.update(s).digest('base64url'); |
2245
87a0e2c73a25
Refactored njs documentation.
Yaroslav Zhuravlev <yar@nginx.com>
parents:
diff
changeset
|
87 } |
87a0e2c73a25
Refactored njs documentation.
Yaroslav Zhuravlev <yar@nginx.com>
parents:
diff
changeset
|
88 |
87a0e2c73a25
Refactored njs documentation.
Yaroslav Zhuravlev <yar@nginx.com>
parents:
diff
changeset
|
89 function jwt(r) { |
87a0e2c73a25
Refactored njs documentation.
Yaroslav Zhuravlev <yar@nginx.com>
parents:
diff
changeset
|
90 var claims = { |
2519
1cd0abf8f1e5
Updated JWT example in njs.
Yaroslav Zhuravlev <yar@nginx.com>
parents:
2517
diff
changeset
|
91 iss: "nginx", |
1cd0abf8f1e5
Updated JWT example in njs.
Yaroslav Zhuravlev <yar@nginx.com>
parents:
2517
diff
changeset
|
92 sub: "alice", |
1cd0abf8f1e5
Updated JWT example in njs.
Yaroslav Zhuravlev <yar@nginx.com>
parents:
2517
diff
changeset
|
93 foo: 123, |
1cd0abf8f1e5
Updated JWT example in njs.
Yaroslav Zhuravlev <yar@nginx.com>
parents:
2517
diff
changeset
|
94 bar: "qq", |
1cd0abf8f1e5
Updated JWT example in njs.
Yaroslav Zhuravlev <yar@nginx.com>
parents:
2517
diff
changeset
|
95 zyx: false |
2245
87a0e2c73a25
Refactored njs documentation.
Yaroslav Zhuravlev <yar@nginx.com>
parents:
diff
changeset
|
96 }; |
87a0e2c73a25
Refactored njs documentation.
Yaroslav Zhuravlev <yar@nginx.com>
parents:
diff
changeset
|
97 |
2519
1cd0abf8f1e5
Updated JWT example in njs.
Yaroslav Zhuravlev <yar@nginx.com>
parents:
2517
diff
changeset
|
98 return generate_hs256_jwt(claims, 'foo', 600); |
2245
87a0e2c73a25
Refactored njs documentation.
Yaroslav Zhuravlev <yar@nginx.com>
parents:
diff
changeset
|
99 } |
2531
9c8a89d3876f
Updated njs examples with js_import and corresponding changes.
Yaroslav Zhuravlev <yar@nginx.com>
parents:
2527
diff
changeset
|
100 |
9c8a89d3876f
Updated njs examples with js_import and corresponding changes.
Yaroslav Zhuravlev <yar@nginx.com>
parents:
2527
diff
changeset
|
101 export default {jwt}; |
2245
87a0e2c73a25
Refactored njs documentation.
Yaroslav Zhuravlev <yar@nginx.com>
parents:
diff
changeset
|
102 </example> |
87a0e2c73a25
Refactored njs documentation.
Yaroslav Zhuravlev <yar@nginx.com>
parents:
diff
changeset
|
103 </para> |
87a0e2c73a25
Refactored njs documentation.
Yaroslav Zhuravlev <yar@nginx.com>
parents:
diff
changeset
|
104 |
87a0e2c73a25
Refactored njs documentation.
Yaroslav Zhuravlev <yar@nginx.com>
parents:
diff
changeset
|
105 </section> |
87a0e2c73a25
Refactored njs documentation.
Yaroslav Zhuravlev <yar@nginx.com>
parents:
diff
changeset
|
106 |
87a0e2c73a25
Refactored njs documentation.
Yaroslav Zhuravlev <yar@nginx.com>
parents:
diff
changeset
|
107 |
2642
c60bcc0de435
Updated TOC in njs examples.
Yaroslav Zhuravlev <yar@nginx.com>
parents:
2640
diff
changeset
|
108 <section id="secure_link" name="Создание secure_link хэша"> |
c60bcc0de435
Updated TOC in njs examples.
Yaroslav Zhuravlev <yar@nginx.com>
parents:
2640
diff
changeset
|
109 |
c60bcc0de435
Updated TOC in njs examples.
Yaroslav Zhuravlev <yar@nginx.com>
parents:
2640
diff
changeset
|
110 <para> |
c60bcc0de435
Updated TOC in njs examples.
Yaroslav Zhuravlev <yar@nginx.com>
parents:
2640
diff
changeset
|
111 <path>nginx.conf</path>: |
c60bcc0de435
Updated TOC in njs examples.
Yaroslav Zhuravlev <yar@nginx.com>
parents:
2640
diff
changeset
|
112 <example> |
c60bcc0de435
Updated TOC in njs examples.
Yaroslav Zhuravlev <yar@nginx.com>
parents:
2640
diff
changeset
|
113 js_import http.js; |
c60bcc0de435
Updated TOC in njs examples.
Yaroslav Zhuravlev <yar@nginx.com>
parents:
2640
diff
changeset
|
114 |
c60bcc0de435
Updated TOC in njs examples.
Yaroslav Zhuravlev <yar@nginx.com>
parents:
2640
diff
changeset
|
115 js_set $new_foo http.create_secure_link; |
2778
9cafae0b7ef3
Minor fixes in njs examples.
Yaroslav Zhuravlev <yar@nginx.com>
parents:
2643
diff
changeset
|
116 #... |
2642
c60bcc0de435
Updated TOC in njs examples.
Yaroslav Zhuravlev <yar@nginx.com>
parents:
2640
diff
changeset
|
117 |
c60bcc0de435
Updated TOC in njs examples.
Yaroslav Zhuravlev <yar@nginx.com>
parents:
2640
diff
changeset
|
118 location / { |
c60bcc0de435
Updated TOC in njs examples.
Yaroslav Zhuravlev <yar@nginx.com>
parents:
2640
diff
changeset
|
119 secure_link $cookie_foo; |
c60bcc0de435
Updated TOC in njs examples.
Yaroslav Zhuravlev <yar@nginx.com>
parents:
2640
diff
changeset
|
120 secure_link_md5 "$uri mykey"; |
2778
9cafae0b7ef3
Minor fixes in njs examples.
Yaroslav Zhuravlev <yar@nginx.com>
parents:
2643
diff
changeset
|
121 #... |
2642
c60bcc0de435
Updated TOC in njs examples.
Yaroslav Zhuravlev <yar@nginx.com>
parents:
2640
diff
changeset
|
122 } |
c60bcc0de435
Updated TOC in njs examples.
Yaroslav Zhuravlev <yar@nginx.com>
parents:
2640
diff
changeset
|
123 |
c60bcc0de435
Updated TOC in njs examples.
Yaroslav Zhuravlev <yar@nginx.com>
parents:
2640
diff
changeset
|
124 location @login { |
c60bcc0de435
Updated TOC in njs examples.
Yaroslav Zhuravlev <yar@nginx.com>
parents:
2640
diff
changeset
|
125 add_header Set-Cookie "foo=$new_foo; Max-Age=60"; |
c60bcc0de435
Updated TOC in njs examples.
Yaroslav Zhuravlev <yar@nginx.com>
parents:
2640
diff
changeset
|
126 return 302 /; |
c60bcc0de435
Updated TOC in njs examples.
Yaroslav Zhuravlev <yar@nginx.com>
parents:
2640
diff
changeset
|
127 } |
c60bcc0de435
Updated TOC in njs examples.
Yaroslav Zhuravlev <yar@nginx.com>
parents:
2640
diff
changeset
|
128 </example> |
c60bcc0de435
Updated TOC in njs examples.
Yaroslav Zhuravlev <yar@nginx.com>
parents:
2640
diff
changeset
|
129 </para> |
c60bcc0de435
Updated TOC in njs examples.
Yaroslav Zhuravlev <yar@nginx.com>
parents:
2640
diff
changeset
|
130 |
c60bcc0de435
Updated TOC in njs examples.
Yaroslav Zhuravlev <yar@nginx.com>
parents:
2640
diff
changeset
|
131 <para> |
c60bcc0de435
Updated TOC in njs examples.
Yaroslav Zhuravlev <yar@nginx.com>
parents:
2640
diff
changeset
|
132 <path>http.js</path>: |
c60bcc0de435
Updated TOC in njs examples.
Yaroslav Zhuravlev <yar@nginx.com>
parents:
2640
diff
changeset
|
133 <example> |
c60bcc0de435
Updated TOC in njs examples.
Yaroslav Zhuravlev <yar@nginx.com>
parents:
2640
diff
changeset
|
134 function create_secure_link(r) { |
c60bcc0de435
Updated TOC in njs examples.
Yaroslav Zhuravlev <yar@nginx.com>
parents:
2640
diff
changeset
|
135 return require('crypto').createHash('md5') |
c60bcc0de435
Updated TOC in njs examples.
Yaroslav Zhuravlev <yar@nginx.com>
parents:
2640
diff
changeset
|
136 .update(r.uri).update(" mykey") |
c60bcc0de435
Updated TOC in njs examples.
Yaroslav Zhuravlev <yar@nginx.com>
parents:
2640
diff
changeset
|
137 .digest('base64url'); |
c60bcc0de435
Updated TOC in njs examples.
Yaroslav Zhuravlev <yar@nginx.com>
parents:
2640
diff
changeset
|
138 } |
c60bcc0de435
Updated TOC in njs examples.
Yaroslav Zhuravlev <yar@nginx.com>
parents:
2640
diff
changeset
|
139 |
c60bcc0de435
Updated TOC in njs examples.
Yaroslav Zhuravlev <yar@nginx.com>
parents:
2640
diff
changeset
|
140 export default {create_secure_link}; |
c60bcc0de435
Updated TOC in njs examples.
Yaroslav Zhuravlev <yar@nginx.com>
parents:
2640
diff
changeset
|
141 </example> |
c60bcc0de435
Updated TOC in njs examples.
Yaroslav Zhuravlev <yar@nginx.com>
parents:
2640
diff
changeset
|
142 </para> |
c60bcc0de435
Updated TOC in njs examples.
Yaroslav Zhuravlev <yar@nginx.com>
parents:
2640
diff
changeset
|
143 |
c60bcc0de435
Updated TOC in njs examples.
Yaroslav Zhuravlev <yar@nginx.com>
parents:
2640
diff
changeset
|
144 </section> |
c60bcc0de435
Updated TOC in njs examples.
Yaroslav Zhuravlev <yar@nginx.com>
parents:
2640
diff
changeset
|
145 |
c60bcc0de435
Updated TOC in njs examples.
Yaroslav Zhuravlev <yar@nginx.com>
parents:
2640
diff
changeset
|
146 |
2640
885bcb41fbcf
Added an example to njs examples.
Yaroslav Zhuravlev <yar@nginx.com>
parents:
2639
diff
changeset
|
147 <section id="jwt_field" name="Получение произвольного поля JWT |
885bcb41fbcf
Added an example to njs examples.
Yaroslav Zhuravlev <yar@nginx.com>
parents:
2639
diff
changeset
|
148 как значение переменной nginx"> |
885bcb41fbcf
Added an example to njs examples.
Yaroslav Zhuravlev <yar@nginx.com>
parents:
2639
diff
changeset
|
149 |
885bcb41fbcf
Added an example to njs examples.
Yaroslav Zhuravlev <yar@nginx.com>
parents:
2639
diff
changeset
|
150 <para> |
2780
bf641527bd3d
Updated jwt_field example in njs Examples.
Yaroslav Zhuravlev <yar@nginx.com>
parents:
2778
diff
changeset
|
151 В данном примере из JWT payload извлекается поле <literal>sub</literal>. |
bf641527bd3d
Updated jwt_field example in njs Examples.
Yaroslav Zhuravlev <yar@nginx.com>
parents:
2778
diff
changeset
|
152 JWT-токен извлекается из заголовка <header>Authorization</header>. |
bf641527bd3d
Updated jwt_field example in njs Examples.
Yaroslav Zhuravlev <yar@nginx.com>
parents:
2778
diff
changeset
|
153 </para> |
bf641527bd3d
Updated jwt_field example in njs Examples.
Yaroslav Zhuravlev <yar@nginx.com>
parents:
2778
diff
changeset
|
154 |
bf641527bd3d
Updated jwt_field example in njs Examples.
Yaroslav Zhuravlev <yar@nginx.com>
parents:
2778
diff
changeset
|
155 <para> |
2640
885bcb41fbcf
Added an example to njs examples.
Yaroslav Zhuravlev <yar@nginx.com>
parents:
2639
diff
changeset
|
156 <path>nginx.conf</path>: |
885bcb41fbcf
Added an example to njs examples.
Yaroslav Zhuravlev <yar@nginx.com>
parents:
2639
diff
changeset
|
157 <example> |
885bcb41fbcf
Added an example to njs examples.
Yaroslav Zhuravlev <yar@nginx.com>
parents:
2639
diff
changeset
|
158 js_import http.js; |
885bcb41fbcf
Added an example to njs examples.
Yaroslav Zhuravlev <yar@nginx.com>
parents:
2639
diff
changeset
|
159 |
2778
9cafae0b7ef3
Minor fixes in njs examples.
Yaroslav Zhuravlev <yar@nginx.com>
parents:
2643
diff
changeset
|
160 js_set $jwt_payload_sub http.jwt_payload_sub; |
2640
885bcb41fbcf
Added an example to njs examples.
Yaroslav Zhuravlev <yar@nginx.com>
parents:
2639
diff
changeset
|
161 |
885bcb41fbcf
Added an example to njs examples.
Yaroslav Zhuravlev <yar@nginx.com>
parents:
2639
diff
changeset
|
162 server { |
2778
9cafae0b7ef3
Minor fixes in njs examples.
Yaroslav Zhuravlev <yar@nginx.com>
parents:
2643
diff
changeset
|
163 #... |
2640
885bcb41fbcf
Added an example to njs examples.
Yaroslav Zhuravlev <yar@nginx.com>
parents:
2639
diff
changeset
|
164 |
885bcb41fbcf
Added an example to njs examples.
Yaroslav Zhuravlev <yar@nginx.com>
parents:
2639
diff
changeset
|
165 location /jwt { |
885bcb41fbcf
Added an example to njs examples.
Yaroslav Zhuravlev <yar@nginx.com>
parents:
2639
diff
changeset
|
166 return 200 $jwt_payload_sub; |
885bcb41fbcf
Added an example to njs examples.
Yaroslav Zhuravlev <yar@nginx.com>
parents:
2639
diff
changeset
|
167 } |
885bcb41fbcf
Added an example to njs examples.
Yaroslav Zhuravlev <yar@nginx.com>
parents:
2639
diff
changeset
|
168 } |
885bcb41fbcf
Added an example to njs examples.
Yaroslav Zhuravlev <yar@nginx.com>
parents:
2639
diff
changeset
|
169 </example> |
885bcb41fbcf
Added an example to njs examples.
Yaroslav Zhuravlev <yar@nginx.com>
parents:
2639
diff
changeset
|
170 </para> |
885bcb41fbcf
Added an example to njs examples.
Yaroslav Zhuravlev <yar@nginx.com>
parents:
2639
diff
changeset
|
171 |
885bcb41fbcf
Added an example to njs examples.
Yaroslav Zhuravlev <yar@nginx.com>
parents:
2639
diff
changeset
|
172 <para> |
885bcb41fbcf
Added an example to njs examples.
Yaroslav Zhuravlev <yar@nginx.com>
parents:
2639
diff
changeset
|
173 <path>http.js</path>: |
885bcb41fbcf
Added an example to njs examples.
Yaroslav Zhuravlev <yar@nginx.com>
parents:
2639
diff
changeset
|
174 <example> |
885bcb41fbcf
Added an example to njs examples.
Yaroslav Zhuravlev <yar@nginx.com>
parents:
2639
diff
changeset
|
175 function jwt(data) { |
885bcb41fbcf
Added an example to njs examples.
Yaroslav Zhuravlev <yar@nginx.com>
parents:
2639
diff
changeset
|
176 var parts = data.split('.').slice(0,2) |
885bcb41fbcf
Added an example to njs examples.
Yaroslav Zhuravlev <yar@nginx.com>
parents:
2639
diff
changeset
|
177 .map(v=>Buffer.from(v, 'base64url').toString()) |
885bcb41fbcf
Added an example to njs examples.
Yaroslav Zhuravlev <yar@nginx.com>
parents:
2639
diff
changeset
|
178 .map(JSON.parse); |
885bcb41fbcf
Added an example to njs examples.
Yaroslav Zhuravlev <yar@nginx.com>
parents:
2639
diff
changeset
|
179 return { headers:parts[0], payload: parts[1] }; |
885bcb41fbcf
Added an example to njs examples.
Yaroslav Zhuravlev <yar@nginx.com>
parents:
2639
diff
changeset
|
180 } |
885bcb41fbcf
Added an example to njs examples.
Yaroslav Zhuravlev <yar@nginx.com>
parents:
2639
diff
changeset
|
181 |
885bcb41fbcf
Added an example to njs examples.
Yaroslav Zhuravlev <yar@nginx.com>
parents:
2639
diff
changeset
|
182 function jwt_payload_sub(r) { |
885bcb41fbcf
Added an example to njs examples.
Yaroslav Zhuravlev <yar@nginx.com>
parents:
2639
diff
changeset
|
183 return jwt(r.headersIn.Authorization.slice(7)).payload.sub; |
2780
bf641527bd3d
Updated jwt_field example in njs Examples.
Yaroslav Zhuravlev <yar@nginx.com>
parents:
2778
diff
changeset
|
184 // в случае, если токен передаётся как аргумент "myjwt" |
bf641527bd3d
Updated jwt_field example in njs Examples.
Yaroslav Zhuravlev <yar@nginx.com>
parents:
2778
diff
changeset
|
185 // return jwt(r.args.myjwt).payload.sub; |
2640
885bcb41fbcf
Added an example to njs examples.
Yaroslav Zhuravlev <yar@nginx.com>
parents:
2639
diff
changeset
|
186 } |
885bcb41fbcf
Added an example to njs examples.
Yaroslav Zhuravlev <yar@nginx.com>
parents:
2639
diff
changeset
|
187 |
2778
9cafae0b7ef3
Minor fixes in njs examples.
Yaroslav Zhuravlev <yar@nginx.com>
parents:
2643
diff
changeset
|
188 export default {jwt_payload_sub}; |
2640
885bcb41fbcf
Added an example to njs examples.
Yaroslav Zhuravlev <yar@nginx.com>
parents:
2639
diff
changeset
|
189 </example> |
885bcb41fbcf
Added an example to njs examples.
Yaroslav Zhuravlev <yar@nginx.com>
parents:
2639
diff
changeset
|
190 </para> |
885bcb41fbcf
Added an example to njs examples.
Yaroslav Zhuravlev <yar@nginx.com>
parents:
2639
diff
changeset
|
191 |
885bcb41fbcf
Added an example to njs examples.
Yaroslav Zhuravlev <yar@nginx.com>
parents:
2639
diff
changeset
|
192 </section> |
885bcb41fbcf
Added an example to njs examples.
Yaroslav Zhuravlev <yar@nginx.com>
parents:
2639
diff
changeset
|
193 |
2642
c60bcc0de435
Updated TOC in njs examples.
Yaroslav Zhuravlev <yar@nginx.com>
parents:
2640
diff
changeset
|
194 </section> |
c60bcc0de435
Updated TOC in njs examples.
Yaroslav Zhuravlev <yar@nginx.com>
parents:
2640
diff
changeset
|
195 |
c60bcc0de435
Updated TOC in njs examples.
Yaroslav Zhuravlev <yar@nginx.com>
parents:
2640
diff
changeset
|
196 |
c60bcc0de435
Updated TOC in njs examples.
Yaroslav Zhuravlev <yar@nginx.com>
parents:
2640
diff
changeset
|
197 <section id="http_proxying" name="HTTP-проксирование"> |
c60bcc0de435
Updated TOC in njs examples.
Yaroslav Zhuravlev <yar@nginx.com>
parents:
2640
diff
changeset
|
198 |
2640
885bcb41fbcf
Added an example to njs examples.
Yaroslav Zhuravlev <yar@nginx.com>
parents:
2639
diff
changeset
|
199 |
2245
87a0e2c73a25
Refactored njs documentation.
Yaroslav Zhuravlev <yar@nginx.com>
parents:
diff
changeset
|
200 <section id="subrequest" name="Доступ к API из подзапроса"> |
87a0e2c73a25
Refactored njs documentation.
Yaroslav Zhuravlev <yar@nginx.com>
parents:
diff
changeset
|
201 |
87a0e2c73a25
Refactored njs documentation.
Yaroslav Zhuravlev <yar@nginx.com>
parents:
diff
changeset
|
202 <para> |
87a0e2c73a25
Refactored njs documentation.
Yaroslav Zhuravlev <yar@nginx.com>
parents:
diff
changeset
|
203 <path>nginx.conf</path>: |
87a0e2c73a25
Refactored njs documentation.
Yaroslav Zhuravlev <yar@nginx.com>
parents:
diff
changeset
|
204 <example> |
2531
9c8a89d3876f
Updated njs examples with js_import and corresponding changes.
Yaroslav Zhuravlev <yar@nginx.com>
parents:
2527
diff
changeset
|
205 js_import http.js; |
2245
87a0e2c73a25
Refactored njs documentation.
Yaroslav Zhuravlev <yar@nginx.com>
parents:
diff
changeset
|
206 |
87a0e2c73a25
Refactored njs documentation.
Yaroslav Zhuravlev <yar@nginx.com>
parents:
diff
changeset
|
207 keyval_zone zone=foo:10m; |
2778
9cafae0b7ef3
Minor fixes in njs examples.
Yaroslav Zhuravlev <yar@nginx.com>
parents:
2643
diff
changeset
|
208 #... |
2245
87a0e2c73a25
Refactored njs documentation.
Yaroslav Zhuravlev <yar@nginx.com>
parents:
diff
changeset
|
209 |
87a0e2c73a25
Refactored njs documentation.
Yaroslav Zhuravlev <yar@nginx.com>
parents:
diff
changeset
|
210 location /keyval { |
2531
9c8a89d3876f
Updated njs examples with js_import and corresponding changes.
Yaroslav Zhuravlev <yar@nginx.com>
parents:
2527
diff
changeset
|
211 js_content http.set_keyval; |
2245
87a0e2c73a25
Refactored njs documentation.
Yaroslav Zhuravlev <yar@nginx.com>
parents:
diff
changeset
|
212 } |
87a0e2c73a25
Refactored njs documentation.
Yaroslav Zhuravlev <yar@nginx.com>
parents:
diff
changeset
|
213 |
87a0e2c73a25
Refactored njs documentation.
Yaroslav Zhuravlev <yar@nginx.com>
parents:
diff
changeset
|
214 location /version { |
2531
9c8a89d3876f
Updated njs examples with js_import and corresponding changes.
Yaroslav Zhuravlev <yar@nginx.com>
parents:
2527
diff
changeset
|
215 js_content http.version; |
2245
87a0e2c73a25
Refactored njs documentation.
Yaroslav Zhuravlev <yar@nginx.com>
parents:
diff
changeset
|
216 } |
87a0e2c73a25
Refactored njs documentation.
Yaroslav Zhuravlev <yar@nginx.com>
parents:
diff
changeset
|
217 |
87a0e2c73a25
Refactored njs documentation.
Yaroslav Zhuravlev <yar@nginx.com>
parents:
diff
changeset
|
218 location /api { |
87a0e2c73a25
Refactored njs documentation.
Yaroslav Zhuravlev <yar@nginx.com>
parents:
diff
changeset
|
219 api write=on; |
87a0e2c73a25
Refactored njs documentation.
Yaroslav Zhuravlev <yar@nginx.com>
parents:
diff
changeset
|
220 } |
87a0e2c73a25
Refactored njs documentation.
Yaroslav Zhuravlev <yar@nginx.com>
parents:
diff
changeset
|
221 </example> |
87a0e2c73a25
Refactored njs documentation.
Yaroslav Zhuravlev <yar@nginx.com>
parents:
diff
changeset
|
222 </para> |
87a0e2c73a25
Refactored njs documentation.
Yaroslav Zhuravlev <yar@nginx.com>
parents:
diff
changeset
|
223 |
87a0e2c73a25
Refactored njs documentation.
Yaroslav Zhuravlev <yar@nginx.com>
parents:
diff
changeset
|
224 <para> |
2531
9c8a89d3876f
Updated njs examples with js_import and corresponding changes.
Yaroslav Zhuravlev <yar@nginx.com>
parents:
2527
diff
changeset
|
225 <path>http.js</path>: |
2245
87a0e2c73a25
Refactored njs documentation.
Yaroslav Zhuravlev <yar@nginx.com>
parents:
diff
changeset
|
226 <example> |
2799
24b379907b0f
Added async and await to njs Examples.
Yaroslav Zhuravlev <yar@nginx.com>
parents:
2780
diff
changeset
|
227 async function set_keyval(r) { |
24b379907b0f
Added async and await to njs Examples.
Yaroslav Zhuravlev <yar@nginx.com>
parents:
2780
diff
changeset
|
228 let res = await r.subrequest('/api/7/http/keyvals/foo', |
2245
87a0e2c73a25
Refactored njs documentation.
Yaroslav Zhuravlev <yar@nginx.com>
parents:
diff
changeset
|
229 { method: 'POST', |
2799
24b379907b0f
Added async and await to njs Examples.
Yaroslav Zhuravlev <yar@nginx.com>
parents:
2780
diff
changeset
|
230 body: JSON.stringify({ foo: 789, bar: "ss dd 00" })}); |
2245
87a0e2c73a25
Refactored njs documentation.
Yaroslav Zhuravlev <yar@nginx.com>
parents:
diff
changeset
|
231 |
2799
24b379907b0f
Added async and await to njs Examples.
Yaroslav Zhuravlev <yar@nginx.com>
parents:
2780
diff
changeset
|
232 if (res.status >= 300) { |
24b379907b0f
Added async and await to njs Examples.
Yaroslav Zhuravlev <yar@nginx.com>
parents:
2780
diff
changeset
|
233 r.return(res.status, res.responseBody); |
24b379907b0f
Added async and await to njs Examples.
Yaroslav Zhuravlev <yar@nginx.com>
parents:
2780
diff
changeset
|
234 return; |
24b379907b0f
Added async and await to njs Examples.
Yaroslav Zhuravlev <yar@nginx.com>
parents:
2780
diff
changeset
|
235 } |
24b379907b0f
Added async and await to njs Examples.
Yaroslav Zhuravlev <yar@nginx.com>
parents:
2780
diff
changeset
|
236 |
24b379907b0f
Added async and await to njs Examples.
Yaroslav Zhuravlev <yar@nginx.com>
parents:
2780
diff
changeset
|
237 r.return(200); |
2245
87a0e2c73a25
Refactored njs documentation.
Yaroslav Zhuravlev <yar@nginx.com>
parents:
diff
changeset
|
238 } |
87a0e2c73a25
Refactored njs documentation.
Yaroslav Zhuravlev <yar@nginx.com>
parents:
diff
changeset
|
239 |
2799
24b379907b0f
Added async and await to njs Examples.
Yaroslav Zhuravlev <yar@nginx.com>
parents:
2780
diff
changeset
|
240 async function version(r) { |
24b379907b0f
Added async and await to njs Examples.
Yaroslav Zhuravlev <yar@nginx.com>
parents:
2780
diff
changeset
|
241 let res = await r.subrequest('/api/7/nginx', { method: 'GET' }); |
2245
87a0e2c73a25
Refactored njs documentation.
Yaroslav Zhuravlev <yar@nginx.com>
parents:
diff
changeset
|
242 |
2799
24b379907b0f
Added async and await to njs Examples.
Yaroslav Zhuravlev <yar@nginx.com>
parents:
2780
diff
changeset
|
243 if (res.status != 200) { |
24b379907b0f
Added async and await to njs Examples.
Yaroslav Zhuravlev <yar@nginx.com>
parents:
2780
diff
changeset
|
244 r.return(res.status); |
24b379907b0f
Added async and await to njs Examples.
Yaroslav Zhuravlev <yar@nginx.com>
parents:
2780
diff
changeset
|
245 return; |
24b379907b0f
Added async and await to njs Examples.
Yaroslav Zhuravlev <yar@nginx.com>
parents:
2780
diff
changeset
|
246 } |
24b379907b0f
Added async and await to njs Examples.
Yaroslav Zhuravlev <yar@nginx.com>
parents:
2780
diff
changeset
|
247 |
24b379907b0f
Added async and await to njs Examples.
Yaroslav Zhuravlev <yar@nginx.com>
parents:
2780
diff
changeset
|
248 var json = JSON.parse(res.responseBody); |
24b379907b0f
Added async and await to njs Examples.
Yaroslav Zhuravlev <yar@nginx.com>
parents:
2780
diff
changeset
|
249 r.return(200, json.version); |
2245
87a0e2c73a25
Refactored njs documentation.
Yaroslav Zhuravlev <yar@nginx.com>
parents:
diff
changeset
|
250 } |
2531
9c8a89d3876f
Updated njs examples with js_import and corresponding changes.
Yaroslav Zhuravlev <yar@nginx.com>
parents:
2527
diff
changeset
|
251 |
9c8a89d3876f
Updated njs examples with js_import and corresponding changes.
Yaroslav Zhuravlev <yar@nginx.com>
parents:
2527
diff
changeset
|
252 export default {set_keyval, version}; |
2245
87a0e2c73a25
Refactored njs documentation.
Yaroslav Zhuravlev <yar@nginx.com>
parents:
diff
changeset
|
253 </example> |
2778
9cafae0b7ef3
Minor fixes in njs examples.
Yaroslav Zhuravlev <yar@nginx.com>
parents:
2643
diff
changeset
|
254 <note> |
9cafae0b7ef3
Minor fixes in njs examples.
Yaroslav Zhuravlev <yar@nginx.com>
parents:
2643
diff
changeset
|
255 Директивы <link doc="../http/ngx_http_keyval_module.xml" id="keyval"/>, |
9cafae0b7ef3
Minor fixes in njs examples.
Yaroslav Zhuravlev <yar@nginx.com>
parents:
2643
diff
changeset
|
256 <link doc="../http/ngx_http_keyval_module.xml" id="keyval_zone"/> |
9cafae0b7ef3
Minor fixes in njs examples.
Yaroslav Zhuravlev <yar@nginx.com>
parents:
2643
diff
changeset
|
257 и <link doc="../http/ngx_http_api_module.xml" id="api"/> |
9cafae0b7ef3
Minor fixes in njs examples.
Yaroslav Zhuravlev <yar@nginx.com>
parents:
2643
diff
changeset
|
258 доступны как часть |
9cafae0b7ef3
Minor fixes in njs examples.
Yaroslav Zhuravlev <yar@nginx.com>
parents:
2643
diff
changeset
|
259 <commercial_version>коммерческой подписки</commercial_version>. |
9cafae0b7ef3
Minor fixes in njs examples.
Yaroslav Zhuravlev <yar@nginx.com>
parents:
2643
diff
changeset
|
260 </note> |
2245
87a0e2c73a25
Refactored njs documentation.
Yaroslav Zhuravlev <yar@nginx.com>
parents:
diff
changeset
|
261 </para> |
87a0e2c73a25
Refactored njs documentation.
Yaroslav Zhuravlev <yar@nginx.com>
parents:
diff
changeset
|
262 |
87a0e2c73a25
Refactored njs documentation.
Yaroslav Zhuravlev <yar@nginx.com>
parents:
diff
changeset
|
263 </section> |
87a0e2c73a25
Refactored njs documentation.
Yaroslav Zhuravlev <yar@nginx.com>
parents:
diff
changeset
|
264 |
87a0e2c73a25
Refactored njs documentation.
Yaroslav Zhuravlev <yar@nginx.com>
parents:
diff
changeset
|
265 |
2642
c60bcc0de435
Updated TOC in njs examples.
Yaroslav Zhuravlev <yar@nginx.com>
parents:
2640
diff
changeset
|
266 <section id="fast_response" name="Возвращение самого быстрого ответа от прокси"> |
2245
87a0e2c73a25
Refactored njs documentation.
Yaroslav Zhuravlev <yar@nginx.com>
parents:
diff
changeset
|
267 |
87a0e2c73a25
Refactored njs documentation.
Yaroslav Zhuravlev <yar@nginx.com>
parents:
diff
changeset
|
268 <para> |
87a0e2c73a25
Refactored njs documentation.
Yaroslav Zhuravlev <yar@nginx.com>
parents:
diff
changeset
|
269 <path>nginx.conf</path>: |
87a0e2c73a25
Refactored njs documentation.
Yaroslav Zhuravlev <yar@nginx.com>
parents:
diff
changeset
|
270 <example> |
2531
9c8a89d3876f
Updated njs examples with js_import and corresponding changes.
Yaroslav Zhuravlev <yar@nginx.com>
parents:
2527
diff
changeset
|
271 js_import http.js; |
2245
87a0e2c73a25
Refactored njs documentation.
Yaroslav Zhuravlev <yar@nginx.com>
parents:
diff
changeset
|
272 |
2642
c60bcc0de435
Updated TOC in njs examples.
Yaroslav Zhuravlev <yar@nginx.com>
parents:
2640
diff
changeset
|
273 location /start { |
c60bcc0de435
Updated TOC in njs examples.
Yaroslav Zhuravlev <yar@nginx.com>
parents:
2640
diff
changeset
|
274 js_content http.content; |
2245
87a0e2c73a25
Refactored njs documentation.
Yaroslav Zhuravlev <yar@nginx.com>
parents:
diff
changeset
|
275 } |
87a0e2c73a25
Refactored njs documentation.
Yaroslav Zhuravlev <yar@nginx.com>
parents:
diff
changeset
|
276 |
2642
c60bcc0de435
Updated TOC in njs examples.
Yaroslav Zhuravlev <yar@nginx.com>
parents:
2640
diff
changeset
|
277 location /foo { |
c60bcc0de435
Updated TOC in njs examples.
Yaroslav Zhuravlev <yar@nginx.com>
parents:
2640
diff
changeset
|
278 proxy_pass http://backend1; |
c60bcc0de435
Updated TOC in njs examples.
Yaroslav Zhuravlev <yar@nginx.com>
parents:
2640
diff
changeset
|
279 } |
c60bcc0de435
Updated TOC in njs examples.
Yaroslav Zhuravlev <yar@nginx.com>
parents:
2640
diff
changeset
|
280 |
c60bcc0de435
Updated TOC in njs examples.
Yaroslav Zhuravlev <yar@nginx.com>
parents:
2640
diff
changeset
|
281 location /bar { |
c60bcc0de435
Updated TOC in njs examples.
Yaroslav Zhuravlev <yar@nginx.com>
parents:
2640
diff
changeset
|
282 proxy_pass http://backend2; |
2245
87a0e2c73a25
Refactored njs documentation.
Yaroslav Zhuravlev <yar@nginx.com>
parents:
diff
changeset
|
283 } |
87a0e2c73a25
Refactored njs documentation.
Yaroslav Zhuravlev <yar@nginx.com>
parents:
diff
changeset
|
284 </example> |
87a0e2c73a25
Refactored njs documentation.
Yaroslav Zhuravlev <yar@nginx.com>
parents:
diff
changeset
|
285 </para> |
87a0e2c73a25
Refactored njs documentation.
Yaroslav Zhuravlev <yar@nginx.com>
parents:
diff
changeset
|
286 |
87a0e2c73a25
Refactored njs documentation.
Yaroslav Zhuravlev <yar@nginx.com>
parents:
diff
changeset
|
287 <para> |
2531
9c8a89d3876f
Updated njs examples with js_import and corresponding changes.
Yaroslav Zhuravlev <yar@nginx.com>
parents:
2527
diff
changeset
|
288 <path>http.js</path>: |
2245
87a0e2c73a25
Refactored njs documentation.
Yaroslav Zhuravlev <yar@nginx.com>
parents:
diff
changeset
|
289 <example> |
2642
c60bcc0de435
Updated TOC in njs examples.
Yaroslav Zhuravlev <yar@nginx.com>
parents:
2640
diff
changeset
|
290 function content(r) { |
c60bcc0de435
Updated TOC in njs examples.
Yaroslav Zhuravlev <yar@nginx.com>
parents:
2640
diff
changeset
|
291 var n = 0; |
c60bcc0de435
Updated TOC in njs examples.
Yaroslav Zhuravlev <yar@nginx.com>
parents:
2640
diff
changeset
|
292 |
c60bcc0de435
Updated TOC in njs examples.
Yaroslav Zhuravlev <yar@nginx.com>
parents:
2640
diff
changeset
|
293 function done(res) { |
c60bcc0de435
Updated TOC in njs examples.
Yaroslav Zhuravlev <yar@nginx.com>
parents:
2640
diff
changeset
|
294 if (n++ == 0) { |
c60bcc0de435
Updated TOC in njs examples.
Yaroslav Zhuravlev <yar@nginx.com>
parents:
2640
diff
changeset
|
295 r.return(res.status, res.responseBody); |
c60bcc0de435
Updated TOC in njs examples.
Yaroslav Zhuravlev <yar@nginx.com>
parents:
2640
diff
changeset
|
296 } |
c60bcc0de435
Updated TOC in njs examples.
Yaroslav Zhuravlev <yar@nginx.com>
parents:
2640
diff
changeset
|
297 } |
c60bcc0de435
Updated TOC in njs examples.
Yaroslav Zhuravlev <yar@nginx.com>
parents:
2640
diff
changeset
|
298 |
c60bcc0de435
Updated TOC in njs examples.
Yaroslav Zhuravlev <yar@nginx.com>
parents:
2640
diff
changeset
|
299 r.subrequest('/foo', r.variables.args, done); |
c60bcc0de435
Updated TOC in njs examples.
Yaroslav Zhuravlev <yar@nginx.com>
parents:
2640
diff
changeset
|
300 r.subrequest('/bar', r.variables.args, done); |
2245
87a0e2c73a25
Refactored njs documentation.
Yaroslav Zhuravlev <yar@nginx.com>
parents:
diff
changeset
|
301 } |
2531
9c8a89d3876f
Updated njs examples with js_import and corresponding changes.
Yaroslav Zhuravlev <yar@nginx.com>
parents:
2527
diff
changeset
|
302 |
2642
c60bcc0de435
Updated TOC in njs examples.
Yaroslav Zhuravlev <yar@nginx.com>
parents:
2640
diff
changeset
|
303 export default {content}; |
2245
87a0e2c73a25
Refactored njs documentation.
Yaroslav Zhuravlev <yar@nginx.com>
parents:
diff
changeset
|
304 </example> |
87a0e2c73a25
Refactored njs documentation.
Yaroslav Zhuravlev <yar@nginx.com>
parents:
diff
changeset
|
305 </para> |
87a0e2c73a25
Refactored njs documentation.
Yaroslav Zhuravlev <yar@nginx.com>
parents:
diff
changeset
|
306 |
87a0e2c73a25
Refactored njs documentation.
Yaroslav Zhuravlev <yar@nginx.com>
parents:
diff
changeset
|
307 </section> |
87a0e2c73a25
Refactored njs documentation.
Yaroslav Zhuravlev <yar@nginx.com>
parents:
diff
changeset
|
308 |
2454
253641e268a8
Added js_requests.js example in njs.
Yaroslav Zhuravlev <yar@nginx.com>
parents:
2438
diff
changeset
|
309 |
2642
c60bcc0de435
Updated TOC in njs examples.
Yaroslav Zhuravlev <yar@nginx.com>
parents:
2640
diff
changeset
|
310 <section id="subrequests_chaining" name="Построение цепочки подзапросов"> |
c60bcc0de435
Updated TOC in njs examples.
Yaroslav Zhuravlev <yar@nginx.com>
parents:
2640
diff
changeset
|
311 |
c60bcc0de435
Updated TOC in njs examples.
Yaroslav Zhuravlev <yar@nginx.com>
parents:
2640
diff
changeset
|
312 <para> |
c60bcc0de435
Updated TOC in njs examples.
Yaroslav Zhuravlev <yar@nginx.com>
parents:
2640
diff
changeset
|
313 <path>nginx.conf</path>: |
c60bcc0de435
Updated TOC in njs examples.
Yaroslav Zhuravlev <yar@nginx.com>
parents:
2640
diff
changeset
|
314 <example> |
c60bcc0de435
Updated TOC in njs examples.
Yaroslav Zhuravlev <yar@nginx.com>
parents:
2640
diff
changeset
|
315 js_import http.js; |
c60bcc0de435
Updated TOC in njs examples.
Yaroslav Zhuravlev <yar@nginx.com>
parents:
2640
diff
changeset
|
316 |
c60bcc0de435
Updated TOC in njs examples.
Yaroslav Zhuravlev <yar@nginx.com>
parents:
2640
diff
changeset
|
317 location /start { |
c60bcc0de435
Updated TOC in njs examples.
Yaroslav Zhuravlev <yar@nginx.com>
parents:
2640
diff
changeset
|
318 js_content http.content; |
c60bcc0de435
Updated TOC in njs examples.
Yaroslav Zhuravlev <yar@nginx.com>
parents:
2640
diff
changeset
|
319 } |
c60bcc0de435
Updated TOC in njs examples.
Yaroslav Zhuravlev <yar@nginx.com>
parents:
2640
diff
changeset
|
320 |
c60bcc0de435
Updated TOC in njs examples.
Yaroslav Zhuravlev <yar@nginx.com>
parents:
2640
diff
changeset
|
321 location /auth { |
c60bcc0de435
Updated TOC in njs examples.
Yaroslav Zhuravlev <yar@nginx.com>
parents:
2640
diff
changeset
|
322 proxy_pass http://auth_backend; |
c60bcc0de435
Updated TOC in njs examples.
Yaroslav Zhuravlev <yar@nginx.com>
parents:
2640
diff
changeset
|
323 } |
c60bcc0de435
Updated TOC in njs examples.
Yaroslav Zhuravlev <yar@nginx.com>
parents:
2640
diff
changeset
|
324 |
c60bcc0de435
Updated TOC in njs examples.
Yaroslav Zhuravlev <yar@nginx.com>
parents:
2640
diff
changeset
|
325 location /backend { |
c60bcc0de435
Updated TOC in njs examples.
Yaroslav Zhuravlev <yar@nginx.com>
parents:
2640
diff
changeset
|
326 proxy_pass http://backend; |
c60bcc0de435
Updated TOC in njs examples.
Yaroslav Zhuravlev <yar@nginx.com>
parents:
2640
diff
changeset
|
327 } |
c60bcc0de435
Updated TOC in njs examples.
Yaroslav Zhuravlev <yar@nginx.com>
parents:
2640
diff
changeset
|
328 </example> |
c60bcc0de435
Updated TOC in njs examples.
Yaroslav Zhuravlev <yar@nginx.com>
parents:
2640
diff
changeset
|
329 </para> |
c60bcc0de435
Updated TOC in njs examples.
Yaroslav Zhuravlev <yar@nginx.com>
parents:
2640
diff
changeset
|
330 |
c60bcc0de435
Updated TOC in njs examples.
Yaroslav Zhuravlev <yar@nginx.com>
parents:
2640
diff
changeset
|
331 <para> |
c60bcc0de435
Updated TOC in njs examples.
Yaroslav Zhuravlev <yar@nginx.com>
parents:
2640
diff
changeset
|
332 <path>http.js</path>: |
c60bcc0de435
Updated TOC in njs examples.
Yaroslav Zhuravlev <yar@nginx.com>
parents:
2640
diff
changeset
|
333 <example> |
2799
24b379907b0f
Added async and await to njs Examples.
Yaroslav Zhuravlev <yar@nginx.com>
parents:
2780
diff
changeset
|
334 async function content(r) { |
24b379907b0f
Added async and await to njs Examples.
Yaroslav Zhuravlev <yar@nginx.com>
parents:
2780
diff
changeset
|
335 try { |
24b379907b0f
Added async and await to njs Examples.
Yaroslav Zhuravlev <yar@nginx.com>
parents:
2780
diff
changeset
|
336 let reply = await r.subrequest('/auth'); |
24b379907b0f
Added async and await to njs Examples.
Yaroslav Zhuravlev <yar@nginx.com>
parents:
2780
diff
changeset
|
337 let response = JSON.parse(reply.responseBody); |
24b379907b0f
Added async and await to njs Examples.
Yaroslav Zhuravlev <yar@nginx.com>
parents:
2780
diff
changeset
|
338 let token = response['token']; |
24b379907b0f
Added async and await to njs Examples.
Yaroslav Zhuravlev <yar@nginx.com>
parents:
2780
diff
changeset
|
339 |
24b379907b0f
Added async and await to njs Examples.
Yaroslav Zhuravlev <yar@nginx.com>
parents:
2780
diff
changeset
|
340 if (!token) { |
24b379907b0f
Added async and await to njs Examples.
Yaroslav Zhuravlev <yar@nginx.com>
parents:
2780
diff
changeset
|
341 throw new Error("token is not available"); |
24b379907b0f
Added async and await to njs Examples.
Yaroslav Zhuravlev <yar@nginx.com>
parents:
2780
diff
changeset
|
342 } |
24b379907b0f
Added async and await to njs Examples.
Yaroslav Zhuravlev <yar@nginx.com>
parents:
2780
diff
changeset
|
343 |
24b379907b0f
Added async and await to njs Examples.
Yaroslav Zhuravlev <yar@nginx.com>
parents:
2780
diff
changeset
|
344 let backend_reply = await r.subrequest('/backend', `token=${token}`); |
24b379907b0f
Added async and await to njs Examples.
Yaroslav Zhuravlev <yar@nginx.com>
parents:
2780
diff
changeset
|
345 r.return(backend_reply.status, backend_reply.responseBody); |
24b379907b0f
Added async and await to njs Examples.
Yaroslav Zhuravlev <yar@nginx.com>
parents:
2780
diff
changeset
|
346 |
24b379907b0f
Added async and await to njs Examples.
Yaroslav Zhuravlev <yar@nginx.com>
parents:
2780
diff
changeset
|
347 } catch (e) { |
24b379907b0f
Added async and await to njs Examples.
Yaroslav Zhuravlev <yar@nginx.com>
parents:
2780
diff
changeset
|
348 r.error(e); |
24b379907b0f
Added async and await to njs Examples.
Yaroslav Zhuravlev <yar@nginx.com>
parents:
2780
diff
changeset
|
349 r.return(500); |
24b379907b0f
Added async and await to njs Examples.
Yaroslav Zhuravlev <yar@nginx.com>
parents:
2780
diff
changeset
|
350 } |
2642
c60bcc0de435
Updated TOC in njs examples.
Yaroslav Zhuravlev <yar@nginx.com>
parents:
2640
diff
changeset
|
351 } |
c60bcc0de435
Updated TOC in njs examples.
Yaroslav Zhuravlev <yar@nginx.com>
parents:
2640
diff
changeset
|
352 |
c60bcc0de435
Updated TOC in njs examples.
Yaroslav Zhuravlev <yar@nginx.com>
parents:
2640
diff
changeset
|
353 export default {content}; |
c60bcc0de435
Updated TOC in njs examples.
Yaroslav Zhuravlev <yar@nginx.com>
parents:
2640
diff
changeset
|
354 </example> |
c60bcc0de435
Updated TOC in njs examples.
Yaroslav Zhuravlev <yar@nginx.com>
parents:
2640
diff
changeset
|
355 </para> |
c60bcc0de435
Updated TOC in njs examples.
Yaroslav Zhuravlev <yar@nginx.com>
parents:
2640
diff
changeset
|
356 |
c60bcc0de435
Updated TOC in njs examples.
Yaroslav Zhuravlev <yar@nginx.com>
parents:
2640
diff
changeset
|
357 </section> |
c60bcc0de435
Updated TOC in njs examples.
Yaroslav Zhuravlev <yar@nginx.com>
parents:
2640
diff
changeset
|
358 |
c60bcc0de435
Updated TOC in njs examples.
Yaroslav Zhuravlev <yar@nginx.com>
parents:
2640
diff
changeset
|
359 </section> |
c60bcc0de435
Updated TOC in njs examples.
Yaroslav Zhuravlev <yar@nginx.com>
parents:
2640
diff
changeset
|
360 |
c60bcc0de435
Updated TOC in njs examples.
Yaroslav Zhuravlev <yar@nginx.com>
parents:
2640
diff
changeset
|
361 |
c60bcc0de435
Updated TOC in njs examples.
Yaroslav Zhuravlev <yar@nginx.com>
parents:
2640
diff
changeset
|
362 <section id="misc" name="Разное"> |
c60bcc0de435
Updated TOC in njs examples.
Yaroslav Zhuravlev <yar@nginx.com>
parents:
2640
diff
changeset
|
363 |
c60bcc0de435
Updated TOC in njs examples.
Yaroslav Zhuravlev <yar@nginx.com>
parents:
2640
diff
changeset
|
364 |
c60bcc0de435
Updated TOC in njs examples.
Yaroslav Zhuravlev <yar@nginx.com>
parents:
2640
diff
changeset
|
365 <section id="redirect" name="Внутренняя переадресация"> |
c60bcc0de435
Updated TOC in njs examples.
Yaroslav Zhuravlev <yar@nginx.com>
parents:
2640
diff
changeset
|
366 |
c60bcc0de435
Updated TOC in njs examples.
Yaroslav Zhuravlev <yar@nginx.com>
parents:
2640
diff
changeset
|
367 <para> |
c60bcc0de435
Updated TOC in njs examples.
Yaroslav Zhuravlev <yar@nginx.com>
parents:
2640
diff
changeset
|
368 <path>nginx.conf</path>: |
c60bcc0de435
Updated TOC in njs examples.
Yaroslav Zhuravlev <yar@nginx.com>
parents:
2640
diff
changeset
|
369 <example> |
c60bcc0de435
Updated TOC in njs examples.
Yaroslav Zhuravlev <yar@nginx.com>
parents:
2640
diff
changeset
|
370 js_import http.js; |
c60bcc0de435
Updated TOC in njs examples.
Yaroslav Zhuravlev <yar@nginx.com>
parents:
2640
diff
changeset
|
371 |
c60bcc0de435
Updated TOC in njs examples.
Yaroslav Zhuravlev <yar@nginx.com>
parents:
2640
diff
changeset
|
372 location /redirect { |
c60bcc0de435
Updated TOC in njs examples.
Yaroslav Zhuravlev <yar@nginx.com>
parents:
2640
diff
changeset
|
373 js_content http.redirect; |
c60bcc0de435
Updated TOC in njs examples.
Yaroslav Zhuravlev <yar@nginx.com>
parents:
2640
diff
changeset
|
374 } |
c60bcc0de435
Updated TOC in njs examples.
Yaroslav Zhuravlev <yar@nginx.com>
parents:
2640
diff
changeset
|
375 |
c60bcc0de435
Updated TOC in njs examples.
Yaroslav Zhuravlev <yar@nginx.com>
parents:
2640
diff
changeset
|
376 location @named { |
c60bcc0de435
Updated TOC in njs examples.
Yaroslav Zhuravlev <yar@nginx.com>
parents:
2640
diff
changeset
|
377 return 200 named; |
c60bcc0de435
Updated TOC in njs examples.
Yaroslav Zhuravlev <yar@nginx.com>
parents:
2640
diff
changeset
|
378 } |
c60bcc0de435
Updated TOC in njs examples.
Yaroslav Zhuravlev <yar@nginx.com>
parents:
2640
diff
changeset
|
379 </example> |
c60bcc0de435
Updated TOC in njs examples.
Yaroslav Zhuravlev <yar@nginx.com>
parents:
2640
diff
changeset
|
380 </para> |
c60bcc0de435
Updated TOC in njs examples.
Yaroslav Zhuravlev <yar@nginx.com>
parents:
2640
diff
changeset
|
381 |
c60bcc0de435
Updated TOC in njs examples.
Yaroslav Zhuravlev <yar@nginx.com>
parents:
2640
diff
changeset
|
382 <para> |
c60bcc0de435
Updated TOC in njs examples.
Yaroslav Zhuravlev <yar@nginx.com>
parents:
2640
diff
changeset
|
383 <path>http.js</path>: |
c60bcc0de435
Updated TOC in njs examples.
Yaroslav Zhuravlev <yar@nginx.com>
parents:
2640
diff
changeset
|
384 <example> |
c60bcc0de435
Updated TOC in njs examples.
Yaroslav Zhuravlev <yar@nginx.com>
parents:
2640
diff
changeset
|
385 function redirect(r) { |
c60bcc0de435
Updated TOC in njs examples.
Yaroslav Zhuravlev <yar@nginx.com>
parents:
2640
diff
changeset
|
386 r.internalRedirect('@named'); |
c60bcc0de435
Updated TOC in njs examples.
Yaroslav Zhuravlev <yar@nginx.com>
parents:
2640
diff
changeset
|
387 } |
c60bcc0de435
Updated TOC in njs examples.
Yaroslav Zhuravlev <yar@nginx.com>
parents:
2640
diff
changeset
|
388 |
c60bcc0de435
Updated TOC in njs examples.
Yaroslav Zhuravlev <yar@nginx.com>
parents:
2640
diff
changeset
|
389 export default {redirect}; |
c60bcc0de435
Updated TOC in njs examples.
Yaroslav Zhuravlev <yar@nginx.com>
parents:
2640
diff
changeset
|
390 </example> |
c60bcc0de435
Updated TOC in njs examples.
Yaroslav Zhuravlev <yar@nginx.com>
parents:
2640
diff
changeset
|
391 </para> |
c60bcc0de435
Updated TOC in njs examples.
Yaroslav Zhuravlev <yar@nginx.com>
parents:
2640
diff
changeset
|
392 |
c60bcc0de435
Updated TOC in njs examples.
Yaroslav Zhuravlev <yar@nginx.com>
parents:
2640
diff
changeset
|
393 </section> |
c60bcc0de435
Updated TOC in njs examples.
Yaroslav Zhuravlev <yar@nginx.com>
parents:
2640
diff
changeset
|
394 |
2643
4849fa0fd4b4
Removed URL Decoding/Encoding examples from njs.
Yaroslav Zhuravlev <yar@nginx.com>
parents:
2642
diff
changeset
|
395 |
2454
253641e268a8
Added js_requests.js example in njs.
Yaroslav Zhuravlev <yar@nginx.com>
parents:
2438
diff
changeset
|
396 <section id="requests" name="Запись в лог количества запросов от клиента"> |
253641e268a8
Added js_requests.js example in njs.
Yaroslav Zhuravlev <yar@nginx.com>
parents:
2438
diff
changeset
|
397 |
253641e268a8
Added js_requests.js example in njs.
Yaroslav Zhuravlev <yar@nginx.com>
parents:
2438
diff
changeset
|
398 <para> |
253641e268a8
Added js_requests.js example in njs.
Yaroslav Zhuravlev <yar@nginx.com>
parents:
2438
diff
changeset
|
399 <path>nginx.conf</path>: |
253641e268a8
Added js_requests.js example in njs.
Yaroslav Zhuravlev <yar@nginx.com>
parents:
2438
diff
changeset
|
400 <example> |
2531
9c8a89d3876f
Updated njs examples with js_import and corresponding changes.
Yaroslav Zhuravlev <yar@nginx.com>
parents:
2527
diff
changeset
|
401 js_import http.js; |
2454
253641e268a8
Added js_requests.js example in njs.
Yaroslav Zhuravlev <yar@nginx.com>
parents:
2438
diff
changeset
|
402 |
2531
9c8a89d3876f
Updated njs examples with js_import and corresponding changes.
Yaroslav Zhuravlev <yar@nginx.com>
parents:
2527
diff
changeset
|
403 js_set $num_requests http.num_requests; |
2454
253641e268a8
Added js_requests.js example in njs.
Yaroslav Zhuravlev <yar@nginx.com>
parents:
2438
diff
changeset
|
404 |
253641e268a8
Added js_requests.js example in njs.
Yaroslav Zhuravlev <yar@nginx.com>
parents:
2438
diff
changeset
|
405 keyval_zone zone=foo:10m; |
253641e268a8
Added js_requests.js example in njs.
Yaroslav Zhuravlev <yar@nginx.com>
parents:
2438
diff
changeset
|
406 |
253641e268a8
Added js_requests.js example in njs.
Yaroslav Zhuravlev <yar@nginx.com>
parents:
2438
diff
changeset
|
407 keyval $remote_addr $foo zone=foo; |
253641e268a8
Added js_requests.js example in njs.
Yaroslav Zhuravlev <yar@nginx.com>
parents:
2438
diff
changeset
|
408 |
253641e268a8
Added js_requests.js example in njs.
Yaroslav Zhuravlev <yar@nginx.com>
parents:
2438
diff
changeset
|
409 log_format bar '$remote_addr [$time_local] $num_requests'; |
253641e268a8
Added js_requests.js example in njs.
Yaroslav Zhuravlev <yar@nginx.com>
parents:
2438
diff
changeset
|
410 access_log logs/access.log bar; |
253641e268a8
Added js_requests.js example in njs.
Yaroslav Zhuravlev <yar@nginx.com>
parents:
2438
diff
changeset
|
411 |
253641e268a8
Added js_requests.js example in njs.
Yaroslav Zhuravlev <yar@nginx.com>
parents:
2438
diff
changeset
|
412 server { |
253641e268a8
Added js_requests.js example in njs.
Yaroslav Zhuravlev <yar@nginx.com>
parents:
2438
diff
changeset
|
413 listen 8000; |
253641e268a8
Added js_requests.js example in njs.
Yaroslav Zhuravlev <yar@nginx.com>
parents:
2438
diff
changeset
|
414 |
253641e268a8
Added js_requests.js example in njs.
Yaroslav Zhuravlev <yar@nginx.com>
parents:
2438
diff
changeset
|
415 location / { |
253641e268a8
Added js_requests.js example in njs.
Yaroslav Zhuravlev <yar@nginx.com>
parents:
2438
diff
changeset
|
416 root html; |
253641e268a8
Added js_requests.js example in njs.
Yaroslav Zhuravlev <yar@nginx.com>
parents:
2438
diff
changeset
|
417 } |
253641e268a8
Added js_requests.js example in njs.
Yaroslav Zhuravlev <yar@nginx.com>
parents:
2438
diff
changeset
|
418 } |
253641e268a8
Added js_requests.js example in njs.
Yaroslav Zhuravlev <yar@nginx.com>
parents:
2438
diff
changeset
|
419 </example> |
253641e268a8
Added js_requests.js example in njs.
Yaroslav Zhuravlev <yar@nginx.com>
parents:
2438
diff
changeset
|
420 </para> |
253641e268a8
Added js_requests.js example in njs.
Yaroslav Zhuravlev <yar@nginx.com>
parents:
2438
diff
changeset
|
421 |
253641e268a8
Added js_requests.js example in njs.
Yaroslav Zhuravlev <yar@nginx.com>
parents:
2438
diff
changeset
|
422 <para> |
2531
9c8a89d3876f
Updated njs examples with js_import and corresponding changes.
Yaroslav Zhuravlev <yar@nginx.com>
parents:
2527
diff
changeset
|
423 <path>http.js</path>: |
2454
253641e268a8
Added js_requests.js example in njs.
Yaroslav Zhuravlev <yar@nginx.com>
parents:
2438
diff
changeset
|
424 <example> |
253641e268a8
Added js_requests.js example in njs.
Yaroslav Zhuravlev <yar@nginx.com>
parents:
2438
diff
changeset
|
425 function num_requests(r) |
253641e268a8
Added js_requests.js example in njs.
Yaroslav Zhuravlev <yar@nginx.com>
parents:
2438
diff
changeset
|
426 { |
253641e268a8
Added js_requests.js example in njs.
Yaroslav Zhuravlev <yar@nginx.com>
parents:
2438
diff
changeset
|
427 var n = r.variables.foo; |
253641e268a8
Added js_requests.js example in njs.
Yaroslav Zhuravlev <yar@nginx.com>
parents:
2438
diff
changeset
|
428 n = n ? Number(n) + 1 : 1; |
253641e268a8
Added js_requests.js example in njs.
Yaroslav Zhuravlev <yar@nginx.com>
parents:
2438
diff
changeset
|
429 r.variables.foo = n; |
253641e268a8
Added js_requests.js example in njs.
Yaroslav Zhuravlev <yar@nginx.com>
parents:
2438
diff
changeset
|
430 return n; |
253641e268a8
Added js_requests.js example in njs.
Yaroslav Zhuravlev <yar@nginx.com>
parents:
2438
diff
changeset
|
431 } |
2531
9c8a89d3876f
Updated njs examples with js_import and corresponding changes.
Yaroslav Zhuravlev <yar@nginx.com>
parents:
2527
diff
changeset
|
432 |
9c8a89d3876f
Updated njs examples with js_import and corresponding changes.
Yaroslav Zhuravlev <yar@nginx.com>
parents:
2527
diff
changeset
|
433 export default {num_requests}; |
2454
253641e268a8
Added js_requests.js example in njs.
Yaroslav Zhuravlev <yar@nginx.com>
parents:
2438
diff
changeset
|
434 </example> |
253641e268a8
Added js_requests.js example in njs.
Yaroslav Zhuravlev <yar@nginx.com>
parents:
2438
diff
changeset
|
435 <note> |
253641e268a8
Added js_requests.js example in njs.
Yaroslav Zhuravlev <yar@nginx.com>
parents:
2438
diff
changeset
|
436 Директивы <link doc="../http/ngx_http_keyval_module.xml" id="keyval"/> и |
253641e268a8
Added js_requests.js example in njs.
Yaroslav Zhuravlev <yar@nginx.com>
parents:
2438
diff
changeset
|
437 <link doc="../http/ngx_http_keyval_module.xml" id="keyval_zone"/> |
253641e268a8
Added js_requests.js example in njs.
Yaroslav Zhuravlev <yar@nginx.com>
parents:
2438
diff
changeset
|
438 доступны как часть |
253641e268a8
Added js_requests.js example in njs.
Yaroslav Zhuravlev <yar@nginx.com>
parents:
2438
diff
changeset
|
439 <commercial_version>коммерческой подписки</commercial_version>. |
253641e268a8
Added js_requests.js example in njs.
Yaroslav Zhuravlev <yar@nginx.com>
parents:
2438
diff
changeset
|
440 </note> |
253641e268a8
Added js_requests.js example in njs.
Yaroslav Zhuravlev <yar@nginx.com>
parents:
2438
diff
changeset
|
441 </para> |
253641e268a8
Added js_requests.js example in njs.
Yaroslav Zhuravlev <yar@nginx.com>
parents:
2438
diff
changeset
|
442 |
253641e268a8
Added js_requests.js example in njs.
Yaroslav Zhuravlev <yar@nginx.com>
parents:
2438
diff
changeset
|
443 </section> |
253641e268a8
Added js_requests.js example in njs.
Yaroslav Zhuravlev <yar@nginx.com>
parents:
2438
diff
changeset
|
444 |
2642
c60bcc0de435
Updated TOC in njs examples.
Yaroslav Zhuravlev <yar@nginx.com>
parents:
2640
diff
changeset
|
445 </section> |
c60bcc0de435
Updated TOC in njs examples.
Yaroslav Zhuravlev <yar@nginx.com>
parents:
2640
diff
changeset
|
446 |
2245
87a0e2c73a25
Refactored njs documentation.
Yaroslav Zhuravlev <yar@nginx.com>
parents:
diff
changeset
|
447 </article> |