forked from openresty/echo-nginx-module
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathREADME
1618 lines (1166 loc) · 47.9 KB
/
README
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
Name
ngx_echo - Brings "echo", "sleep", "time", "exec" and more shell-style
goodies to Nginx config file.
*This module is not distributed with the Nginx source.* See the
installation instructions.
Status
This module is production ready.
Version
This document describes ngx_echo v0.43
(<https://github.com/agentzh/echo-nginx-module/tags>) released on 10
March 2013.
Synopsis
location /hello {
echo "hello, world!";
}
location /hello {
echo -n "hello, "
echo "world!";
}
location /timed_hello {
echo_reset_timer;
echo hello world;
echo "'hello world' takes about $echo_timer_elapsed sec.";
echo hiya igor;
echo "'hiya igor' takes about $echo_timer_elapsed sec.";
}
location /echo_with_sleep {
echo hello;
echo_flush; # ensure the client can see previous output immediately
echo_sleep 2.5; # in sec
echo world;
}
# in the following example, accessing /echo yields
# hello
# world
# blah
# hiya
# igor
location /echo {
echo_before_body hello;
echo_before_body world;
proxy_pass $scheme://127.0.0.1:$server_port$request_uri/more;
echo_after_body hiya;
echo_after_body igor;
}
location /echo/more {
echo blah;
}
# the output of /main might be
# hello
# world
# took 0.000 sec for total.
# and the whole request would take about 2 sec to complete.
location /main {
echo_reset_timer;
# subrequests in parallel
echo_location_async /sub1;
echo_location_async /sub2;
echo "took $echo_timer_elapsed sec for total.";
}
location /sub1 {
echo_sleep 2;
echo hello;
}
location /sub2 {
echo_sleep 1;
echo world;
}
# the output of /main might be
# hello
# world
# took 3.003 sec for total.
# and the whole request would take about 3 sec to complete.
location /main {
echo_reset_timer;
# subrequests in series (chained by CPS)
echo_location /sub1;
echo_location /sub2;
echo "took $echo_timer_elapsed sec for total.";
}
location /sub1 {
echo_sleep 2;
echo hello;
}
location /sub2 {
echo_sleep 1;
echo world;
}
# Accessing /dup gives
# ------ END ------
location /dup {
echo_duplicate 3 "--";
echo_duplicate 1 " END ";
echo_duplicate 3 "--";
echo;
}
# /bighello will generate 1000,000,000 hello's.
location /bighello {
echo_duplicate 1000_000_000 'hello';
}
# echo back the client request
location /echoback {
echo_duplicate 1 $echo_client_request_headers;
echo "\r";
echo_read_request_body;
echo_request_body;
}
# GET /multi will yields
# querystring: foo=Foo
# method: POST
# body: hi
# content length: 2
# ///
# querystring: bar=Bar
# method: PUT
# body: hello
# content length: 5
# ///
location /multi {
echo_subrequest_async POST '/sub' -q 'foo=Foo' -b 'hi';
echo_subrequest_async PUT '/sub' -q 'bar=Bar' -b 'hello';
}
location /sub {
echo "querystring: $query_string";
echo "method: $echo_request_method";
echo "body: $echo_request_body";
echo "content length: $http_content_length";
echo '///';
}
# GET /merge?/foo.js&/bar/blah.js&/yui/baz.js will merge the .js resources together
location /merge {
default_type 'text/javascript';
echo_foreach_split '&' $query_string;
echo "/* JS File $echo_it */";
echo_location_async $echo_it;
echo;
echo_end;
}
# accessing /if?val=abc yields the "hit" output
# while /if?val=bcd yields "miss":
location ^~ /if {
set $res miss;
if ($arg_val ~* '^a') {
set $res hit;
echo $res;
}
echo $res;
}
Description
This module wraps lots of Nginx internal APIs for streaming input and
output, parallel/sequential subrequests, timers and sleeping, as well as
various meta data accessing.
Basically it provides various utilities that help testing and debugging
of other modules by trivially emulating different kinds of faked
subrequest locations.
People will also find it useful in real-world applications that need to
1. serve static contents directly from memory (loading from the Nginx
config file).
2. wrap the upstream response with custom header and footer (kinda like
the [module (HttpAdditionModule)] but with contents read directly
from the config file and Nginx variables).
3. merge contents of various "Nginx locations" (i.e., subrequests)
together in a single main request (using echo_location and its
friends).
This is a special dual-role module that can *lazily* serve as a content
handler or register itself as an output filter only upon demand. By
default, this module does not do anything at all.
Technially, this module has also demonstrated the following techniques
that might be helpful for module writers:
1. Issue parallel subreqeusts directly from content handler.
2. Issue chained subrequests directly from content handler, by passing
continuation along the subrequest chain.
3. Issue subrequests with all HTTP 1.1 methods and even an optional
faked HTTP request body.
4. Interact with the Nginx event model directly from content handler
using custom events and timers, and resume the content handler back
if necessary.
5. Dual-role module that can (lazily) serve as a content handler or an
output filter or both.
6. Nginx config file variable creation and interpolation.
7. Streaming output control using output_chain, flush and its friends.
8. Read client request body from the content handler, and returns back
(asynchronously) to the content handler after completion.
9. Use Perl-based declarative test suite to drive the development of
Nginx C modules.
Content Handler Directives
Use of the following directives register this module to the current
Nginx location as a content handler. If you want to use another module,
like the [proxy module (HttpProxyModule)], as the content handler, use
the filter directives provided by this module.
All the content handler directives can be mixed together in a single
Nginx location and they're supposed to run sequentially just as in the
Bash scripting language.
Every content handler directive supports variable interpolation in its
arguments (if any).
The MIME type set by the [default_type directive
(HttpCoreModule#default_type)] is respected by this module, as in:
location /hello {
default_type text/plain;
echo hello;
}
Then on the client side:
$ curl -I 'http://localhost/echo'
HTTP/1.1 200 OK
Server: nginx/0.8.20
Date: Sat, 17 Oct 2009 03:40:19 GMT
Content-Type: text/plain
Connection: keep-alive
Since the v0.22 release, all of the directives are allowed in the
[module (HttpRewriteModule)]'s if directive block, for instance:
location ^~ /if {
set $res miss;
if ($arg_val ~* '^a') {
set $res hit;
echo $res;
}
echo $res;
}
echo
syntax: *echo [options] <string>...*
default: *no*
context: *location, location if*
phase: *content*
Sends arguments joined by spaces, along with a trailing newline, out to
the client.
Note that the data might be buffered by Nginx's underlying buffer. To
force the output data flushed immediately, use the echo_flush command
just after "echo", as in
echo hello world;
echo_flush;
When no argument is specified, *echo* emits the trailing newline alone,
just like the *echo* command in shell.
Variables may appear in the arguments. An example is
echo The current request uri is $request_uri;
where $request_uri is a variable exposed by the [[HttpCoreModule]].
This command can be used multiple times in a single location
configuration, as in
location /echo {
echo hello;
echo world;
}
The output on the client side looks like this
$ curl 'http://localhost/echo'
hello
world
Special characters like newlines ("\n") and tabs ("\t") can be escaped
using C-style escaping sequences. But a notable exception is the dollar
sign ("$"). As of Nginx 0.8.20, there's still no clean way to esacpe
this characters. (A work-around might be to use a $echo_dollor variable
that is always evaluated to the constant "$" character. This feature
will possibly be introduced in a future version of this module.)
As of the echo v0.28 release, one can suppress the trailing newline
character in the output by using the "-n" option, as in
location /echo {
echo -n "hello, ";
echo "world";
}
Accessing "/echo" gives
$ curl 'http://localhost/echo'
hello, world
Leading "-n" in variable values won't take effect and will be emitted
literally, as in
location /echo {
set $opt -n;
echo $opt "hello,";
echo "world";
}
This gives the following output
$ curl 'http://localhost/echo'
-n hello,
world
One can output leading "-n" literals and other options using the special
"--" option like this
location /echo {
echo -- -n is an option;
}
which yields
$ curl 'http://localhost/echo'
-n is an option
echo_duplicate
syntax: *echo_duplicate <count> <string>*
default: *no*
context: *location, location if*
phase: *content*
Outputs duplication of a string indicated by the second argument, using
the times specified in the first argument.
For instance,
location /dup {
echo_duplicate 3 "abc";
}
will lead to an output of "abcabcabc".
Underscores are allowed in the count number, just like in Perl. For
example, to emit 1000,000,000 instances of "hello, world":
location /many_hellos {
echo_duplicate 1000_000_000 "hello, world";
}
The "count" argument could be zero, but not negative. The second
"string" argument could be an empty string ("") likewise.
Unlike the echo directive, no trailing newline is appended to the
result. So it's possible to "abuse" this directive as a
no-trailing-newline version of echo by using "count" 1, as in
location /echo_art {
echo_duplicate 2 '---';
echo_duplicate 1 ' END '; # we don't want a trailing newline here
echo_duplicate 2 '---';
echo; # we want a trailing newline here...
}
You get
------ END ------
This directive was first introduced in version 0.11.
echo_flush
syntax: *echo_flush*
default: *no*
context: *location, location if*
phase: *content*
Forces the data potentially buffered by underlying Nginx output filters
to send immediately to the client side via socket.
Note that techically the command just emits a ngx_buf_t object with
"flush" slot set to 1, so certain weird third-party output filter module
could still block it before it reaches Nginx's (last) write filter.
This directive does not take any argument.
Consider the following example:
location /flush {
echo hello;
echo_flush;
echo_sleep 1;
echo world;
}
Then on the client side, using curl to access "/flush", you'll see the
"hello" line immediately, but only after 1 second, the last "world"
line. Without calling "echo_flush" in the example above, you'll most
likely see no output until 1 second is elapsed due to the internal
buffering of Nginx.
This directive will fail to flush the output buffer in case of
subrequests get involved. Consider the following example:
location /main {
echo_location_async /sub;
echo hello;
echo_flush;
}
location /sub {
echo_sleep 1;
}
Then the client won't see "hello" appear even if "echo_flush" has been
executed before the subrequest to "/sub" has actually started executing.
The outputs of "/main" that are sent *after* echo_location_async will be
postponed and buffered firmly.
This does *not* apply to outputs sent before the subrequest initiated.
For a modified version of the example given above:
location /main {
echo hello;
echo_flush;
echo_location_async /sub;
}
location /sub {
echo_sleep 1;
}
The client will immediately see "hello" before "/sub" enters sleeping.
See also echo, echo_sleep, and echo_location_async.
echo_sleep
syntax: *echo_sleep <seconds>*
default: *no*
context: *location, location if*
phase: *content*
Sleeps for the time period specified by the argument, which is in
seconds.
This operation is non-blocking on server side, so unlike the
echo_blocking_sleep directive, it won't block the whole Nginx worker
process.
The period might takes three digits after the decimal point and must be
greater than 0.001.
An example is
location /echo_after_sleep {
echo_sleep 1.234;
echo resumed!;
}
Behind the scene, it sets up a per-request "sleep" ngx_event_t object,
and adds a timer using that custom event to the Nginx event model and
just waits for a timeout on that event. Because the "sleep" event is
per-request, this directive can work in parallel subrequests.
echo_blocking_sleep
syntax: *echo_blocking_sleep <seconds>*
default: *no*
context: *location, location if*
phase: *content*
This is a blocking version of the echo_sleep directive.
See the documentation of echo_sleep for more detail.
Behind the curtain, it calls the ngx_msleep macro provided by the Nginx
core which maps to usleep on POSIX-compliant systems.
Note that this directive will block the current Nginx worker process
completely while being executed, so never use it in production
environment.
echo_reset_timer
syntax: *echo_reset_timer*
default: *no*
context: *location, location if*
phase: *content*
Reset the timer begin time to *now*, i.e., the time when this command is
executed during request.
The timer begin time is default to the starting time of the current
request and can be overridden by this directive, potentially multiple
times in a single location. For example:
location /timed_sleep {
echo_sleep 0.03;
echo "$echo_timer_elapsed sec elapsed.";
echo_reset_timer;
echo_sleep 0.02;
echo "$echo_timer_elapsed sec elapsed.";
}
The output on the client side might be
$ curl 'http://localhost/timed_sleep'
0.032 sec elapsed.
0.020 sec elapsed.
The actual figures you get on your side may vary a bit due to your
system's current activities.
Invocation of this directive will force the underlying Nginx timer to
get updated to the current system time (regardless the timer resolution
specified elsewhere in the config file). Furthermore, references of the
$echo_timer_elapsed variable will also trigger timer update forcibly.
See also echo_sleep and $echo_timer_elapsed.
echo_read_request_body
syntax: *echo_read_request_body*
default: *no*
context: *location, location if*
phase: *content*
Explicitly reads request body so that the $request_body variable will
always have non-empty values (unless the body is so big that it has been
saved by Nginx to a local temporary file).
Note that this might not be the original client request body because the
current request might be a subrequest with a "artificial" body specified
by its parent.
This directive does not generate any output itself, just like
echo_sleep.
Here's an example for echo'ing back the original HTTP client request
(both headers and body are included):
location /echoback {
echo_duplicate 1 $echo_client_request_headers;
echo "\r";
echo_read_request_body;
echo $request_body;
}
The content of "/echoback" looks like this on my side (I was using
Perl's LWP utility to access this location on the server):
$ (echo hello; echo world) | lwp-request -m POST 'http://localhost/echoback'
POST /echoback HTTP/1.1
TE: deflate,gzip;q=0.3
Connection: TE, close
Host: localhost
User-Agent: lwp-request/5.818 libwww-perl/5.820
Content-Length: 12
Content-Type: application/x-www-form-urlencoded
hello
world
Because "/echoback" is the main request, $request_body holds the
original client request body.
Before Nginx 0.7.56, it makes no sense to use this directive because
$request_body was first introduced in Nginx 0.7.58.
This directive itself was first introduced in the echo module's v0.14
release.
echo_location_async
syntax: *echo_location_async <location> [<url_args>]*
default: *no*
context: *location, location if*
phase: *content*
Issue GET subrequest to the location specified (first argument) with
optional url arguments specified in the second argument.
As of Nginx 0.8.20, the "location" argument does *not* support named
location, due to a limitation in the "ngx_http_subrequest" function. The
same is true for its brother, the echo_location directive.
A very simple example is
location /main {
echo_location_async /sub;
echo world;
}
location /sub {
echo hello;
}
Accessing "/main" gets
hello
world
Calling multiple locations in parallel is also possible:
location /main {
echo_reset_timer;
echo_location_async /sub1;
echo_location_async /sub2;
echo "took $echo_timer_elapsed sec for total.";
}
location /sub1 {
echo_sleep 2; # sleeps 2 sec
echo hello;
}
location /sub2 {
echo_sleep 1; # sleeps 1 sec
echo world;
}
Accessing "/main" yields
$ time curl 'http://localhost/main'
hello
world
took 0.000 sec for total.
real 0m2.006s
user 0m0.000s
sys 0m0.004s
You can see that the main handler "/main" does *not* wait the
subrequests "/sub1" and "/sub2" to complete and quickly goes on, hence
the "0.000 sec" timing result. The whole request, however takes
approximately 2 sec in total to complete because "/sub1" and "/sub2" run
in parallel (or "concurrently" to be more accurate).
If you use echo_blocking_sleep in the previous example instead, then
you'll get the same output, but with 3 sec total response time, because
"blocking sleep" blocks the whole Nginx worker process.
Locations can also take an optional querystring argument, for instance
location /main {
echo_location_async /sub 'foo=Foo&bar=Bar';
}
location /sub {
echo $arg_foo $arg_bar;
}
Accessing "/main" yields
$ curl 'http://localhost/main'
Foo Bar
Querystrings is *not* allowed to be concatenated onto the "location"
argument with "?" directly, for example, "/sub?foo=Foo&bar=Bar" is an
invalid location, and shouldn't be fed as the first argument to this
directive.
Due to an unknown bug in Nginx (it still exists in Nginx 0.8.20), the
[SSI module (HttpSsiModule)] is required to ensure that the contents of
the subrequests issued by this directive are correctly merged into the
output chains of the main one. Fortunately, the SSI module is enabled by
default during Nginx's "configure" process.
If calling this directive without SSI module enabled, you'll get
truncated response without contents of any subrequests and get an alert
message in your Nginx's "error.log", like this:
[alert] 24212#0: *1 the http output chain is empty, client: 127.0.0.1, ...
Technically speaking, this directive is an example that Nginx content
handler issues one or more subrequests directly. AFAIK, the fancyindex
module (<https://connectical.com/projects/ngx-fancyindex/wiki>) also
does such kind of things ;)
Nginx named locations like @foo is *not* supported here.
This directive is logically equivalent to the GET version of
echo_subrequest_async. For example,
echo_location_async /foo 'bar=Bar';
is logically equivalent to
echo_subrequest_async GET /foo -q 'bar=Bar';
But calling this directive is slightly faster than calling
echo_subrequest_async using "GET" because we don't have to parse the
HTTP method names like "GET" and options like "-q".
This directive is first introduced in version 0.09 of this module and
requires at least Nginx 0.7.46.
echo_location
syntax: *echo_location <location> [<url_args>]*
default: *no*
context: *location, location if*
phase: *content*
Just like the echo_location_async directive, but "echo_location" issues
subrequests *in series* rather than in parallel. That is, the content
handler directives following this directive won't be executed until the
subrequest issued by this directive completes.
The final response body is almost always equivalent to the case when
echo_location_async is used instead, only if timing variables is used in
the outputs.
Consider the following example:
location /main {
echo_reset_timer;
echo_location /sub1;
echo_location /sub2;
echo "took $echo_timer_elapsed sec for total.";
}
location /sub1 {
echo_sleep 2;
echo hello;
}
location /sub2 {
echo_sleep 1;
echo world;
}
The location "/main" above will take for total 3 sec to complete
(compared to 2 sec if echo_location_async is used instead here). Here's
the result in action on my machine:
$ curl 'http://localhost/main'
hello
world
took 3.003 sec for total.
real 0m3.027s
user 0m0.020s
sys 0m0.004s
This directive is logically equivalent to the GET version of
echo_subrequest. For example,
echo_location /foo 'bar=Bar';
is logically equivalent to
echo_subrequest GET /foo -q 'bar=Bar';
But calling this directive is slightly faster than calling
echo_subrequest using "GET" because we don't have to parse the HTTP
method names like "GET" and options like "-q".
Behind the scene, it creates an "ngx_http_post_subrequest_t" object as a
*continuation* and passes it into the "ngx_http_subrequest" function
call. Nginx will later reopen this "continuation" in the subrequest's
"ngx_http_finalize_request" function call. We resumes the execution of
the parent-request's content handler and starts to run the next
directive (command) if any.
Nginx named locations like @foo is *not* supported here.
This directive was first introduced in the release v0.12.
See also echo_location_async for more details about the meaning of the
arguments.
echo_subrequest_async
syntax: *echo_subrequest_async <HTTP_method> <location> [-q <url_args>]
[-b <request_body>] [-f <request_body_path>]*
default: *no*
context: *location, location if*
phase: *content*
Initiate an asynchronous subrequest using HTTP method, an optional url
arguments (or querystring) and an optional request body which can be
defined as a string or as a path to a file which contains the body.
This directive is very much like a generalized version of the
echo_location_async directive.
Here's a small example demonstrating its usage:
location /multi {
# body defined as string
echo_subrequest_async POST '/sub' -q 'foo=Foo' -b 'hi';
# body defined as path to a file, relative to nginx prefix path if not absolute
echo_subrequest_async PUT '/sub' -q 'bar=Bar' -f '/tmp/hello.txt';
}
location /sub {
echo "querystring: $query_string";
echo "method: $echo_request_method";
echo "body: $echo_request_body";
echo "content length: $http_content_length";
echo '///';
}
Then on the client side:
$ echo -n hello > /tmp/hello.txt
$ curl 'http://localhost/multi'
querystring: foo=Foo
method: POST
body: hi
content length: 2
///
querystring: bar=Bar
method: PUT
body: hello
content length: 5
///
Here's more funny example using the standard proxy module to handle the
subrequest:
location /main {
echo_subrequest_async POST /sub -b 'hello, world';
}
location /sub {
proxy_pass $scheme://127.0.0.1:$server_port/proxied;
}
location /proxied {
echo "method: $echo_request_method.";
# we need to read body explicitly here...or $echo_request_body
# will evaluate to empty ("")
echo_read_request_body;
echo "body: $echo_request_body.";
}
Then on the client side, we can see that
$ curl 'http://localhost/main'
method: POST.
body: hello, world.
Nginx named locations like @foo is *not* supported here.
This directive was first introduced in the release v0.15.
The "-f" option to define a file path for the body was introduced in the
release v0.35.
See also the echo_subrequest and echo_location_async directives.
echo_subrequest
syntax: *echo_subrequest_async <HTTP_method> <location> [-q <url_args>]
[-b <request_body>] [-f <request_body_path>]*
default: *no*
context: *location, location if*
phase: *content*
This is the synchronous version of the echo_subrequest_async directive.
And just like echo_location, it does not block the Nginx worker process
(while echo_blocking_sleep does), rather, it uses continuation to pass
control along the subrequest chain.
See echo_subrequest_async for more details.
Nginx named locations like @foo is *not* supported here.
This directive was first introduced in the release v0.15.
echo_foreach_split
syntax: *echo_foreach_split <delimiter> <string>*
default: *no*
context: *location, location if*
phase: *content*
Split the second argument "string" using the delimiter specified in the
first argument, and then iterate through the resulting items. For
instance:
location /loop {
echo_foreach_split ',' $arg_list;
echo "item: $echo_it";
echo_end;
}
Accessing /main yields
$ curl 'http://localhost/loop?list=cat,dog,mouse'
item: cat
item: dog
item: mouse
As seen in the previous example, this directive should always be
accompanied by an echo_end directive.
Parallel "echo_foreach_split" loops are allowed, but nested ones are
currently forbidden.
The "delimiter" argument could contain *multiple* arbitrary characters,
like
echo_foreach_split '-a-' 'cat-a-dog-a-mouse';
echo $echo_it;
echo_end;
Logically speaking, this looping structure is just the "foreach" loop
combined with a "split" function call in Perl (using the previous
example):
foreach (split ',', $arg_list) {
print "item $_\n";
}
People will also find it useful in merging multiple ".js" or ".css"
resources into a whole. Here's an example:
location /merge {
default_type 'text/javascript';
echo_foreach_split '&' $query_string;
echo "/* JS File $echo_it */";
echo_location_async $echo_it;
echo;
echo_end;
}
Then accessing /merge to merge the ".js" resources specified in the
query string:
$ curl 'http://localhost/merge?/foo/bar.js&/yui/blah.js&/baz.js'
One can also use third-party Nginx cache module to cache the merged
response generated by the "/merge" location in the previous example.
This directive was first introduced in the release v0.17.
echo_end
syntax: *echo_end*
default: *no*
context: *location, location if*
phase: *content*
This directive is used to terminate the body of looping and conditional
control structures like echo_foreach_split.