Mercurial > hg > nginx-site
annotate xml/en/docs/njs/examples.xml @ 2554:62523c26ddf3
Updated OpenSSL version used for win32 builds.
author | Yaroslav Zhuravlev <yar@nginx.com> |
---|---|
date | Thu, 18 Jun 2020 13:40:27 +0100 |
parents | 87f34fafa4e8 |
children | c81b1fadbcf3 |
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" |
2536
87f34fafa4e8
Removed load_module from "Hello World" example in njs.
Yaroslav Zhuravlev <yar@nginx.com>
parents:
2531
diff
changeset
|
12 rev="14"> |
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; |
9c8a89d3876f
Updated njs examples with js_import and corresponding changes.
Yaroslav Zhuravlev <yar@nginx.com>
parents:
2527
diff
changeset
|
33 js_content http.hello; |
2245
87a0e2c73a25
Refactored njs documentation.
Yaroslav Zhuravlev <yar@nginx.com>
parents:
diff
changeset
|
34 } |
87a0e2c73a25
Refactored njs documentation.
Yaroslav Zhuravlev <yar@nginx.com>
parents:
diff
changeset
|
35 |
87a0e2c73a25
Refactored njs documentation.
Yaroslav Zhuravlev <yar@nginx.com>
parents:
diff
changeset
|
36 </example> |
87a0e2c73a25
Refactored njs documentation.
Yaroslav Zhuravlev <yar@nginx.com>
parents:
diff
changeset
|
37 </para> |
87a0e2c73a25
Refactored njs documentation.
Yaroslav Zhuravlev <yar@nginx.com>
parents:
diff
changeset
|
38 |
87a0e2c73a25
Refactored njs documentation.
Yaroslav Zhuravlev <yar@nginx.com>
parents:
diff
changeset
|
39 <para> |
2531
9c8a89d3876f
Updated njs examples with js_import and corresponding changes.
Yaroslav Zhuravlev <yar@nginx.com>
parents:
2527
diff
changeset
|
40 <literal>http.js</literal>: |
2245
87a0e2c73a25
Refactored njs documentation.
Yaroslav Zhuravlev <yar@nginx.com>
parents:
diff
changeset
|
41 <example> |
87a0e2c73a25
Refactored njs documentation.
Yaroslav Zhuravlev <yar@nginx.com>
parents:
diff
changeset
|
42 function hello(r) { |
2257
8cef5ef98336
Changed quotes to ASCII in NJS example.
Nick Shadrin <nick@nginx.com>
parents:
2246
diff
changeset
|
43 r.return(200, "Hello world!"); |
2245
87a0e2c73a25
Refactored njs documentation.
Yaroslav Zhuravlev <yar@nginx.com>
parents:
diff
changeset
|
44 } |
2531
9c8a89d3876f
Updated njs examples with js_import and corresponding changes.
Yaroslav Zhuravlev <yar@nginx.com>
parents:
2527
diff
changeset
|
45 |
9c8a89d3876f
Updated njs examples with js_import and corresponding changes.
Yaroslav Zhuravlev <yar@nginx.com>
parents:
2527
diff
changeset
|
46 export default {hello}; |
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 </para> |
87a0e2c73a25
Refactored njs documentation.
Yaroslav Zhuravlev <yar@nginx.com>
parents:
diff
changeset
|
49 |
87a0e2c73a25
Refactored njs documentation.
Yaroslav Zhuravlev <yar@nginx.com>
parents:
diff
changeset
|
50 </section> |
87a0e2c73a25
Refactored njs documentation.
Yaroslav Zhuravlev <yar@nginx.com>
parents:
diff
changeset
|
51 |
87a0e2c73a25
Refactored njs documentation.
Yaroslav Zhuravlev <yar@nginx.com>
parents:
diff
changeset
|
52 |
87a0e2c73a25
Refactored njs documentation.
Yaroslav Zhuravlev <yar@nginx.com>
parents:
diff
changeset
|
53 <section id="urldecode" name="URL Decoding"> |
87a0e2c73a25
Refactored njs documentation.
Yaroslav Zhuravlev <yar@nginx.com>
parents:
diff
changeset
|
54 |
87a0e2c73a25
Refactored njs documentation.
Yaroslav Zhuravlev <yar@nginx.com>
parents:
diff
changeset
|
55 <para> |
87a0e2c73a25
Refactored njs documentation.
Yaroslav Zhuravlev <yar@nginx.com>
parents:
diff
changeset
|
56 <path>nginx.conf</path>: |
87a0e2c73a25
Refactored njs documentation.
Yaroslav Zhuravlev <yar@nginx.com>
parents:
diff
changeset
|
57 <example> |
2531
9c8a89d3876f
Updated njs examples with js_import and corresponding changes.
Yaroslav Zhuravlev <yar@nginx.com>
parents:
2527
diff
changeset
|
58 js_import http.js; |
2245
87a0e2c73a25
Refactored njs documentation.
Yaroslav Zhuravlev <yar@nginx.com>
parents:
diff
changeset
|
59 |
2531
9c8a89d3876f
Updated njs examples with js_import and corresponding changes.
Yaroslav Zhuravlev <yar@nginx.com>
parents:
2527
diff
changeset
|
60 js_set $decoded_foo http.decoded_foo; |
2245
87a0e2c73a25
Refactored njs documentation.
Yaroslav Zhuravlev <yar@nginx.com>
parents:
diff
changeset
|
61 </example> |
87a0e2c73a25
Refactored njs documentation.
Yaroslav Zhuravlev <yar@nginx.com>
parents:
diff
changeset
|
62 </para> |
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> |
2531
9c8a89d3876f
Updated njs examples with js_import and corresponding changes.
Yaroslav Zhuravlev <yar@nginx.com>
parents:
2527
diff
changeset
|
65 <path>http.js</path>: |
2245
87a0e2c73a25
Refactored njs documentation.
Yaroslav Zhuravlev <yar@nginx.com>
parents:
diff
changeset
|
66 <example> |
87a0e2c73a25
Refactored njs documentation.
Yaroslav Zhuravlev <yar@nginx.com>
parents:
diff
changeset
|
67 function decoded_foo(r) { |
87a0e2c73a25
Refactored njs documentation.
Yaroslav Zhuravlev <yar@nginx.com>
parents:
diff
changeset
|
68 return decodeURIComponent(r.args.foo); |
87a0e2c73a25
Refactored njs documentation.
Yaroslav Zhuravlev <yar@nginx.com>
parents:
diff
changeset
|
69 } |
2531
9c8a89d3876f
Updated njs examples with js_import and corresponding changes.
Yaroslav Zhuravlev <yar@nginx.com>
parents:
2527
diff
changeset
|
70 |
9c8a89d3876f
Updated njs examples with js_import and corresponding changes.
Yaroslav Zhuravlev <yar@nginx.com>
parents:
2527
diff
changeset
|
71 export default {decoded_foo}; |
2245
87a0e2c73a25
Refactored njs documentation.
Yaroslav Zhuravlev <yar@nginx.com>
parents:
diff
changeset
|
72 </example> |
87a0e2c73a25
Refactored njs documentation.
Yaroslav Zhuravlev <yar@nginx.com>
parents:
diff
changeset
|
73 </para> |
87a0e2c73a25
Refactored njs documentation.
Yaroslav Zhuravlev <yar@nginx.com>
parents:
diff
changeset
|
74 |
87a0e2c73a25
Refactored njs documentation.
Yaroslav Zhuravlev <yar@nginx.com>
parents:
diff
changeset
|
75 </section> |
87a0e2c73a25
Refactored njs documentation.
Yaroslav Zhuravlev <yar@nginx.com>
parents:
diff
changeset
|
76 |
87a0e2c73a25
Refactored njs documentation.
Yaroslav Zhuravlev <yar@nginx.com>
parents:
diff
changeset
|
77 |
87a0e2c73a25
Refactored njs documentation.
Yaroslav Zhuravlev <yar@nginx.com>
parents:
diff
changeset
|
78 <section id="urlencode" name="URL Encoding"> |
87a0e2c73a25
Refactored njs documentation.
Yaroslav Zhuravlev <yar@nginx.com>
parents:
diff
changeset
|
79 |
87a0e2c73a25
Refactored njs documentation.
Yaroslav Zhuravlev <yar@nginx.com>
parents:
diff
changeset
|
80 <para> |
87a0e2c73a25
Refactored njs documentation.
Yaroslav Zhuravlev <yar@nginx.com>
parents:
diff
changeset
|
81 <path>nginx.conf</path>: |
87a0e2c73a25
Refactored njs documentation.
Yaroslav Zhuravlev <yar@nginx.com>
parents:
diff
changeset
|
82 <example> |
2531
9c8a89d3876f
Updated njs examples with js_import and corresponding changes.
Yaroslav Zhuravlev <yar@nginx.com>
parents:
2527
diff
changeset
|
83 js_import http.js; |
2245
87a0e2c73a25
Refactored njs documentation.
Yaroslav Zhuravlev <yar@nginx.com>
parents:
diff
changeset
|
84 |
2531
9c8a89d3876f
Updated njs examples with js_import and corresponding changes.
Yaroslav Zhuravlev <yar@nginx.com>
parents:
2527
diff
changeset
|
85 js_set $encoded_foo http.encoded_foo; |
2245
87a0e2c73a25
Refactored njs documentation.
Yaroslav Zhuravlev <yar@nginx.com>
parents:
diff
changeset
|
86 ... |
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 location / { |
87a0e2c73a25
Refactored njs documentation.
Yaroslav Zhuravlev <yar@nginx.com>
parents:
diff
changeset
|
89 proxy_pass http://example.com?foo=$encoded_foo; |
87a0e2c73a25
Refactored njs documentation.
Yaroslav Zhuravlev <yar@nginx.com>
parents:
diff
changeset
|
90 } |
87a0e2c73a25
Refactored njs documentation.
Yaroslav Zhuravlev <yar@nginx.com>
parents:
diff
changeset
|
91 </example> |
87a0e2c73a25
Refactored njs documentation.
Yaroslav Zhuravlev <yar@nginx.com>
parents:
diff
changeset
|
92 </para> |
87a0e2c73a25
Refactored njs documentation.
Yaroslav Zhuravlev <yar@nginx.com>
parents:
diff
changeset
|
93 |
87a0e2c73a25
Refactored njs documentation.
Yaroslav Zhuravlev <yar@nginx.com>
parents:
diff
changeset
|
94 <para> |
2531
9c8a89d3876f
Updated njs examples with js_import and corresponding changes.
Yaroslav Zhuravlev <yar@nginx.com>
parents:
2527
diff
changeset
|
95 <path>http.js</path>: |
2245
87a0e2c73a25
Refactored njs documentation.
Yaroslav Zhuravlev <yar@nginx.com>
parents:
diff
changeset
|
96 <example> |
87a0e2c73a25
Refactored njs documentation.
Yaroslav Zhuravlev <yar@nginx.com>
parents:
diff
changeset
|
97 function encoded_foo(r) { |
87a0e2c73a25
Refactored njs documentation.
Yaroslav Zhuravlev <yar@nginx.com>
parents:
diff
changeset
|
98 return encodeURIComponent('foo & bar?'); |
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 {encoded_foo}; |
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 |
87a0e2c73a25
Refactored njs documentation.
Yaroslav Zhuravlev <yar@nginx.com>
parents:
diff
changeset
|
108 <section id="redirect" name="Internal Redirect"> |
87a0e2c73a25
Refactored njs documentation.
Yaroslav Zhuravlev <yar@nginx.com>
parents:
diff
changeset
|
109 |
87a0e2c73a25
Refactored njs documentation.
Yaroslav Zhuravlev <yar@nginx.com>
parents:
diff
changeset
|
110 <para> |
87a0e2c73a25
Refactored njs documentation.
Yaroslav Zhuravlev <yar@nginx.com>
parents:
diff
changeset
|
111 <path>nginx.conf</path>: |
87a0e2c73a25
Refactored njs documentation.
Yaroslav Zhuravlev <yar@nginx.com>
parents:
diff
changeset
|
112 <example> |
2531
9c8a89d3876f
Updated njs examples with js_import and corresponding changes.
Yaroslav Zhuravlev <yar@nginx.com>
parents:
2527
diff
changeset
|
113 js_import http.js; |
2245
87a0e2c73a25
Refactored njs documentation.
Yaroslav Zhuravlev <yar@nginx.com>
parents:
diff
changeset
|
114 |
87a0e2c73a25
Refactored njs documentation.
Yaroslav Zhuravlev <yar@nginx.com>
parents:
diff
changeset
|
115 location /redirect { |
2531
9c8a89d3876f
Updated njs examples with js_import and corresponding changes.
Yaroslav Zhuravlev <yar@nginx.com>
parents:
2527
diff
changeset
|
116 js_content http.redirect; |
2245
87a0e2c73a25
Refactored njs documentation.
Yaroslav Zhuravlev <yar@nginx.com>
parents:
diff
changeset
|
117 } |
87a0e2c73a25
Refactored njs documentation.
Yaroslav Zhuravlev <yar@nginx.com>
parents:
diff
changeset
|
118 |
87a0e2c73a25
Refactored njs documentation.
Yaroslav Zhuravlev <yar@nginx.com>
parents:
diff
changeset
|
119 location @named { |
87a0e2c73a25
Refactored njs documentation.
Yaroslav Zhuravlev <yar@nginx.com>
parents:
diff
changeset
|
120 return 200 named; |
87a0e2c73a25
Refactored njs documentation.
Yaroslav Zhuravlev <yar@nginx.com>
parents:
diff
changeset
|
121 } |
87a0e2c73a25
Refactored njs documentation.
Yaroslav Zhuravlev <yar@nginx.com>
parents:
diff
changeset
|
122 </example> |
87a0e2c73a25
Refactored njs documentation.
Yaroslav Zhuravlev <yar@nginx.com>
parents:
diff
changeset
|
123 </para> |
87a0e2c73a25
Refactored njs documentation.
Yaroslav Zhuravlev <yar@nginx.com>
parents:
diff
changeset
|
124 |
87a0e2c73a25
Refactored njs documentation.
Yaroslav Zhuravlev <yar@nginx.com>
parents:
diff
changeset
|
125 <para> |
2531
9c8a89d3876f
Updated njs examples with js_import and corresponding changes.
Yaroslav Zhuravlev <yar@nginx.com>
parents:
2527
diff
changeset
|
126 <path>http.js</path>: |
2245
87a0e2c73a25
Refactored njs documentation.
Yaroslav Zhuravlev <yar@nginx.com>
parents:
diff
changeset
|
127 <example> |
87a0e2c73a25
Refactored njs documentation.
Yaroslav Zhuravlev <yar@nginx.com>
parents:
diff
changeset
|
128 function redirect(r) { |
87a0e2c73a25
Refactored njs documentation.
Yaroslav Zhuravlev <yar@nginx.com>
parents:
diff
changeset
|
129 r.internalRedirect('@named'); |
87a0e2c73a25
Refactored njs documentation.
Yaroslav Zhuravlev <yar@nginx.com>
parents:
diff
changeset
|
130 } |
2531
9c8a89d3876f
Updated njs examples with js_import and corresponding changes.
Yaroslav Zhuravlev <yar@nginx.com>
parents:
2527
diff
changeset
|
131 |
9c8a89d3876f
Updated njs examples with js_import and corresponding changes.
Yaroslav Zhuravlev <yar@nginx.com>
parents:
2527
diff
changeset
|
132 export default {redirect}; |
2245
87a0e2c73a25
Refactored njs documentation.
Yaroslav Zhuravlev <yar@nginx.com>
parents:
diff
changeset
|
133 </example> |
87a0e2c73a25
Refactored njs documentation.
Yaroslav Zhuravlev <yar@nginx.com>
parents:
diff
changeset
|
134 </para> |
87a0e2c73a25
Refactored njs documentation.
Yaroslav Zhuravlev <yar@nginx.com>
parents:
diff
changeset
|
135 |
87a0e2c73a25
Refactored njs documentation.
Yaroslav Zhuravlev <yar@nginx.com>
parents:
diff
changeset
|
136 </section> |
87a0e2c73a25
Refactored njs documentation.
Yaroslav Zhuravlev <yar@nginx.com>
parents:
diff
changeset
|
137 |
87a0e2c73a25
Refactored njs documentation.
Yaroslav Zhuravlev <yar@nginx.com>
parents:
diff
changeset
|
138 |
87a0e2c73a25
Refactored njs documentation.
Yaroslav Zhuravlev <yar@nginx.com>
parents:
diff
changeset
|
139 <section id="fast_response" name="Returning Fastest Response from Proxy"> |
87a0e2c73a25
Refactored njs documentation.
Yaroslav Zhuravlev <yar@nginx.com>
parents:
diff
changeset
|
140 |
87a0e2c73a25
Refactored njs documentation.
Yaroslav Zhuravlev <yar@nginx.com>
parents:
diff
changeset
|
141 <para> |
87a0e2c73a25
Refactored njs documentation.
Yaroslav Zhuravlev <yar@nginx.com>
parents:
diff
changeset
|
142 <path>nginx.conf</path>: |
87a0e2c73a25
Refactored njs documentation.
Yaroslav Zhuravlev <yar@nginx.com>
parents:
diff
changeset
|
143 <example> |
2531
9c8a89d3876f
Updated njs examples with js_import and corresponding changes.
Yaroslav Zhuravlev <yar@nginx.com>
parents:
2527
diff
changeset
|
144 js_import http.js; |
2245
87a0e2c73a25
Refactored njs documentation.
Yaroslav Zhuravlev <yar@nginx.com>
parents:
diff
changeset
|
145 |
87a0e2c73a25
Refactored njs documentation.
Yaroslav Zhuravlev <yar@nginx.com>
parents:
diff
changeset
|
146 location /start { |
2531
9c8a89d3876f
Updated njs examples with js_import and corresponding changes.
Yaroslav Zhuravlev <yar@nginx.com>
parents:
2527
diff
changeset
|
147 js_content http.content; |
2245
87a0e2c73a25
Refactored njs documentation.
Yaroslav Zhuravlev <yar@nginx.com>
parents:
diff
changeset
|
148 } |
87a0e2c73a25
Refactored njs documentation.
Yaroslav Zhuravlev <yar@nginx.com>
parents:
diff
changeset
|
149 |
87a0e2c73a25
Refactored njs documentation.
Yaroslav Zhuravlev <yar@nginx.com>
parents:
diff
changeset
|
150 location /foo { |
87a0e2c73a25
Refactored njs documentation.
Yaroslav Zhuravlev <yar@nginx.com>
parents:
diff
changeset
|
151 proxy_pass http://backend1; |
87a0e2c73a25
Refactored njs documentation.
Yaroslav Zhuravlev <yar@nginx.com>
parents:
diff
changeset
|
152 } |
87a0e2c73a25
Refactored njs documentation.
Yaroslav Zhuravlev <yar@nginx.com>
parents:
diff
changeset
|
153 |
87a0e2c73a25
Refactored njs documentation.
Yaroslav Zhuravlev <yar@nginx.com>
parents:
diff
changeset
|
154 location /bar { |
87a0e2c73a25
Refactored njs documentation.
Yaroslav Zhuravlev <yar@nginx.com>
parents:
diff
changeset
|
155 proxy_pass http://backend2; |
87a0e2c73a25
Refactored njs documentation.
Yaroslav Zhuravlev <yar@nginx.com>
parents:
diff
changeset
|
156 } |
87a0e2c73a25
Refactored njs documentation.
Yaroslav Zhuravlev <yar@nginx.com>
parents:
diff
changeset
|
157 </example> |
87a0e2c73a25
Refactored njs documentation.
Yaroslav Zhuravlev <yar@nginx.com>
parents:
diff
changeset
|
158 </para> |
87a0e2c73a25
Refactored njs documentation.
Yaroslav Zhuravlev <yar@nginx.com>
parents:
diff
changeset
|
159 |
87a0e2c73a25
Refactored njs documentation.
Yaroslav Zhuravlev <yar@nginx.com>
parents:
diff
changeset
|
160 <para> |
2531
9c8a89d3876f
Updated njs examples with js_import and corresponding changes.
Yaroslav Zhuravlev <yar@nginx.com>
parents:
2527
diff
changeset
|
161 <path>http.js</path>: |
2245
87a0e2c73a25
Refactored njs documentation.
Yaroslav Zhuravlev <yar@nginx.com>
parents:
diff
changeset
|
162 <example> |
87a0e2c73a25
Refactored njs documentation.
Yaroslav Zhuravlev <yar@nginx.com>
parents:
diff
changeset
|
163 function content(r) { |
87a0e2c73a25
Refactored njs documentation.
Yaroslav Zhuravlev <yar@nginx.com>
parents:
diff
changeset
|
164 var n = 0; |
87a0e2c73a25
Refactored njs documentation.
Yaroslav Zhuravlev <yar@nginx.com>
parents:
diff
changeset
|
165 |
87a0e2c73a25
Refactored njs documentation.
Yaroslav Zhuravlev <yar@nginx.com>
parents:
diff
changeset
|
166 function done(res) { |
87a0e2c73a25
Refactored njs documentation.
Yaroslav Zhuravlev <yar@nginx.com>
parents:
diff
changeset
|
167 if (n++ == 0) { |
87a0e2c73a25
Refactored njs documentation.
Yaroslav Zhuravlev <yar@nginx.com>
parents:
diff
changeset
|
168 r.return(res.status, res.responseBody); |
87a0e2c73a25
Refactored njs documentation.
Yaroslav Zhuravlev <yar@nginx.com>
parents:
diff
changeset
|
169 } |
87a0e2c73a25
Refactored njs documentation.
Yaroslav Zhuravlev <yar@nginx.com>
parents:
diff
changeset
|
170 } |
87a0e2c73a25
Refactored njs documentation.
Yaroslav Zhuravlev <yar@nginx.com>
parents:
diff
changeset
|
171 |
87a0e2c73a25
Refactored njs documentation.
Yaroslav Zhuravlev <yar@nginx.com>
parents:
diff
changeset
|
172 r.subrequest('/foo', r.variables.args, done); |
87a0e2c73a25
Refactored njs documentation.
Yaroslav Zhuravlev <yar@nginx.com>
parents:
diff
changeset
|
173 r.subrequest('/bar', r.variables.args, done); |
87a0e2c73a25
Refactored njs documentation.
Yaroslav Zhuravlev <yar@nginx.com>
parents:
diff
changeset
|
174 } |
2531
9c8a89d3876f
Updated njs examples with js_import and corresponding changes.
Yaroslav Zhuravlev <yar@nginx.com>
parents:
2527
diff
changeset
|
175 |
9c8a89d3876f
Updated njs examples with js_import and corresponding changes.
Yaroslav Zhuravlev <yar@nginx.com>
parents:
2527
diff
changeset
|
176 export default {content}; |
2245
87a0e2c73a25
Refactored njs documentation.
Yaroslav Zhuravlev <yar@nginx.com>
parents:
diff
changeset
|
177 </example> |
87a0e2c73a25
Refactored njs documentation.
Yaroslav Zhuravlev <yar@nginx.com>
parents:
diff
changeset
|
178 </para> |
87a0e2c73a25
Refactored njs documentation.
Yaroslav Zhuravlev <yar@nginx.com>
parents:
diff
changeset
|
179 |
87a0e2c73a25
Refactored njs documentation.
Yaroslav Zhuravlev <yar@nginx.com>
parents:
diff
changeset
|
180 </section> |
87a0e2c73a25
Refactored njs documentation.
Yaroslav Zhuravlev <yar@nginx.com>
parents:
diff
changeset
|
181 |
87a0e2c73a25
Refactored njs documentation.
Yaroslav Zhuravlev <yar@nginx.com>
parents:
diff
changeset
|
182 |
87a0e2c73a25
Refactored njs documentation.
Yaroslav Zhuravlev <yar@nginx.com>
parents:
diff
changeset
|
183 <section id="jwt" name="Creating HS JWT"> |
87a0e2c73a25
Refactored njs documentation.
Yaroslav Zhuravlev <yar@nginx.com>
parents:
diff
changeset
|
184 |
87a0e2c73a25
Refactored njs documentation.
Yaroslav Zhuravlev <yar@nginx.com>
parents:
diff
changeset
|
185 <para> |
87a0e2c73a25
Refactored njs documentation.
Yaroslav Zhuravlev <yar@nginx.com>
parents:
diff
changeset
|
186 <path>nginx.conf</path>: |
87a0e2c73a25
Refactored njs documentation.
Yaroslav Zhuravlev <yar@nginx.com>
parents:
diff
changeset
|
187 <example> |
2531
9c8a89d3876f
Updated njs examples with js_import and corresponding changes.
Yaroslav Zhuravlev <yar@nginx.com>
parents:
2527
diff
changeset
|
188 js_import http.js; |
2245
87a0e2c73a25
Refactored njs documentation.
Yaroslav Zhuravlev <yar@nginx.com>
parents:
diff
changeset
|
189 |
2531
9c8a89d3876f
Updated njs examples with js_import and corresponding changes.
Yaroslav Zhuravlev <yar@nginx.com>
parents:
2527
diff
changeset
|
190 js_set $jwt http.jwt; |
2245
87a0e2c73a25
Refactored njs documentation.
Yaroslav Zhuravlev <yar@nginx.com>
parents:
diff
changeset
|
191 </example> |
87a0e2c73a25
Refactored njs documentation.
Yaroslav Zhuravlev <yar@nginx.com>
parents:
diff
changeset
|
192 </para> |
87a0e2c73a25
Refactored njs documentation.
Yaroslav Zhuravlev <yar@nginx.com>
parents:
diff
changeset
|
193 |
87a0e2c73a25
Refactored njs documentation.
Yaroslav Zhuravlev <yar@nginx.com>
parents:
diff
changeset
|
194 <para> |
2531
9c8a89d3876f
Updated njs examples with js_import and corresponding changes.
Yaroslav Zhuravlev <yar@nginx.com>
parents:
2527
diff
changeset
|
195 <path>http.js</path>: |
2245
87a0e2c73a25
Refactored njs documentation.
Yaroslav Zhuravlev <yar@nginx.com>
parents:
diff
changeset
|
196 <example> |
2519
1cd0abf8f1e5
Updated JWT example in njs.
Yaroslav Zhuravlev <yar@nginx.com>
parents:
2517
diff
changeset
|
197 function generate_hs256_jwt(claims, key, valid) { |
2527
351a669a576d
Corrected JWT example in njs.
Yaroslav Zhuravlev <yar@nginx.com>
parents:
2520
diff
changeset
|
198 var header = { typ: "JWT", alg: "HS256" }; |
351a669a576d
Corrected JWT example in njs.
Yaroslav Zhuravlev <yar@nginx.com>
parents:
2520
diff
changeset
|
199 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
|
200 |
2519
1cd0abf8f1e5
Updated JWT example in njs.
Yaroslav Zhuravlev <yar@nginx.com>
parents:
2517
diff
changeset
|
201 var s = [header, claims].map(JSON.stringify) |
2527
351a669a576d
Corrected JWT example in njs.
Yaroslav Zhuravlev <yar@nginx.com>
parents:
2520
diff
changeset
|
202 .map(v=>v.toUTF8()) |
2519
1cd0abf8f1e5
Updated JWT example in njs.
Yaroslav Zhuravlev <yar@nginx.com>
parents:
2517
diff
changeset
|
203 .map(v=>v.toString('base64url')) |
1cd0abf8f1e5
Updated JWT example in njs.
Yaroslav Zhuravlev <yar@nginx.com>
parents:
2517
diff
changeset
|
204 .join('.'); |
2245
87a0e2c73a25
Refactored njs documentation.
Yaroslav Zhuravlev <yar@nginx.com>
parents:
diff
changeset
|
205 |
87a0e2c73a25
Refactored njs documentation.
Yaroslav Zhuravlev <yar@nginx.com>
parents:
diff
changeset
|
206 var h = require('crypto').createHmac('sha256', key); |
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 return s + '.' + h.update(s).digest().toString('base64url'); |
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 |
87a0e2c73a25
Refactored njs documentation.
Yaroslav Zhuravlev <yar@nginx.com>
parents:
diff
changeset
|
211 function jwt(r) { |
87a0e2c73a25
Refactored njs documentation.
Yaroslav Zhuravlev <yar@nginx.com>
parents:
diff
changeset
|
212 var claims = { |
2519
1cd0abf8f1e5
Updated JWT example in njs.
Yaroslav Zhuravlev <yar@nginx.com>
parents:
2517
diff
changeset
|
213 iss: "nginx", |
1cd0abf8f1e5
Updated JWT example in njs.
Yaroslav Zhuravlev <yar@nginx.com>
parents:
2517
diff
changeset
|
214 sub: "alice", |
1cd0abf8f1e5
Updated JWT example in njs.
Yaroslav Zhuravlev <yar@nginx.com>
parents:
2517
diff
changeset
|
215 foo: 123, |
1cd0abf8f1e5
Updated JWT example in njs.
Yaroslav Zhuravlev <yar@nginx.com>
parents:
2517
diff
changeset
|
216 bar: "qq", |
1cd0abf8f1e5
Updated JWT example in njs.
Yaroslav Zhuravlev <yar@nginx.com>
parents:
2517
diff
changeset
|
217 zyx: false |
2245
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 |
2519
1cd0abf8f1e5
Updated JWT example in njs.
Yaroslav Zhuravlev <yar@nginx.com>
parents:
2517
diff
changeset
|
220 return generate_hs256_jwt(claims, 'foo', 600); |
2245
87a0e2c73a25
Refactored njs documentation.
Yaroslav Zhuravlev <yar@nginx.com>
parents:
diff
changeset
|
221 } |
2531
9c8a89d3876f
Updated njs examples with js_import and corresponding changes.
Yaroslav Zhuravlev <yar@nginx.com>
parents:
2527
diff
changeset
|
222 |
9c8a89d3876f
Updated njs examples with js_import and corresponding changes.
Yaroslav Zhuravlev <yar@nginx.com>
parents:
2527
diff
changeset
|
223 export default {jwt}; |
2245
87a0e2c73a25
Refactored njs documentation.
Yaroslav Zhuravlev <yar@nginx.com>
parents:
diff
changeset
|
224 </example> |
87a0e2c73a25
Refactored njs documentation.
Yaroslav Zhuravlev <yar@nginx.com>
parents:
diff
changeset
|
225 </para> |
87a0e2c73a25
Refactored njs documentation.
Yaroslav Zhuravlev <yar@nginx.com>
parents:
diff
changeset
|
226 |
87a0e2c73a25
Refactored njs documentation.
Yaroslav Zhuravlev <yar@nginx.com>
parents:
diff
changeset
|
227 </section> |
87a0e2c73a25
Refactored njs documentation.
Yaroslav Zhuravlev <yar@nginx.com>
parents:
diff
changeset
|
228 |
87a0e2c73a25
Refactored njs documentation.
Yaroslav Zhuravlev <yar@nginx.com>
parents:
diff
changeset
|
229 |
87a0e2c73a25
Refactored njs documentation.
Yaroslav Zhuravlev <yar@nginx.com>
parents:
diff
changeset
|
230 <section id="subrequest" name="Accessing API from a Subrequest"> |
87a0e2c73a25
Refactored njs documentation.
Yaroslav Zhuravlev <yar@nginx.com>
parents:
diff
changeset
|
231 |
87a0e2c73a25
Refactored njs documentation.
Yaroslav Zhuravlev <yar@nginx.com>
parents:
diff
changeset
|
232 <para> |
87a0e2c73a25
Refactored njs documentation.
Yaroslav Zhuravlev <yar@nginx.com>
parents:
diff
changeset
|
233 <path>nginx.conf</path>: |
87a0e2c73a25
Refactored njs documentation.
Yaroslav Zhuravlev <yar@nginx.com>
parents:
diff
changeset
|
234 <example> |
2531
9c8a89d3876f
Updated njs examples with js_import and corresponding changes.
Yaroslav Zhuravlev <yar@nginx.com>
parents:
2527
diff
changeset
|
235 js_import http.js; |
2245
87a0e2c73a25
Refactored njs documentation.
Yaroslav Zhuravlev <yar@nginx.com>
parents:
diff
changeset
|
236 |
87a0e2c73a25
Refactored njs documentation.
Yaroslav Zhuravlev <yar@nginx.com>
parents:
diff
changeset
|
237 keyval_zone zone=foo:10m; |
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 |
87a0e2c73a25
Refactored njs documentation.
Yaroslav Zhuravlev <yar@nginx.com>
parents:
diff
changeset
|
240 location /keyval { |
2531
9c8a89d3876f
Updated njs examples with js_import and corresponding changes.
Yaroslav Zhuravlev <yar@nginx.com>
parents:
2527
diff
changeset
|
241 js_content http.set_keyval; |
2245
87a0e2c73a25
Refactored njs documentation.
Yaroslav Zhuravlev <yar@nginx.com>
parents:
diff
changeset
|
242 } |
87a0e2c73a25
Refactored njs documentation.
Yaroslav Zhuravlev <yar@nginx.com>
parents:
diff
changeset
|
243 |
87a0e2c73a25
Refactored njs documentation.
Yaroslav Zhuravlev <yar@nginx.com>
parents:
diff
changeset
|
244 location /version { |
2531
9c8a89d3876f
Updated njs examples with js_import and corresponding changes.
Yaroslav Zhuravlev <yar@nginx.com>
parents:
2527
diff
changeset
|
245 js_content http.version; |
2245
87a0e2c73a25
Refactored njs documentation.
Yaroslav Zhuravlev <yar@nginx.com>
parents:
diff
changeset
|
246 } |
87a0e2c73a25
Refactored njs documentation.
Yaroslav Zhuravlev <yar@nginx.com>
parents:
diff
changeset
|
247 |
87a0e2c73a25
Refactored njs documentation.
Yaroslav Zhuravlev <yar@nginx.com>
parents:
diff
changeset
|
248 location /api { |
87a0e2c73a25
Refactored njs documentation.
Yaroslav Zhuravlev <yar@nginx.com>
parents:
diff
changeset
|
249 api write=on; |
87a0e2c73a25
Refactored njs documentation.
Yaroslav Zhuravlev <yar@nginx.com>
parents:
diff
changeset
|
250 } |
87a0e2c73a25
Refactored njs documentation.
Yaroslav Zhuravlev <yar@nginx.com>
parents:
diff
changeset
|
251 </example> |
87a0e2c73a25
Refactored njs documentation.
Yaroslav Zhuravlev <yar@nginx.com>
parents:
diff
changeset
|
252 </para> |
87a0e2c73a25
Refactored njs documentation.
Yaroslav Zhuravlev <yar@nginx.com>
parents:
diff
changeset
|
253 |
87a0e2c73a25
Refactored njs documentation.
Yaroslav Zhuravlev <yar@nginx.com>
parents:
diff
changeset
|
254 <para> |
2531
9c8a89d3876f
Updated njs examples with js_import and corresponding changes.
Yaroslav Zhuravlev <yar@nginx.com>
parents:
2527
diff
changeset
|
255 <path>http.js</path>: |
2245
87a0e2c73a25
Refactored njs documentation.
Yaroslav Zhuravlev <yar@nginx.com>
parents:
diff
changeset
|
256 <example> |
87a0e2c73a25
Refactored njs documentation.
Yaroslav Zhuravlev <yar@nginx.com>
parents:
diff
changeset
|
257 function set_keyval(r) { |
2438
86b0dd6e7208
Updated API version in njs examples.
Yaroslav Zhuravlev <yar@nginx.com>
parents:
2433
diff
changeset
|
258 r.subrequest('/api/5/http/keyvals/foo', |
2245
87a0e2c73a25
Refactored njs documentation.
Yaroslav Zhuravlev <yar@nginx.com>
parents:
diff
changeset
|
259 { method: 'POST', |
87a0e2c73a25
Refactored njs documentation.
Yaroslav Zhuravlev <yar@nginx.com>
parents:
diff
changeset
|
260 body: JSON.stringify({ foo: 789, bar: "ss dd 00" })}, |
87a0e2c73a25
Refactored njs documentation.
Yaroslav Zhuravlev <yar@nginx.com>
parents:
diff
changeset
|
261 |
87a0e2c73a25
Refactored njs documentation.
Yaroslav Zhuravlev <yar@nginx.com>
parents:
diff
changeset
|
262 function(res) { |
87a0e2c73a25
Refactored njs documentation.
Yaroslav Zhuravlev <yar@nginx.com>
parents:
diff
changeset
|
263 if (res.status >= 300) { |
87a0e2c73a25
Refactored njs documentation.
Yaroslav Zhuravlev <yar@nginx.com>
parents:
diff
changeset
|
264 r.return(res.status, res.responseBody); |
87a0e2c73a25
Refactored njs documentation.
Yaroslav Zhuravlev <yar@nginx.com>
parents:
diff
changeset
|
265 return; |
87a0e2c73a25
Refactored njs documentation.
Yaroslav Zhuravlev <yar@nginx.com>
parents:
diff
changeset
|
266 } |
87a0e2c73a25
Refactored njs documentation.
Yaroslav Zhuravlev <yar@nginx.com>
parents:
diff
changeset
|
267 r.return(500); |
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 } |
87a0e2c73a25
Refactored njs documentation.
Yaroslav Zhuravlev <yar@nginx.com>
parents:
diff
changeset
|
270 |
87a0e2c73a25
Refactored njs documentation.
Yaroslav Zhuravlev <yar@nginx.com>
parents:
diff
changeset
|
271 function version(r) { |
2438
86b0dd6e7208
Updated API version in njs examples.
Yaroslav Zhuravlev <yar@nginx.com>
parents:
2433
diff
changeset
|
272 r.subrequest('/api/5/nginx', { method: 'GET' }, function(res) { |
2245
87a0e2c73a25
Refactored njs documentation.
Yaroslav Zhuravlev <yar@nginx.com>
parents:
diff
changeset
|
273 if (res.status != 200) { |
87a0e2c73a25
Refactored njs documentation.
Yaroslav Zhuravlev <yar@nginx.com>
parents:
diff
changeset
|
274 r.return(res.status); |
87a0e2c73a25
Refactored njs documentation.
Yaroslav Zhuravlev <yar@nginx.com>
parents:
diff
changeset
|
275 return; |
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 |
87a0e2c73a25
Refactored njs documentation.
Yaroslav Zhuravlev <yar@nginx.com>
parents:
diff
changeset
|
278 var json = JSON.parse(res.responseBody); |
87a0e2c73a25
Refactored njs documentation.
Yaroslav Zhuravlev <yar@nginx.com>
parents:
diff
changeset
|
279 r.return(200, json.version); |
87a0e2c73a25
Refactored njs documentation.
Yaroslav Zhuravlev <yar@nginx.com>
parents:
diff
changeset
|
280 }); |
87a0e2c73a25
Refactored njs documentation.
Yaroslav Zhuravlev <yar@nginx.com>
parents:
diff
changeset
|
281 } |
2531
9c8a89d3876f
Updated njs examples with js_import and corresponding changes.
Yaroslav Zhuravlev <yar@nginx.com>
parents:
2527
diff
changeset
|
282 |
9c8a89d3876f
Updated njs examples with js_import and corresponding changes.
Yaroslav Zhuravlev <yar@nginx.com>
parents:
2527
diff
changeset
|
283 export default {set_keyval, version}; |
2245
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 </section> |
87a0e2c73a25
Refactored njs documentation.
Yaroslav Zhuravlev <yar@nginx.com>
parents:
diff
changeset
|
288 |
87a0e2c73a25
Refactored njs documentation.
Yaroslav Zhuravlev <yar@nginx.com>
parents:
diff
changeset
|
289 |
87a0e2c73a25
Refactored njs documentation.
Yaroslav Zhuravlev <yar@nginx.com>
parents:
diff
changeset
|
290 <section id="secure_link" name="Creating secure_link Hash"> |
87a0e2c73a25
Refactored njs documentation.
Yaroslav Zhuravlev <yar@nginx.com>
parents:
diff
changeset
|
291 |
87a0e2c73a25
Refactored njs documentation.
Yaroslav Zhuravlev <yar@nginx.com>
parents:
diff
changeset
|
292 <para> |
87a0e2c73a25
Refactored njs documentation.
Yaroslav Zhuravlev <yar@nginx.com>
parents:
diff
changeset
|
293 <path>nginx.conf</path>: |
87a0e2c73a25
Refactored njs documentation.
Yaroslav Zhuravlev <yar@nginx.com>
parents:
diff
changeset
|
294 <example> |
2531
9c8a89d3876f
Updated njs examples with js_import and corresponding changes.
Yaroslav Zhuravlev <yar@nginx.com>
parents:
2527
diff
changeset
|
295 js_import http.js; |
2245
87a0e2c73a25
Refactored njs documentation.
Yaroslav Zhuravlev <yar@nginx.com>
parents:
diff
changeset
|
296 |
2531
9c8a89d3876f
Updated njs examples with js_import and corresponding changes.
Yaroslav Zhuravlev <yar@nginx.com>
parents:
2527
diff
changeset
|
297 js_set $new_foo http.create_secure_link; |
2245
87a0e2c73a25
Refactored njs documentation.
Yaroslav Zhuravlev <yar@nginx.com>
parents:
diff
changeset
|
298 ... |
87a0e2c73a25
Refactored njs documentation.
Yaroslav Zhuravlev <yar@nginx.com>
parents:
diff
changeset
|
299 |
87a0e2c73a25
Refactored njs documentation.
Yaroslav Zhuravlev <yar@nginx.com>
parents:
diff
changeset
|
300 location / { |
87a0e2c73a25
Refactored njs documentation.
Yaroslav Zhuravlev <yar@nginx.com>
parents:
diff
changeset
|
301 secure_link $cookie_foo; |
87a0e2c73a25
Refactored njs documentation.
Yaroslav Zhuravlev <yar@nginx.com>
parents:
diff
changeset
|
302 secure_link_md5 "$uri mykey"; |
87a0e2c73a25
Refactored njs documentation.
Yaroslav Zhuravlev <yar@nginx.com>
parents:
diff
changeset
|
303 ... |
87a0e2c73a25
Refactored njs documentation.
Yaroslav Zhuravlev <yar@nginx.com>
parents:
diff
changeset
|
304 } |
87a0e2c73a25
Refactored njs documentation.
Yaroslav Zhuravlev <yar@nginx.com>
parents:
diff
changeset
|
305 |
87a0e2c73a25
Refactored njs documentation.
Yaroslav Zhuravlev <yar@nginx.com>
parents:
diff
changeset
|
306 location @login { |
87a0e2c73a25
Refactored njs documentation.
Yaroslav Zhuravlev <yar@nginx.com>
parents:
diff
changeset
|
307 add_header Set-Cookie "foo=$new_foo; Max-Age=60"; |
87a0e2c73a25
Refactored njs documentation.
Yaroslav Zhuravlev <yar@nginx.com>
parents:
diff
changeset
|
308 return 302 /; |
87a0e2c73a25
Refactored njs documentation.
Yaroslav Zhuravlev <yar@nginx.com>
parents:
diff
changeset
|
309 } |
87a0e2c73a25
Refactored njs documentation.
Yaroslav Zhuravlev <yar@nginx.com>
parents:
diff
changeset
|
310 </example> |
87a0e2c73a25
Refactored njs documentation.
Yaroslav Zhuravlev <yar@nginx.com>
parents:
diff
changeset
|
311 </para> |
87a0e2c73a25
Refactored njs documentation.
Yaroslav Zhuravlev <yar@nginx.com>
parents:
diff
changeset
|
312 |
87a0e2c73a25
Refactored njs documentation.
Yaroslav Zhuravlev <yar@nginx.com>
parents:
diff
changeset
|
313 <para> |
2531
9c8a89d3876f
Updated njs examples with js_import and corresponding changes.
Yaroslav Zhuravlev <yar@nginx.com>
parents:
2527
diff
changeset
|
314 <path>http.js</path>: |
2245
87a0e2c73a25
Refactored njs documentation.
Yaroslav Zhuravlev <yar@nginx.com>
parents:
diff
changeset
|
315 <example> |
87a0e2c73a25
Refactored njs documentation.
Yaroslav Zhuravlev <yar@nginx.com>
parents:
diff
changeset
|
316 function create_secure_link(r) { |
87a0e2c73a25
Refactored njs documentation.
Yaroslav Zhuravlev <yar@nginx.com>
parents:
diff
changeset
|
317 return require('crypto').createHash('md5') |
87a0e2c73a25
Refactored njs documentation.
Yaroslav Zhuravlev <yar@nginx.com>
parents:
diff
changeset
|
318 .update(r.uri).update(" mykey") |
87a0e2c73a25
Refactored njs documentation.
Yaroslav Zhuravlev <yar@nginx.com>
parents:
diff
changeset
|
319 .digest('base64url'); |
87a0e2c73a25
Refactored njs documentation.
Yaroslav Zhuravlev <yar@nginx.com>
parents:
diff
changeset
|
320 } |
2531
9c8a89d3876f
Updated njs examples with js_import and corresponding changes.
Yaroslav Zhuravlev <yar@nginx.com>
parents:
2527
diff
changeset
|
321 |
9c8a89d3876f
Updated njs examples with js_import and corresponding changes.
Yaroslav Zhuravlev <yar@nginx.com>
parents:
2527
diff
changeset
|
322 export default {create_secure_link}; |
2245
87a0e2c73a25
Refactored njs documentation.
Yaroslav Zhuravlev <yar@nginx.com>
parents:
diff
changeset
|
323 </example> |
87a0e2c73a25
Refactored njs documentation.
Yaroslav Zhuravlev <yar@nginx.com>
parents:
diff
changeset
|
324 </para> |
87a0e2c73a25
Refactored njs documentation.
Yaroslav Zhuravlev <yar@nginx.com>
parents:
diff
changeset
|
325 |
87a0e2c73a25
Refactored njs documentation.
Yaroslav Zhuravlev <yar@nginx.com>
parents:
diff
changeset
|
326 </section> |
87a0e2c73a25
Refactored njs documentation.
Yaroslav Zhuravlev <yar@nginx.com>
parents:
diff
changeset
|
327 |
2454
253641e268a8
Added js_requests.js example in njs.
Yaroslav Zhuravlev <yar@nginx.com>
parents:
2438
diff
changeset
|
328 |
253641e268a8
Added js_requests.js example in njs.
Yaroslav Zhuravlev <yar@nginx.com>
parents:
2438
diff
changeset
|
329 <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
|
330 |
253641e268a8
Added js_requests.js example in njs.
Yaroslav Zhuravlev <yar@nginx.com>
parents:
2438
diff
changeset
|
331 <para> |
253641e268a8
Added js_requests.js example in njs.
Yaroslav Zhuravlev <yar@nginx.com>
parents:
2438
diff
changeset
|
332 <path>nginx.conf</path>: |
253641e268a8
Added js_requests.js example in njs.
Yaroslav Zhuravlev <yar@nginx.com>
parents:
2438
diff
changeset
|
333 <example> |
2531
9c8a89d3876f
Updated njs examples with js_import and corresponding changes.
Yaroslav Zhuravlev <yar@nginx.com>
parents:
2527
diff
changeset
|
334 js_import http.js; |
2454
253641e268a8
Added js_requests.js example in njs.
Yaroslav Zhuravlev <yar@nginx.com>
parents:
2438
diff
changeset
|
335 |
2531
9c8a89d3876f
Updated njs examples with js_import and corresponding changes.
Yaroslav Zhuravlev <yar@nginx.com>
parents:
2527
diff
changeset
|
336 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
|
337 |
253641e268a8
Added js_requests.js example in njs.
Yaroslav Zhuravlev <yar@nginx.com>
parents:
2438
diff
changeset
|
338 keyval_zone zone=foo:10m; |
253641e268a8
Added js_requests.js example in njs.
Yaroslav Zhuravlev <yar@nginx.com>
parents:
2438
diff
changeset
|
339 |
253641e268a8
Added js_requests.js example in njs.
Yaroslav Zhuravlev <yar@nginx.com>
parents:
2438
diff
changeset
|
340 keyval $remote_addr $foo zone=foo; |
253641e268a8
Added js_requests.js example in njs.
Yaroslav Zhuravlev <yar@nginx.com>
parents:
2438
diff
changeset
|
341 |
253641e268a8
Added js_requests.js example in njs.
Yaroslav Zhuravlev <yar@nginx.com>
parents:
2438
diff
changeset
|
342 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
|
343 access_log logs/access.log bar; |
253641e268a8
Added js_requests.js example in njs.
Yaroslav Zhuravlev <yar@nginx.com>
parents:
2438
diff
changeset
|
344 |
253641e268a8
Added js_requests.js example in njs.
Yaroslav Zhuravlev <yar@nginx.com>
parents:
2438
diff
changeset
|
345 server { |
253641e268a8
Added js_requests.js example in njs.
Yaroslav Zhuravlev <yar@nginx.com>
parents:
2438
diff
changeset
|
346 listen 8000; |
253641e268a8
Added js_requests.js example in njs.
Yaroslav Zhuravlev <yar@nginx.com>
parents:
2438
diff
changeset
|
347 |
253641e268a8
Added js_requests.js example in njs.
Yaroslav Zhuravlev <yar@nginx.com>
parents:
2438
diff
changeset
|
348 location / { |
253641e268a8
Added js_requests.js example in njs.
Yaroslav Zhuravlev <yar@nginx.com>
parents:
2438
diff
changeset
|
349 root html; |
253641e268a8
Added js_requests.js example in njs.
Yaroslav Zhuravlev <yar@nginx.com>
parents:
2438
diff
changeset
|
350 } |
253641e268a8
Added js_requests.js example in njs.
Yaroslav Zhuravlev <yar@nginx.com>
parents:
2438
diff
changeset
|
351 } |
253641e268a8
Added js_requests.js example in njs.
Yaroslav Zhuravlev <yar@nginx.com>
parents:
2438
diff
changeset
|
352 </example> |
253641e268a8
Added js_requests.js example in njs.
Yaroslav Zhuravlev <yar@nginx.com>
parents:
2438
diff
changeset
|
353 </para> |
253641e268a8
Added js_requests.js example in njs.
Yaroslav Zhuravlev <yar@nginx.com>
parents:
2438
diff
changeset
|
354 |
253641e268a8
Added js_requests.js example in njs.
Yaroslav Zhuravlev <yar@nginx.com>
parents:
2438
diff
changeset
|
355 <para> |
2531
9c8a89d3876f
Updated njs examples with js_import and corresponding changes.
Yaroslav Zhuravlev <yar@nginx.com>
parents:
2527
diff
changeset
|
356 <path>http.js</path>: |
2454
253641e268a8
Added js_requests.js example in njs.
Yaroslav Zhuravlev <yar@nginx.com>
parents:
2438
diff
changeset
|
357 <example> |
253641e268a8
Added js_requests.js example in njs.
Yaroslav Zhuravlev <yar@nginx.com>
parents:
2438
diff
changeset
|
358 function num_requests(r) |
253641e268a8
Added js_requests.js example in njs.
Yaroslav Zhuravlev <yar@nginx.com>
parents:
2438
diff
changeset
|
359 { |
253641e268a8
Added js_requests.js example in njs.
Yaroslav Zhuravlev <yar@nginx.com>
parents:
2438
diff
changeset
|
360 var n = r.variables.foo; |
253641e268a8
Added js_requests.js example in njs.
Yaroslav Zhuravlev <yar@nginx.com>
parents:
2438
diff
changeset
|
361 n = n ? Number(n) + 1 : 1; |
253641e268a8
Added js_requests.js example in njs.
Yaroslav Zhuravlev <yar@nginx.com>
parents:
2438
diff
changeset
|
362 r.variables.foo = n; |
253641e268a8
Added js_requests.js example in njs.
Yaroslav Zhuravlev <yar@nginx.com>
parents:
2438
diff
changeset
|
363 return n; |
253641e268a8
Added js_requests.js example in njs.
Yaroslav Zhuravlev <yar@nginx.com>
parents:
2438
diff
changeset
|
364 } |
2531
9c8a89d3876f
Updated njs examples with js_import and corresponding changes.
Yaroslav Zhuravlev <yar@nginx.com>
parents:
2527
diff
changeset
|
365 |
9c8a89d3876f
Updated njs examples with js_import and corresponding changes.
Yaroslav Zhuravlev <yar@nginx.com>
parents:
2527
diff
changeset
|
366 export default {num_requests}; |
2454
253641e268a8
Added js_requests.js example in njs.
Yaroslav Zhuravlev <yar@nginx.com>
parents:
2438
diff
changeset
|
367 </example> |
253641e268a8
Added js_requests.js example in njs.
Yaroslav Zhuravlev <yar@nginx.com>
parents:
2438
diff
changeset
|
368 <note> |
253641e268a8
Added js_requests.js example in njs.
Yaroslav Zhuravlev <yar@nginx.com>
parents:
2438
diff
changeset
|
369 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
|
370 <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
|
371 are available as part of our |
253641e268a8
Added js_requests.js example in njs.
Yaroslav Zhuravlev <yar@nginx.com>
parents:
2438
diff
changeset
|
372 <commercial_version>commercial subscription</commercial_version>. |
253641e268a8
Added js_requests.js example in njs.
Yaroslav Zhuravlev <yar@nginx.com>
parents:
2438
diff
changeset
|
373 </note> |
253641e268a8
Added js_requests.js example in njs.
Yaroslav Zhuravlev <yar@nginx.com>
parents:
2438
diff
changeset
|
374 </para> |
253641e268a8
Added js_requests.js example in njs.
Yaroslav Zhuravlev <yar@nginx.com>
parents:
2438
diff
changeset
|
375 |
253641e268a8
Added js_requests.js example in njs.
Yaroslav Zhuravlev <yar@nginx.com>
parents:
2438
diff
changeset
|
376 </section> |
253641e268a8
Added js_requests.js example in njs.
Yaroslav Zhuravlev <yar@nginx.com>
parents:
2438
diff
changeset
|
377 |
2483
b60e5be733cd
Added Example of promisified requests to njs.
Yaroslav Zhuravlev <yar@nginx.com>
parents:
2454
diff
changeset
|
378 |
2517
ba9bfd064a61
Renamed and corrected example in njs.
Yaroslav Zhuravlev <yar@nginx.com>
parents:
2483
diff
changeset
|
379 <section id="subrequests_chaining" name="Subrequests Chaining"> |
2483
b60e5be733cd
Added Example of promisified requests to njs.
Yaroslav Zhuravlev <yar@nginx.com>
parents:
2454
diff
changeset
|
380 |
b60e5be733cd
Added Example of promisified requests to njs.
Yaroslav Zhuravlev <yar@nginx.com>
parents:
2454
diff
changeset
|
381 <para> |
b60e5be733cd
Added Example of promisified requests to njs.
Yaroslav Zhuravlev <yar@nginx.com>
parents:
2454
diff
changeset
|
382 <path>nginx.conf</path>: |
b60e5be733cd
Added Example of promisified requests to njs.
Yaroslav Zhuravlev <yar@nginx.com>
parents:
2454
diff
changeset
|
383 <example> |
2531
9c8a89d3876f
Updated njs examples with js_import and corresponding changes.
Yaroslav Zhuravlev <yar@nginx.com>
parents:
2527
diff
changeset
|
384 js_import http.js; |
2483
b60e5be733cd
Added Example of promisified requests to njs.
Yaroslav Zhuravlev <yar@nginx.com>
parents:
2454
diff
changeset
|
385 |
b60e5be733cd
Added Example of promisified requests to njs.
Yaroslav Zhuravlev <yar@nginx.com>
parents:
2454
diff
changeset
|
386 location /start { |
2531
9c8a89d3876f
Updated njs examples with js_import and corresponding changes.
Yaroslav Zhuravlev <yar@nginx.com>
parents:
2527
diff
changeset
|
387 js_content http.content; |
2483
b60e5be733cd
Added Example of promisified requests to njs.
Yaroslav Zhuravlev <yar@nginx.com>
parents:
2454
diff
changeset
|
388 } |
b60e5be733cd
Added Example of promisified requests to njs.
Yaroslav Zhuravlev <yar@nginx.com>
parents:
2454
diff
changeset
|
389 |
b60e5be733cd
Added Example of promisified requests to njs.
Yaroslav Zhuravlev <yar@nginx.com>
parents:
2454
diff
changeset
|
390 location /auth { |
b60e5be733cd
Added Example of promisified requests to njs.
Yaroslav Zhuravlev <yar@nginx.com>
parents:
2454
diff
changeset
|
391 proxy_pass http://auth_backend; |
b60e5be733cd
Added Example of promisified requests to njs.
Yaroslav Zhuravlev <yar@nginx.com>
parents:
2454
diff
changeset
|
392 } |
b60e5be733cd
Added Example of promisified requests to njs.
Yaroslav Zhuravlev <yar@nginx.com>
parents:
2454
diff
changeset
|
393 |
b60e5be733cd
Added Example of promisified requests to njs.
Yaroslav Zhuravlev <yar@nginx.com>
parents:
2454
diff
changeset
|
394 location /backend { |
b60e5be733cd
Added Example of promisified requests to njs.
Yaroslav Zhuravlev <yar@nginx.com>
parents:
2454
diff
changeset
|
395 proxy_pass http://backend; |
b60e5be733cd
Added Example of promisified requests to njs.
Yaroslav Zhuravlev <yar@nginx.com>
parents:
2454
diff
changeset
|
396 } |
b60e5be733cd
Added Example of promisified requests to njs.
Yaroslav Zhuravlev <yar@nginx.com>
parents:
2454
diff
changeset
|
397 </example> |
b60e5be733cd
Added Example of promisified requests to njs.
Yaroslav Zhuravlev <yar@nginx.com>
parents:
2454
diff
changeset
|
398 </para> |
b60e5be733cd
Added Example of promisified requests to njs.
Yaroslav Zhuravlev <yar@nginx.com>
parents:
2454
diff
changeset
|
399 |
b60e5be733cd
Added Example of promisified requests to njs.
Yaroslav Zhuravlev <yar@nginx.com>
parents:
2454
diff
changeset
|
400 <para> |
2531
9c8a89d3876f
Updated njs examples with js_import and corresponding changes.
Yaroslav Zhuravlev <yar@nginx.com>
parents:
2527
diff
changeset
|
401 <path>http.js</path>: |
2483
b60e5be733cd
Added Example of promisified requests to njs.
Yaroslav Zhuravlev <yar@nginx.com>
parents:
2454
diff
changeset
|
402 <example> |
b60e5be733cd
Added Example of promisified requests to njs.
Yaroslav Zhuravlev <yar@nginx.com>
parents:
2454
diff
changeset
|
403 function content(r) { |
2517
ba9bfd064a61
Renamed and corrected example in njs.
Yaroslav Zhuravlev <yar@nginx.com>
parents:
2483
diff
changeset
|
404 r.subrequest('/auth') |
2483
b60e5be733cd
Added Example of promisified requests to njs.
Yaroslav Zhuravlev <yar@nginx.com>
parents:
2454
diff
changeset
|
405 .then(reply => JSON.parse(reply.responseBody)) |
b60e5be733cd
Added Example of promisified requests to njs.
Yaroslav Zhuravlev <yar@nginx.com>
parents:
2454
diff
changeset
|
406 .then(response => { |
b60e5be733cd
Added Example of promisified requests to njs.
Yaroslav Zhuravlev <yar@nginx.com>
parents:
2454
diff
changeset
|
407 if (!response['token']) { |
b60e5be733cd
Added Example of promisified requests to njs.
Yaroslav Zhuravlev <yar@nginx.com>
parents:
2454
diff
changeset
|
408 throw new Error("token is not available"); |
b60e5be733cd
Added Example of promisified requests to njs.
Yaroslav Zhuravlev <yar@nginx.com>
parents:
2454
diff
changeset
|
409 } |
2517
ba9bfd064a61
Renamed and corrected example in njs.
Yaroslav Zhuravlev <yar@nginx.com>
parents:
2483
diff
changeset
|
410 return reply['token']; |
2483
b60e5be733cd
Added Example of promisified requests to njs.
Yaroslav Zhuravlev <yar@nginx.com>
parents:
2454
diff
changeset
|
411 }) |
b60e5be733cd
Added Example of promisified requests to njs.
Yaroslav Zhuravlev <yar@nginx.com>
parents:
2454
diff
changeset
|
412 .then(token => { |
b60e5be733cd
Added Example of promisified requests to njs.
Yaroslav Zhuravlev <yar@nginx.com>
parents:
2454
diff
changeset
|
413 r.subrequest('/backend', `token=${token}`) |
b60e5be733cd
Added Example of promisified requests to njs.
Yaroslav Zhuravlev <yar@nginx.com>
parents:
2454
diff
changeset
|
414 .then(reply => r.return(reply.status, reply.responseBody)); |
b60e5be733cd
Added Example of promisified requests to njs.
Yaroslav Zhuravlev <yar@nginx.com>
parents:
2454
diff
changeset
|
415 }) |
b60e5be733cd
Added Example of promisified requests to njs.
Yaroslav Zhuravlev <yar@nginx.com>
parents:
2454
diff
changeset
|
416 .catch(_ => r.return(500)); |
b60e5be733cd
Added Example of promisified requests to njs.
Yaroslav Zhuravlev <yar@nginx.com>
parents:
2454
diff
changeset
|
417 } |
2531
9c8a89d3876f
Updated njs examples with js_import and corresponding changes.
Yaroslav Zhuravlev <yar@nginx.com>
parents:
2527
diff
changeset
|
418 |
9c8a89d3876f
Updated njs examples with js_import and corresponding changes.
Yaroslav Zhuravlev <yar@nginx.com>
parents:
2527
diff
changeset
|
419 export default {content}; |
2483
b60e5be733cd
Added Example of promisified requests to njs.
Yaroslav Zhuravlev <yar@nginx.com>
parents:
2454
diff
changeset
|
420 </example> |
b60e5be733cd
Added Example of promisified requests to njs.
Yaroslav Zhuravlev <yar@nginx.com>
parents:
2454
diff
changeset
|
421 </para> |
b60e5be733cd
Added Example of promisified requests to njs.
Yaroslav Zhuravlev <yar@nginx.com>
parents:
2454
diff
changeset
|
422 |
b60e5be733cd
Added Example of promisified requests to njs.
Yaroslav Zhuravlev <yar@nginx.com>
parents:
2454
diff
changeset
|
423 </section> |
b60e5be733cd
Added Example of promisified requests to njs.
Yaroslav Zhuravlev <yar@nginx.com>
parents:
2454
diff
changeset
|
424 |
2245
87a0e2c73a25
Refactored njs documentation.
Yaroslav Zhuravlev <yar@nginx.com>
parents:
diff
changeset
|
425 </article> |