Mercurial > hg > nginx-site
annotate xml/en/docs/njs/examples.xml @ 2841:55a573521ac3
Linux packages: added Ubuntu 22.04 "jammy".
author | Konstantin Pavlov <thresh@nginx.com> |
---|---|
date | Fri, 29 Apr 2022 21:36:07 +0400 |
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="Examples" |
87a0e2c73a25
Refactored njs documentation.
Yaroslav Zhuravlev <yar@nginx.com>
parents:
diff
changeset
|
10 link="/en/docs/njs/examples.html" |
87a0e2c73a25
Refactored njs documentation.
Yaroslav Zhuravlev <yar@nginx.com>
parents:
diff
changeset
|
11 lang="en" |
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 The examples work since |
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> |
9c8a89d3876f
Updated njs examples with js_import and corresponding changes.
Yaroslav Zhuravlev <yar@nginx.com>
parents:
2527
diff
changeset
|
22 |
2245
87a0e2c73a25
Refactored njs documentation.
Yaroslav Zhuravlev <yar@nginx.com>
parents:
diff
changeset
|
23 |
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 Аuthorization"> |
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="Creating 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="Creating secure_link Hash"> |
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="Getting Arbitrary Field from JWT |
885bcb41fbcf
Added an example to njs examples.
Yaroslav Zhuravlev <yar@nginx.com>
parents:
2639
diff
changeset
|
148 as nginx Variable"> |
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 In the following example, the <literal>sub</literal> field |
bf641527bd3d
Updated jwt_field example in njs Examples.
Yaroslav Zhuravlev <yar@nginx.com>
parents:
2778
diff
changeset
|
152 is extracted from JWT payload. |
bf641527bd3d
Updated jwt_field example in njs Examples.
Yaroslav Zhuravlev <yar@nginx.com>
parents:
2778
diff
changeset
|
153 The JWT token is taken from the <header>Authorization</header> header. |
bf641527bd3d
Updated jwt_field example in njs Examples.
Yaroslav Zhuravlev <yar@nginx.com>
parents:
2778
diff
changeset
|
154 </para> |
bf641527bd3d
Updated jwt_field example in njs Examples.
Yaroslav Zhuravlev <yar@nginx.com>
parents:
2778
diff
changeset
|
155 |
bf641527bd3d
Updated jwt_field example in njs Examples.
Yaroslav Zhuravlev <yar@nginx.com>
parents:
2778
diff
changeset
|
156 <para> |
2640
885bcb41fbcf
Added an example to njs examples.
Yaroslav Zhuravlev <yar@nginx.com>
parents:
2639
diff
changeset
|
157 <path>nginx.conf</path>: |
885bcb41fbcf
Added an example to njs examples.
Yaroslav Zhuravlev <yar@nginx.com>
parents:
2639
diff
changeset
|
158 <example> |
885bcb41fbcf
Added an example to njs examples.
Yaroslav Zhuravlev <yar@nginx.com>
parents:
2639
diff
changeset
|
159 js_import http.js; |
885bcb41fbcf
Added an example to njs examples.
Yaroslav Zhuravlev <yar@nginx.com>
parents:
2639
diff
changeset
|
160 |
2778
9cafae0b7ef3
Minor fixes in njs examples.
Yaroslav Zhuravlev <yar@nginx.com>
parents:
2643
diff
changeset
|
161 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
|
162 |
885bcb41fbcf
Added an example to njs examples.
Yaroslav Zhuravlev <yar@nginx.com>
parents:
2639
diff
changeset
|
163 server { |
2778
9cafae0b7ef3
Minor fixes in njs examples.
Yaroslav Zhuravlev <yar@nginx.com>
parents:
2643
diff
changeset
|
164 #... |
2640
885bcb41fbcf
Added an example to njs examples.
Yaroslav Zhuravlev <yar@nginx.com>
parents:
2639
diff
changeset
|
165 |
885bcb41fbcf
Added an example to njs examples.
Yaroslav Zhuravlev <yar@nginx.com>
parents:
2639
diff
changeset
|
166 location /jwt { |
885bcb41fbcf
Added an example to njs examples.
Yaroslav Zhuravlev <yar@nginx.com>
parents:
2639
diff
changeset
|
167 return 200 $jwt_payload_sub; |
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 } |
885bcb41fbcf
Added an example to njs examples.
Yaroslav Zhuravlev <yar@nginx.com>
parents:
2639
diff
changeset
|
170 </example> |
885bcb41fbcf
Added an example to njs examples.
Yaroslav Zhuravlev <yar@nginx.com>
parents:
2639
diff
changeset
|
171 </para> |
885bcb41fbcf
Added an example to njs examples.
Yaroslav Zhuravlev <yar@nginx.com>
parents:
2639
diff
changeset
|
172 |
885bcb41fbcf
Added an example to njs examples.
Yaroslav Zhuravlev <yar@nginx.com>
parents:
2639
diff
changeset
|
173 <para> |
885bcb41fbcf
Added an example to njs examples.
Yaroslav Zhuravlev <yar@nginx.com>
parents:
2639
diff
changeset
|
174 <path>http.js</path>: |
885bcb41fbcf
Added an example to njs examples.
Yaroslav Zhuravlev <yar@nginx.com>
parents:
2639
diff
changeset
|
175 <example> |
885bcb41fbcf
Added an example to njs examples.
Yaroslav Zhuravlev <yar@nginx.com>
parents:
2639
diff
changeset
|
176 function jwt(data) { |
885bcb41fbcf
Added an example to njs examples.
Yaroslav Zhuravlev <yar@nginx.com>
parents:
2639
diff
changeset
|
177 var parts = data.split('.').slice(0,2) |
885bcb41fbcf
Added an example to njs examples.
Yaroslav Zhuravlev <yar@nginx.com>
parents:
2639
diff
changeset
|
178 .map(v=>Buffer.from(v, 'base64url').toString()) |
885bcb41fbcf
Added an example to njs examples.
Yaroslav Zhuravlev <yar@nginx.com>
parents:
2639
diff
changeset
|
179 .map(JSON.parse); |
885bcb41fbcf
Added an example to njs examples.
Yaroslav Zhuravlev <yar@nginx.com>
parents:
2639
diff
changeset
|
180 return { headers:parts[0], payload: parts[1] }; |
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 |
885bcb41fbcf
Added an example to njs examples.
Yaroslav Zhuravlev <yar@nginx.com>
parents:
2639
diff
changeset
|
183 function jwt_payload_sub(r) { |
885bcb41fbcf
Added an example to njs examples.
Yaroslav Zhuravlev <yar@nginx.com>
parents:
2639
diff
changeset
|
184 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
|
185 // when the token is provided as the "myjwt" argument |
bf641527bd3d
Updated jwt_field example in njs Examples.
Yaroslav Zhuravlev <yar@nginx.com>
parents:
2778
diff
changeset
|
186 // return jwt(r.args.myjwt).payload.sub; |
2640
885bcb41fbcf
Added an example to njs examples.
Yaroslav Zhuravlev <yar@nginx.com>
parents:
2639
diff
changeset
|
187 } |
885bcb41fbcf
Added an example to njs examples.
Yaroslav Zhuravlev <yar@nginx.com>
parents:
2639
diff
changeset
|
188 |
2778
9cafae0b7ef3
Minor fixes in njs examples.
Yaroslav Zhuravlev <yar@nginx.com>
parents:
2643
diff
changeset
|
189 export default {jwt_payload_sub}; |
2640
885bcb41fbcf
Added an example to njs examples.
Yaroslav Zhuravlev <yar@nginx.com>
parents:
2639
diff
changeset
|
190 </example> |
885bcb41fbcf
Added an example to njs examples.
Yaroslav Zhuravlev <yar@nginx.com>
parents:
2639
diff
changeset
|
191 </para> |
885bcb41fbcf
Added an example to njs examples.
Yaroslav Zhuravlev <yar@nginx.com>
parents:
2639
diff
changeset
|
192 |
885bcb41fbcf
Added an example to njs examples.
Yaroslav Zhuravlev <yar@nginx.com>
parents:
2639
diff
changeset
|
193 </section> |
885bcb41fbcf
Added an example to njs examples.
Yaroslav Zhuravlev <yar@nginx.com>
parents:
2639
diff
changeset
|
194 |
2642
c60bcc0de435
Updated TOC in njs examples.
Yaroslav Zhuravlev <yar@nginx.com>
parents:
2640
diff
changeset
|
195 </section> |
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 |
c60bcc0de435
Updated TOC in njs examples.
Yaroslav Zhuravlev <yar@nginx.com>
parents:
2640
diff
changeset
|
198 <section id="http_proxying" name="HTTP Proxying"> |
c60bcc0de435
Updated TOC in njs examples.
Yaroslav Zhuravlev <yar@nginx.com>
parents:
2640
diff
changeset
|
199 |
2640
885bcb41fbcf
Added an example to njs examples.
Yaroslav Zhuravlev <yar@nginx.com>
parents:
2639
diff
changeset
|
200 |
2245
87a0e2c73a25
Refactored njs documentation.
Yaroslav Zhuravlev <yar@nginx.com>
parents:
diff
changeset
|
201 <section id="subrequest" name="Accessing API from a Subrequest"> |
87a0e2c73a25
Refactored njs documentation.
Yaroslav Zhuravlev <yar@nginx.com>
parents:
diff
changeset
|
202 |
87a0e2c73a25
Refactored njs documentation.
Yaroslav Zhuravlev <yar@nginx.com>
parents:
diff
changeset
|
203 <para> |
87a0e2c73a25
Refactored njs documentation.
Yaroslav Zhuravlev <yar@nginx.com>
parents:
diff
changeset
|
204 <path>nginx.conf</path>: |
87a0e2c73a25
Refactored njs documentation.
Yaroslav Zhuravlev <yar@nginx.com>
parents:
diff
changeset
|
205 <example> |
2531
9c8a89d3876f
Updated njs examples with js_import and corresponding changes.
Yaroslav Zhuravlev <yar@nginx.com>
parents:
2527
diff
changeset
|
206 js_import http.js; |
2245
87a0e2c73a25
Refactored njs documentation.
Yaroslav Zhuravlev <yar@nginx.com>
parents:
diff
changeset
|
207 |
87a0e2c73a25
Refactored njs documentation.
Yaroslav Zhuravlev <yar@nginx.com>
parents:
diff
changeset
|
208 keyval_zone zone=foo:10m; |
2778
9cafae0b7ef3
Minor fixes in njs examples.
Yaroslav Zhuravlev <yar@nginx.com>
parents:
2643
diff
changeset
|
209 #... |
2245
87a0e2c73a25
Refactored njs documentation.
Yaroslav Zhuravlev <yar@nginx.com>
parents:
diff
changeset
|
210 |
87a0e2c73a25
Refactored njs documentation.
Yaroslav Zhuravlev <yar@nginx.com>
parents:
diff
changeset
|
211 location /keyval { |
2531
9c8a89d3876f
Updated njs examples with js_import and corresponding changes.
Yaroslav Zhuravlev <yar@nginx.com>
parents:
2527
diff
changeset
|
212 js_content http.set_keyval; |
2245
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 |
87a0e2c73a25
Refactored njs documentation.
Yaroslav Zhuravlev <yar@nginx.com>
parents:
diff
changeset
|
215 location /version { |
2531
9c8a89d3876f
Updated njs examples with js_import and corresponding changes.
Yaroslav Zhuravlev <yar@nginx.com>
parents:
2527
diff
changeset
|
216 js_content http.version; |
2245
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 |
87a0e2c73a25
Refactored njs documentation.
Yaroslav Zhuravlev <yar@nginx.com>
parents:
diff
changeset
|
219 location /api { |
87a0e2c73a25
Refactored njs documentation.
Yaroslav Zhuravlev <yar@nginx.com>
parents:
diff
changeset
|
220 api write=on; |
87a0e2c73a25
Refactored njs documentation.
Yaroslav Zhuravlev <yar@nginx.com>
parents:
diff
changeset
|
221 } |
87a0e2c73a25
Refactored njs documentation.
Yaroslav Zhuravlev <yar@nginx.com>
parents:
diff
changeset
|
222 </example> |
87a0e2c73a25
Refactored njs documentation.
Yaroslav Zhuravlev <yar@nginx.com>
parents:
diff
changeset
|
223 </para> |
87a0e2c73a25
Refactored njs documentation.
Yaroslav Zhuravlev <yar@nginx.com>
parents:
diff
changeset
|
224 |
87a0e2c73a25
Refactored njs documentation.
Yaroslav Zhuravlev <yar@nginx.com>
parents:
diff
changeset
|
225 <para> |
2531
9c8a89d3876f
Updated njs examples with js_import and corresponding changes.
Yaroslav Zhuravlev <yar@nginx.com>
parents:
2527
diff
changeset
|
226 <path>http.js</path>: |
2245
87a0e2c73a25
Refactored njs documentation.
Yaroslav Zhuravlev <yar@nginx.com>
parents:
diff
changeset
|
227 <example> |
2799
24b379907b0f
Added async and await to njs Examples.
Yaroslav Zhuravlev <yar@nginx.com>
parents:
2780
diff
changeset
|
228 async function set_keyval(r) { |
24b379907b0f
Added async and await to njs Examples.
Yaroslav Zhuravlev <yar@nginx.com>
parents:
2780
diff
changeset
|
229 let res = await r.subrequest('/api/7/http/keyvals/foo', |
2245
87a0e2c73a25
Refactored njs documentation.
Yaroslav Zhuravlev <yar@nginx.com>
parents:
diff
changeset
|
230 { method: 'POST', |
2799
24b379907b0f
Added async and await to njs Examples.
Yaroslav Zhuravlev <yar@nginx.com>
parents:
2780
diff
changeset
|
231 body: JSON.stringify({ foo: 789, bar: "ss dd 00" })}); |
2245
87a0e2c73a25
Refactored njs documentation.
Yaroslav Zhuravlev <yar@nginx.com>
parents:
diff
changeset
|
232 |
2799
24b379907b0f
Added async and await to njs Examples.
Yaroslav Zhuravlev <yar@nginx.com>
parents:
2780
diff
changeset
|
233 if (res.status >= 300) { |
24b379907b0f
Added async and await to njs Examples.
Yaroslav Zhuravlev <yar@nginx.com>
parents:
2780
diff
changeset
|
234 r.return(res.status, res.responseBody); |
24b379907b0f
Added async and await to njs Examples.
Yaroslav Zhuravlev <yar@nginx.com>
parents:
2780
diff
changeset
|
235 return; |
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 |
24b379907b0f
Added async and await to njs Examples.
Yaroslav Zhuravlev <yar@nginx.com>
parents:
2780
diff
changeset
|
238 r.return(200); |
2245
87a0e2c73a25
Refactored njs documentation.
Yaroslav Zhuravlev <yar@nginx.com>
parents:
diff
changeset
|
239 } |
87a0e2c73a25
Refactored njs documentation.
Yaroslav Zhuravlev <yar@nginx.com>
parents:
diff
changeset
|
240 |
2799
24b379907b0f
Added async and await to njs Examples.
Yaroslav Zhuravlev <yar@nginx.com>
parents:
2780
diff
changeset
|
241 async function version(r) { |
24b379907b0f
Added async and await to njs Examples.
Yaroslav Zhuravlev <yar@nginx.com>
parents:
2780
diff
changeset
|
242 let res = await r.subrequest('/api/7/nginx', { method: 'GET' }); |
2245
87a0e2c73a25
Refactored njs documentation.
Yaroslav Zhuravlev <yar@nginx.com>
parents:
diff
changeset
|
243 |
2799
24b379907b0f
Added async and await to njs Examples.
Yaroslav Zhuravlev <yar@nginx.com>
parents:
2780
diff
changeset
|
244 if (res.status != 200) { |
24b379907b0f
Added async and await to njs Examples.
Yaroslav Zhuravlev <yar@nginx.com>
parents:
2780
diff
changeset
|
245 r.return(res.status); |
24b379907b0f
Added async and await to njs Examples.
Yaroslav Zhuravlev <yar@nginx.com>
parents:
2780
diff
changeset
|
246 return; |
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 |
24b379907b0f
Added async and await to njs Examples.
Yaroslav Zhuravlev <yar@nginx.com>
parents:
2780
diff
changeset
|
249 var json = JSON.parse(res.responseBody); |
24b379907b0f
Added async and await to njs Examples.
Yaroslav Zhuravlev <yar@nginx.com>
parents:
2780
diff
changeset
|
250 r.return(200, json.version); |
2245
87a0e2c73a25
Refactored njs documentation.
Yaroslav Zhuravlev <yar@nginx.com>
parents:
diff
changeset
|
251 } |
2531
9c8a89d3876f
Updated njs examples with js_import and corresponding changes.
Yaroslav Zhuravlev <yar@nginx.com>
parents:
2527
diff
changeset
|
252 |
9c8a89d3876f
Updated njs examples with js_import and corresponding changes.
Yaroslav Zhuravlev <yar@nginx.com>
parents:
2527
diff
changeset
|
253 export default {set_keyval, version}; |
2245
87a0e2c73a25
Refactored njs documentation.
Yaroslav Zhuravlev <yar@nginx.com>
parents:
diff
changeset
|
254 </example> |
2778
9cafae0b7ef3
Minor fixes in njs examples.
Yaroslav Zhuravlev <yar@nginx.com>
parents:
2643
diff
changeset
|
255 <note> |
9cafae0b7ef3
Minor fixes in njs examples.
Yaroslav Zhuravlev <yar@nginx.com>
parents:
2643
diff
changeset
|
256 The <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
|
257 <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
|
258 and <link doc="../http/ngx_http_api_module.xml" id="api"/> directives |
9cafae0b7ef3
Minor fixes in njs examples.
Yaroslav Zhuravlev <yar@nginx.com>
parents:
2643
diff
changeset
|
259 are available as part of our |
9cafae0b7ef3
Minor fixes in njs examples.
Yaroslav Zhuravlev <yar@nginx.com>
parents:
2643
diff
changeset
|
260 <commercial_version>commercial subscription</commercial_version>. |
9cafae0b7ef3
Minor fixes in njs examples.
Yaroslav Zhuravlev <yar@nginx.com>
parents:
2643
diff
changeset
|
261 </note> |
2245
87a0e2c73a25
Refactored njs documentation.
Yaroslav Zhuravlev <yar@nginx.com>
parents:
diff
changeset
|
262 </para> |
87a0e2c73a25
Refactored njs documentation.
Yaroslav Zhuravlev <yar@nginx.com>
parents:
diff
changeset
|
263 |
87a0e2c73a25
Refactored njs documentation.
Yaroslav Zhuravlev <yar@nginx.com>
parents:
diff
changeset
|
264 </section> |
87a0e2c73a25
Refactored njs documentation.
Yaroslav Zhuravlev <yar@nginx.com>
parents:
diff
changeset
|
265 |
87a0e2c73a25
Refactored njs documentation.
Yaroslav Zhuravlev <yar@nginx.com>
parents:
diff
changeset
|
266 |
2642
c60bcc0de435
Updated TOC in njs examples.
Yaroslav Zhuravlev <yar@nginx.com>
parents:
2640
diff
changeset
|
267 <section id="fast_response" name="Returning Fastest Response from Proxy"> |
2245
87a0e2c73a25
Refactored njs documentation.
Yaroslav Zhuravlev <yar@nginx.com>
parents:
diff
changeset
|
268 |
87a0e2c73a25
Refactored njs documentation.
Yaroslav Zhuravlev <yar@nginx.com>
parents:
diff
changeset
|
269 <para> |
87a0e2c73a25
Refactored njs documentation.
Yaroslav Zhuravlev <yar@nginx.com>
parents:
diff
changeset
|
270 <path>nginx.conf</path>: |
87a0e2c73a25
Refactored njs documentation.
Yaroslav Zhuravlev <yar@nginx.com>
parents:
diff
changeset
|
271 <example> |
2531
9c8a89d3876f
Updated njs examples with js_import and corresponding changes.
Yaroslav Zhuravlev <yar@nginx.com>
parents:
2527
diff
changeset
|
272 js_import http.js; |
2245
87a0e2c73a25
Refactored njs documentation.
Yaroslav Zhuravlev <yar@nginx.com>
parents:
diff
changeset
|
273 |
2642
c60bcc0de435
Updated TOC in njs examples.
Yaroslav Zhuravlev <yar@nginx.com>
parents:
2640
diff
changeset
|
274 location /start { |
c60bcc0de435
Updated TOC in njs examples.
Yaroslav Zhuravlev <yar@nginx.com>
parents:
2640
diff
changeset
|
275 js_content http.content; |
2245
87a0e2c73a25
Refactored njs documentation.
Yaroslav Zhuravlev <yar@nginx.com>
parents:
diff
changeset
|
276 } |
87a0e2c73a25
Refactored njs documentation.
Yaroslav Zhuravlev <yar@nginx.com>
parents:
diff
changeset
|
277 |
2642
c60bcc0de435
Updated TOC in njs examples.
Yaroslav Zhuravlev <yar@nginx.com>
parents:
2640
diff
changeset
|
278 location /foo { |
c60bcc0de435
Updated TOC in njs examples.
Yaroslav Zhuravlev <yar@nginx.com>
parents:
2640
diff
changeset
|
279 proxy_pass http://backend1; |
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 |
c60bcc0de435
Updated TOC in njs examples.
Yaroslav Zhuravlev <yar@nginx.com>
parents:
2640
diff
changeset
|
282 location /bar { |
c60bcc0de435
Updated TOC in njs examples.
Yaroslav Zhuravlev <yar@nginx.com>
parents:
2640
diff
changeset
|
283 proxy_pass http://backend2; |
2245
87a0e2c73a25
Refactored njs documentation.
Yaroslav Zhuravlev <yar@nginx.com>
parents:
diff
changeset
|
284 } |
87a0e2c73a25
Refactored njs documentation.
Yaroslav Zhuravlev <yar@nginx.com>
parents:
diff
changeset
|
285 </example> |
87a0e2c73a25
Refactored njs documentation.
Yaroslav Zhuravlev <yar@nginx.com>
parents:
diff
changeset
|
286 </para> |
87a0e2c73a25
Refactored njs documentation.
Yaroslav Zhuravlev <yar@nginx.com>
parents:
diff
changeset
|
287 |
87a0e2c73a25
Refactored njs documentation.
Yaroslav Zhuravlev <yar@nginx.com>
parents:
diff
changeset
|
288 <para> |
2531
9c8a89d3876f
Updated njs examples with js_import and corresponding changes.
Yaroslav Zhuravlev <yar@nginx.com>
parents:
2527
diff
changeset
|
289 <path>http.js</path>: |
2245
87a0e2c73a25
Refactored njs documentation.
Yaroslav Zhuravlev <yar@nginx.com>
parents:
diff
changeset
|
290 <example> |
2642
c60bcc0de435
Updated TOC in njs examples.
Yaroslav Zhuravlev <yar@nginx.com>
parents:
2640
diff
changeset
|
291 function content(r) { |
c60bcc0de435
Updated TOC in njs examples.
Yaroslav Zhuravlev <yar@nginx.com>
parents:
2640
diff
changeset
|
292 var n = 0; |
c60bcc0de435
Updated TOC in njs examples.
Yaroslav Zhuravlev <yar@nginx.com>
parents:
2640
diff
changeset
|
293 |
c60bcc0de435
Updated TOC in njs examples.
Yaroslav Zhuravlev <yar@nginx.com>
parents:
2640
diff
changeset
|
294 function done(res) { |
c60bcc0de435
Updated TOC in njs examples.
Yaroslav Zhuravlev <yar@nginx.com>
parents:
2640
diff
changeset
|
295 if (n++ == 0) { |
c60bcc0de435
Updated TOC in njs examples.
Yaroslav Zhuravlev <yar@nginx.com>
parents:
2640
diff
changeset
|
296 r.return(res.status, res.responseBody); |
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 |
c60bcc0de435
Updated TOC in njs examples.
Yaroslav Zhuravlev <yar@nginx.com>
parents:
2640
diff
changeset
|
300 r.subrequest('/foo', r.variables.args, done); |
c60bcc0de435
Updated TOC in njs examples.
Yaroslav Zhuravlev <yar@nginx.com>
parents:
2640
diff
changeset
|
301 r.subrequest('/bar', r.variables.args, done); |
c60bcc0de435
Updated TOC in njs examples.
Yaroslav Zhuravlev <yar@nginx.com>
parents:
2640
diff
changeset
|
302 } |
c60bcc0de435
Updated TOC in njs examples.
Yaroslav Zhuravlev <yar@nginx.com>
parents:
2640
diff
changeset
|
303 |
c60bcc0de435
Updated TOC in njs examples.
Yaroslav Zhuravlev <yar@nginx.com>
parents:
2640
diff
changeset
|
304 export default {content}; |
c60bcc0de435
Updated TOC in njs examples.
Yaroslav Zhuravlev <yar@nginx.com>
parents:
2640
diff
changeset
|
305 </example> |
c60bcc0de435
Updated TOC in njs examples.
Yaroslav Zhuravlev <yar@nginx.com>
parents:
2640
diff
changeset
|
306 </para> |
c60bcc0de435
Updated TOC in njs examples.
Yaroslav Zhuravlev <yar@nginx.com>
parents:
2640
diff
changeset
|
307 |
c60bcc0de435
Updated TOC in njs examples.
Yaroslav Zhuravlev <yar@nginx.com>
parents:
2640
diff
changeset
|
308 </section> |
c60bcc0de435
Updated TOC in njs examples.
Yaroslav Zhuravlev <yar@nginx.com>
parents:
2640
diff
changeset
|
309 |
c60bcc0de435
Updated TOC in njs examples.
Yaroslav Zhuravlev <yar@nginx.com>
parents:
2640
diff
changeset
|
310 |
c60bcc0de435
Updated TOC in njs examples.
Yaroslav Zhuravlev <yar@nginx.com>
parents:
2640
diff
changeset
|
311 <section id="subrequests_chaining" name="Subrequests Chaining"> |
c60bcc0de435
Updated TOC in njs examples.
Yaroslav Zhuravlev <yar@nginx.com>
parents:
2640
diff
changeset
|
312 |
c60bcc0de435
Updated TOC in njs examples.
Yaroslav Zhuravlev <yar@nginx.com>
parents:
2640
diff
changeset
|
313 <para> |
c60bcc0de435
Updated TOC in njs examples.
Yaroslav Zhuravlev <yar@nginx.com>
parents:
2640
diff
changeset
|
314 <path>nginx.conf</path>: |
c60bcc0de435
Updated TOC in njs examples.
Yaroslav Zhuravlev <yar@nginx.com>
parents:
2640
diff
changeset
|
315 <example> |
c60bcc0de435
Updated TOC in njs examples.
Yaroslav Zhuravlev <yar@nginx.com>
parents:
2640
diff
changeset
|
316 js_import http.js; |
c60bcc0de435
Updated TOC in njs examples.
Yaroslav Zhuravlev <yar@nginx.com>
parents:
2640
diff
changeset
|
317 |
c60bcc0de435
Updated TOC in njs examples.
Yaroslav Zhuravlev <yar@nginx.com>
parents:
2640
diff
changeset
|
318 location /start { |
c60bcc0de435
Updated TOC in njs examples.
Yaroslav Zhuravlev <yar@nginx.com>
parents:
2640
diff
changeset
|
319 js_content http.content; |
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 |
c60bcc0de435
Updated TOC in njs examples.
Yaroslav Zhuravlev <yar@nginx.com>
parents:
2640
diff
changeset
|
322 location /auth { |
c60bcc0de435
Updated TOC in njs examples.
Yaroslav Zhuravlev <yar@nginx.com>
parents:
2640
diff
changeset
|
323 proxy_pass http://auth_backend; |
2245
87a0e2c73a25
Refactored njs documentation.
Yaroslav Zhuravlev <yar@nginx.com>
parents:
diff
changeset
|
324 } |
2531
9c8a89d3876f
Updated njs examples with js_import and corresponding changes.
Yaroslav Zhuravlev <yar@nginx.com>
parents:
2527
diff
changeset
|
325 |
2642
c60bcc0de435
Updated TOC in njs examples.
Yaroslav Zhuravlev <yar@nginx.com>
parents:
2640
diff
changeset
|
326 location /backend { |
c60bcc0de435
Updated TOC in njs examples.
Yaroslav Zhuravlev <yar@nginx.com>
parents:
2640
diff
changeset
|
327 proxy_pass http://backend; |
c60bcc0de435
Updated TOC in njs examples.
Yaroslav Zhuravlev <yar@nginx.com>
parents:
2640
diff
changeset
|
328 } |
c60bcc0de435
Updated TOC in njs examples.
Yaroslav Zhuravlev <yar@nginx.com>
parents:
2640
diff
changeset
|
329 </example> |
c60bcc0de435
Updated TOC in njs examples.
Yaroslav Zhuravlev <yar@nginx.com>
parents:
2640
diff
changeset
|
330 </para> |
c60bcc0de435
Updated TOC in njs examples.
Yaroslav Zhuravlev <yar@nginx.com>
parents:
2640
diff
changeset
|
331 |
c60bcc0de435
Updated TOC in njs examples.
Yaroslav Zhuravlev <yar@nginx.com>
parents:
2640
diff
changeset
|
332 <para> |
c60bcc0de435
Updated TOC in njs examples.
Yaroslav Zhuravlev <yar@nginx.com>
parents:
2640
diff
changeset
|
333 <path>http.js</path>: |
c60bcc0de435
Updated TOC in njs examples.
Yaroslav Zhuravlev <yar@nginx.com>
parents:
2640
diff
changeset
|
334 <example> |
2799
24b379907b0f
Added async and await to njs Examples.
Yaroslav Zhuravlev <yar@nginx.com>
parents:
2780
diff
changeset
|
335 async function content(r) { |
24b379907b0f
Added async and await to njs Examples.
Yaroslav Zhuravlev <yar@nginx.com>
parents:
2780
diff
changeset
|
336 try { |
24b379907b0f
Added async and await to njs Examples.
Yaroslav Zhuravlev <yar@nginx.com>
parents:
2780
diff
changeset
|
337 let reply = await r.subrequest('/auth'); |
24b379907b0f
Added async and await to njs Examples.
Yaroslav Zhuravlev <yar@nginx.com>
parents:
2780
diff
changeset
|
338 let response = JSON.parse(reply.responseBody); |
24b379907b0f
Added async and await to njs Examples.
Yaroslav Zhuravlev <yar@nginx.com>
parents:
2780
diff
changeset
|
339 let token = response['token']; |
24b379907b0f
Added async and await to njs Examples.
Yaroslav Zhuravlev <yar@nginx.com>
parents:
2780
diff
changeset
|
340 |
24b379907b0f
Added async and await to njs Examples.
Yaroslav Zhuravlev <yar@nginx.com>
parents:
2780
diff
changeset
|
341 if (!token) { |
24b379907b0f
Added async and await to njs Examples.
Yaroslav Zhuravlev <yar@nginx.com>
parents:
2780
diff
changeset
|
342 throw new Error("token is not available"); |
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 |
24b379907b0f
Added async and await to njs Examples.
Yaroslav Zhuravlev <yar@nginx.com>
parents:
2780
diff
changeset
|
345 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
|
346 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
|
347 |
24b379907b0f
Added async and await to njs Examples.
Yaroslav Zhuravlev <yar@nginx.com>
parents:
2780
diff
changeset
|
348 } catch (e) { |
24b379907b0f
Added async and await to njs Examples.
Yaroslav Zhuravlev <yar@nginx.com>
parents:
2780
diff
changeset
|
349 r.error(e); |
24b379907b0f
Added async and await to njs Examples.
Yaroslav Zhuravlev <yar@nginx.com>
parents:
2780
diff
changeset
|
350 r.return(500); |
24b379907b0f
Added async and await to njs Examples.
Yaroslav Zhuravlev <yar@nginx.com>
parents:
2780
diff
changeset
|
351 } |
2642
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 |
c60bcc0de435
Updated TOC in njs examples.
Yaroslav Zhuravlev <yar@nginx.com>
parents:
2640
diff
changeset
|
354 export default {content}; |
c60bcc0de435
Updated TOC in njs examples.
Yaroslav Zhuravlev <yar@nginx.com>
parents:
2640
diff
changeset
|
355 </example> |
c60bcc0de435
Updated TOC in njs examples.
Yaroslav Zhuravlev <yar@nginx.com>
parents:
2640
diff
changeset
|
356 </para> |
c60bcc0de435
Updated TOC in njs examples.
Yaroslav Zhuravlev <yar@nginx.com>
parents:
2640
diff
changeset
|
357 |
c60bcc0de435
Updated TOC in njs examples.
Yaroslav Zhuravlev <yar@nginx.com>
parents:
2640
diff
changeset
|
358 </section> |
c60bcc0de435
Updated TOC in njs examples.
Yaroslav Zhuravlev <yar@nginx.com>
parents:
2640
diff
changeset
|
359 |
c60bcc0de435
Updated TOC in njs examples.
Yaroslav Zhuravlev <yar@nginx.com>
parents:
2640
diff
changeset
|
360 </section> |
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 |
c60bcc0de435
Updated TOC in njs examples.
Yaroslav Zhuravlev <yar@nginx.com>
parents:
2640
diff
changeset
|
363 <section id="misc" name="Miscellaneous"> |
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 |
c60bcc0de435
Updated TOC in njs examples.
Yaroslav Zhuravlev <yar@nginx.com>
parents:
2640
diff
changeset
|
366 <section id="redirect" name="Internal Redirect"> |
c60bcc0de435
Updated TOC in njs examples.
Yaroslav Zhuravlev <yar@nginx.com>
parents:
2640
diff
changeset
|
367 |
c60bcc0de435
Updated TOC in njs examples.
Yaroslav Zhuravlev <yar@nginx.com>
parents:
2640
diff
changeset
|
368 <para> |
c60bcc0de435
Updated TOC in njs examples.
Yaroslav Zhuravlev <yar@nginx.com>
parents:
2640
diff
changeset
|
369 <path>nginx.conf</path>: |
c60bcc0de435
Updated TOC in njs examples.
Yaroslav Zhuravlev <yar@nginx.com>
parents:
2640
diff
changeset
|
370 <example> |
c60bcc0de435
Updated TOC in njs examples.
Yaroslav Zhuravlev <yar@nginx.com>
parents:
2640
diff
changeset
|
371 js_import http.js; |
c60bcc0de435
Updated TOC in njs examples.
Yaroslav Zhuravlev <yar@nginx.com>
parents:
2640
diff
changeset
|
372 |
c60bcc0de435
Updated TOC in njs examples.
Yaroslav Zhuravlev <yar@nginx.com>
parents:
2640
diff
changeset
|
373 location /redirect { |
c60bcc0de435
Updated TOC in njs examples.
Yaroslav Zhuravlev <yar@nginx.com>
parents:
2640
diff
changeset
|
374 js_content http.redirect; |
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 |
c60bcc0de435
Updated TOC in njs examples.
Yaroslav Zhuravlev <yar@nginx.com>
parents:
2640
diff
changeset
|
377 location @named { |
c60bcc0de435
Updated TOC in njs examples.
Yaroslav Zhuravlev <yar@nginx.com>
parents:
2640
diff
changeset
|
378 return 200 named; |
c60bcc0de435
Updated TOC in njs examples.
Yaroslav Zhuravlev <yar@nginx.com>
parents:
2640
diff
changeset
|
379 } |
c60bcc0de435
Updated TOC in njs examples.
Yaroslav Zhuravlev <yar@nginx.com>
parents:
2640
diff
changeset
|
380 </example> |
c60bcc0de435
Updated TOC in njs examples.
Yaroslav Zhuravlev <yar@nginx.com>
parents:
2640
diff
changeset
|
381 </para> |
c60bcc0de435
Updated TOC in njs examples.
Yaroslav Zhuravlev <yar@nginx.com>
parents:
2640
diff
changeset
|
382 |
c60bcc0de435
Updated TOC in njs examples.
Yaroslav Zhuravlev <yar@nginx.com>
parents:
2640
diff
changeset
|
383 <para> |
c60bcc0de435
Updated TOC in njs examples.
Yaroslav Zhuravlev <yar@nginx.com>
parents:
2640
diff
changeset
|
384 <path>http.js</path>: |
c60bcc0de435
Updated TOC in njs examples.
Yaroslav Zhuravlev <yar@nginx.com>
parents:
2640
diff
changeset
|
385 <example> |
c60bcc0de435
Updated TOC in njs examples.
Yaroslav Zhuravlev <yar@nginx.com>
parents:
2640
diff
changeset
|
386 function redirect(r) { |
c60bcc0de435
Updated TOC in njs examples.
Yaroslav Zhuravlev <yar@nginx.com>
parents:
2640
diff
changeset
|
387 r.internalRedirect('@named'); |
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 |
c60bcc0de435
Updated TOC in njs examples.
Yaroslav Zhuravlev <yar@nginx.com>
parents:
2640
diff
changeset
|
390 export default {redirect}; |
2245
87a0e2c73a25
Refactored njs documentation.
Yaroslav Zhuravlev <yar@nginx.com>
parents:
diff
changeset
|
391 </example> |
87a0e2c73a25
Refactored njs documentation.
Yaroslav Zhuravlev <yar@nginx.com>
parents:
diff
changeset
|
392 </para> |
87a0e2c73a25
Refactored njs documentation.
Yaroslav Zhuravlev <yar@nginx.com>
parents:
diff
changeset
|
393 |
87a0e2c73a25
Refactored njs documentation.
Yaroslav Zhuravlev <yar@nginx.com>
parents:
diff
changeset
|
394 </section> |
87a0e2c73a25
Refactored njs documentation.
Yaroslav Zhuravlev <yar@nginx.com>
parents:
diff
changeset
|
395 |
2454
253641e268a8
Added js_requests.js example in njs.
Yaroslav Zhuravlev <yar@nginx.com>
parents:
2438
diff
changeset
|
396 |
253641e268a8
Added js_requests.js example in njs.
Yaroslav Zhuravlev <yar@nginx.com>
parents:
2438
diff
changeset
|
397 <section id="requests" name="Logging the Number of Requests Per Client"> |
253641e268a8
Added js_requests.js example in njs.
Yaroslav Zhuravlev <yar@nginx.com>
parents:
2438
diff
changeset
|
398 |
253641e268a8
Added js_requests.js example in njs.
Yaroslav Zhuravlev <yar@nginx.com>
parents:
2438
diff
changeset
|
399 <para> |
253641e268a8
Added js_requests.js example in njs.
Yaroslav Zhuravlev <yar@nginx.com>
parents:
2438
diff
changeset
|
400 <path>nginx.conf</path>: |
253641e268a8
Added js_requests.js example in njs.
Yaroslav Zhuravlev <yar@nginx.com>
parents:
2438
diff
changeset
|
401 <example> |
2531
9c8a89d3876f
Updated njs examples with js_import and corresponding changes.
Yaroslav Zhuravlev <yar@nginx.com>
parents:
2527
diff
changeset
|
402 js_import http.js; |
2454
253641e268a8
Added js_requests.js example in njs.
Yaroslav Zhuravlev <yar@nginx.com>
parents:
2438
diff
changeset
|
403 |
2531
9c8a89d3876f
Updated njs examples with js_import and corresponding changes.
Yaroslav Zhuravlev <yar@nginx.com>
parents:
2527
diff
changeset
|
404 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
|
405 |
253641e268a8
Added js_requests.js example in njs.
Yaroslav Zhuravlev <yar@nginx.com>
parents:
2438
diff
changeset
|
406 keyval_zone zone=foo:10m; |
253641e268a8
Added js_requests.js example in njs.
Yaroslav Zhuravlev <yar@nginx.com>
parents:
2438
diff
changeset
|
407 |
253641e268a8
Added js_requests.js example in njs.
Yaroslav Zhuravlev <yar@nginx.com>
parents:
2438
diff
changeset
|
408 keyval $remote_addr $foo zone=foo; |
253641e268a8
Added js_requests.js example in njs.
Yaroslav Zhuravlev <yar@nginx.com>
parents:
2438
diff
changeset
|
409 |
253641e268a8
Added js_requests.js example in njs.
Yaroslav Zhuravlev <yar@nginx.com>
parents:
2438
diff
changeset
|
410 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
|
411 access_log logs/access.log bar; |
253641e268a8
Added js_requests.js example in njs.
Yaroslav Zhuravlev <yar@nginx.com>
parents:
2438
diff
changeset
|
412 |
253641e268a8
Added js_requests.js example in njs.
Yaroslav Zhuravlev <yar@nginx.com>
parents:
2438
diff
changeset
|
413 server { |
253641e268a8
Added js_requests.js example in njs.
Yaroslav Zhuravlev <yar@nginx.com>
parents:
2438
diff
changeset
|
414 listen 8000; |
253641e268a8
Added js_requests.js example in njs.
Yaroslav Zhuravlev <yar@nginx.com>
parents:
2438
diff
changeset
|
415 |
253641e268a8
Added js_requests.js example in njs.
Yaroslav Zhuravlev <yar@nginx.com>
parents:
2438
diff
changeset
|
416 location / { |
253641e268a8
Added js_requests.js example in njs.
Yaroslav Zhuravlev <yar@nginx.com>
parents:
2438
diff
changeset
|
417 root html; |
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 } |
253641e268a8
Added js_requests.js example in njs.
Yaroslav Zhuravlev <yar@nginx.com>
parents:
2438
diff
changeset
|
420 </example> |
253641e268a8
Added js_requests.js example in njs.
Yaroslav Zhuravlev <yar@nginx.com>
parents:
2438
diff
changeset
|
421 </para> |
253641e268a8
Added js_requests.js example in njs.
Yaroslav Zhuravlev <yar@nginx.com>
parents:
2438
diff
changeset
|
422 |
253641e268a8
Added js_requests.js example in njs.
Yaroslav Zhuravlev <yar@nginx.com>
parents:
2438
diff
changeset
|
423 <para> |
2531
9c8a89d3876f
Updated njs examples with js_import and corresponding changes.
Yaroslav Zhuravlev <yar@nginx.com>
parents:
2527
diff
changeset
|
424 <path>http.js</path>: |
2454
253641e268a8
Added js_requests.js example in njs.
Yaroslav Zhuravlev <yar@nginx.com>
parents:
2438
diff
changeset
|
425 <example> |
253641e268a8
Added js_requests.js example in njs.
Yaroslav Zhuravlev <yar@nginx.com>
parents:
2438
diff
changeset
|
426 function num_requests(r) |
253641e268a8
Added js_requests.js example in njs.
Yaroslav Zhuravlev <yar@nginx.com>
parents:
2438
diff
changeset
|
427 { |
253641e268a8
Added js_requests.js example in njs.
Yaroslav Zhuravlev <yar@nginx.com>
parents:
2438
diff
changeset
|
428 var n = r.variables.foo; |
253641e268a8
Added js_requests.js example in njs.
Yaroslav Zhuravlev <yar@nginx.com>
parents:
2438
diff
changeset
|
429 n = n ? Number(n) + 1 : 1; |
253641e268a8
Added js_requests.js example in njs.
Yaroslav Zhuravlev <yar@nginx.com>
parents:
2438
diff
changeset
|
430 r.variables.foo = n; |
253641e268a8
Added js_requests.js example in njs.
Yaroslav Zhuravlev <yar@nginx.com>
parents:
2438
diff
changeset
|
431 return n; |
253641e268a8
Added js_requests.js example in njs.
Yaroslav Zhuravlev <yar@nginx.com>
parents:
2438
diff
changeset
|
432 } |
2531
9c8a89d3876f
Updated njs examples with js_import and corresponding changes.
Yaroslav Zhuravlev <yar@nginx.com>
parents:
2527
diff
changeset
|
433 |
9c8a89d3876f
Updated njs examples with js_import and corresponding changes.
Yaroslav Zhuravlev <yar@nginx.com>
parents:
2527
diff
changeset
|
434 export default {num_requests}; |
2454
253641e268a8
Added js_requests.js example in njs.
Yaroslav Zhuravlev <yar@nginx.com>
parents:
2438
diff
changeset
|
435 </example> |
253641e268a8
Added js_requests.js example in njs.
Yaroslav Zhuravlev <yar@nginx.com>
parents:
2438
diff
changeset
|
436 <note> |
253641e268a8
Added js_requests.js example in njs.
Yaroslav Zhuravlev <yar@nginx.com>
parents:
2438
diff
changeset
|
437 The <link doc="../http/ngx_http_keyval_module.xml" id="keyval"/> and |
253641e268a8
Added js_requests.js example in njs.
Yaroslav Zhuravlev <yar@nginx.com>
parents:
2438
diff
changeset
|
438 <link doc="../http/ngx_http_keyval_module.xml" id="keyval_zone"/> directives |
253641e268a8
Added js_requests.js example in njs.
Yaroslav Zhuravlev <yar@nginx.com>
parents:
2438
diff
changeset
|
439 are available as part of our |
253641e268a8
Added js_requests.js example in njs.
Yaroslav Zhuravlev <yar@nginx.com>
parents:
2438
diff
changeset
|
440 <commercial_version>commercial subscription</commercial_version>. |
253641e268a8
Added js_requests.js example in njs.
Yaroslav Zhuravlev <yar@nginx.com>
parents:
2438
diff
changeset
|
441 </note> |
253641e268a8
Added js_requests.js example in njs.
Yaroslav Zhuravlev <yar@nginx.com>
parents:
2438
diff
changeset
|
442 </para> |
253641e268a8
Added js_requests.js example in njs.
Yaroslav Zhuravlev <yar@nginx.com>
parents:
2438
diff
changeset
|
443 |
253641e268a8
Added js_requests.js example in njs.
Yaroslav Zhuravlev <yar@nginx.com>
parents:
2438
diff
changeset
|
444 </section> |
253641e268a8
Added js_requests.js example in njs.
Yaroslav Zhuravlev <yar@nginx.com>
parents:
2438
diff
changeset
|
445 |
2642
c60bcc0de435
Updated TOC in njs examples.
Yaroslav Zhuravlev <yar@nginx.com>
parents:
2640
diff
changeset
|
446 </section> |
c60bcc0de435
Updated TOC in njs examples.
Yaroslav Zhuravlev <yar@nginx.com>
parents:
2640
diff
changeset
|
447 |
2245
87a0e2c73a25
Refactored njs documentation.
Yaroslav Zhuravlev <yar@nginx.com>
parents:
diff
changeset
|
448 </article> |