summaryrefslogtreecommitdiff
path: root/pod/srcache-nginx-module-0.33/srcache-nginx-module-0.33.pod
blob: ef402fb530d194c98c82f45ea03f5ff0b79387c8 (plain)
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
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
1099
1100
1101
1102
1103
1104
1105
1106
1107
1108
1109
1110
1111
1112
1113
1114
1115
1116
1117
1118
1119
1120
1121
1122
1123
1124
1125
1126
1127
1128
1129
1130
1131
1132
1133
1134
1135
1136
1137
1138
1139
1140
1141
1142
1143
1144
1145
1146
1147
1148
1149
1150
1151
1152
1153
1154
1155
1156
1157
1158
1159
1160
1161
1162
1163
1164
1165
1166
1167
1168
1169
1170
1171
1172
1173
1174
1175
1176
1177
1178
1179
1180
1181
1182
1183
1184
1185
1186
1187
1188
1189
1190
1191
1192
1193
1194
1195
1196
1197
1198
1199
1200
1201
1202
1203
1204
1205
1206
1207
1208
1209
1210
1211
1212
1213
1214
1215
1216
1217
1218
1219
1220
1221
1222
1223
1224
1225
1226
1227
1228
1229
1230
1231
1232
1233
1234
1235
1236
1237
1238
1239
1240
1241
1242
1243
1244
1245
1246
1247
1248
1249
1250
1251
1252
1253
1254
1255
1256
1257
1258
1259
1260
1261
1262
1263
1264
1265
1266
1267
1268
1269
1270
1271
1272
1273
1274
1275
1276
1277
1278
1279
1280
1281
1282
1283
1284
1285
1286
1287
1288
1289
1290
1291
1292
1293
1294
1295
1296
1297
1298
1299
1300
1301
1302
1303
1304
1305
1306
1307
1308
1309
1310
1311
1312
1313
1314
1315
1316
1317
1318
1319
1320
1321
1322
1323
1324
1325
1326
1327
1328
1329
1330
1331
1332
1333
1334
1335
1336
1337
1338
1339
1340
1341
1342
1343
1344
1345
1346
1347
1348
1349
1350
1351
1352
1353
1354
1355
1356
1357
1358
1359
1360
1361
1362
1363
1364
1365
1366
1367
1368
1369
1370
1371
1372
1373
1374
1375
1376
1377
1378
1379
1380
1381
1382
1383
1384
1385
1386
1387
1388
1389
1390
1391
1392
1393
1394
1395
1396
1397
1398
1399
1400
1401
1402
1403
1404
1405
1406
1407
1408
1409
1410
1411
1412
1413
1414
1415
1416
1417
1418
1419
1420
1421
1422
1423
1424
1425
1426
1427
1428
1429
1430
1431
1432
1433
1434
1435
1436
1437
1438
1439
1440
1441
1442
1443
1444
1445
1446
1447
1448
1449
1450
1451
1452
1453
1454
1455
1456
1457
1458
1459
1460
1461
1462
1463
1464
1465
1466
1467
1468
1469
1470
1471
1472
1473
1474
1475
1476
1477
1478
1479
1480
1481
1482
1483
1484
1485
1486
1487
1488
1489
1490
1491
1492
1493
1494
1495
1496
1497
1498
1499
1500
1501
1502
1503
1504
1505
1506
1507
1508
1509
1510
1511
1512
1513
1514
1515
1516
1517
1518
1519
1520
1521
1522
1523
1524
1525
1526
1527
1528
1529
1530
1531
1532
=encoding utf-8


=head1 Name

B<ngx_srcache> - Transparent subrequest-based caching layout for arbitrary nginx locations

I<This module is not distributed with the Nginx source.> See L<the installation instructions>.


=head1 Status

This module is production ready.


=head1 Version

This document describes srcache-nginx-module L<v0.32|https://github.com/openresty/srcache-nginx-module/tags> released on 2 July 2020.


=head1 Synopsis


     upstream my_memcached {
         server 10.62.136.7:11211;
         keepalive 10;
     }
    
     location = /memc {
         internal;
    
         memc_connect_timeout 100ms;
         memc_send_timeout 100ms;
         memc_read_timeout 100ms;
         memc_ignore_client_abort on;
    
         set $memc_key $query_string;
         set $memc_exptime 300;
    
         memc_pass my_memcached;
     }
    
     location /foo {
         set $key $uri$args;
         srcache_fetch GET /memc $key;
         srcache_store PUT /memc $key;
         srcache_store_statuses 200 301 302 307 308;
    
         # proxy_pass/fastcgi_pass/drizzle_pass/echo/etc...
         # or even static files on the disk
     }


     location = /memc2 {
         internal;
    
         memc_connect_timeout 100ms;
         memc_send_timeout 100ms;
         memc_read_timeout 100ms;
         memc_ignore_client_abort on;
    
         set_unescape_uri $memc_key $arg_key;
         set $memc_exptime $arg_exptime;
    
         memc_pass unix:/tmp/memcached.sock;
     }
    
     location /bar {
         set_escape_uri $key $uri$args;
         srcache_fetch GET /memc2 key=$key;
         srcache_store PUT /memc2 key=$key&exptime=$srcache_expire;
    
         # proxy_pass/fastcgi_pass/drizzle_pass/echo/etc...
         # or even static files on the disk
     }


     map $request_method $skip_fetch {
         default     0;
         POST        1;
         PUT         1;
     }
    
     server {
         listen 8080;
    
         location /api/ {
             set $key "$uri?$args";
    
             srcache_fetch GET /memc $key;
             srcache_store PUT /memc $key;
    
             srcache_methods GET PUT POST;
             srcache_fetch_skip $skip_fetch;
    
             # proxy_pass/drizzle_pass/content_by_lua/echo/...
         }
     }




=head1 Description

This module provides a transparent caching layer for arbitrary nginx locations (like those use an upstream or even serve static disk files). The caching behavior is mostly compatible with L<RFC 2616|http://www.ietf.org/rfc/rfc2616.txt>.

Usually, L<memc-nginx-module|https://github.com/openresty/memc-nginx-module> is used together with this module to provide a concrete caching storage backend. But technically, any modules that provide a REST interface can be used as the fetching and storage subrequests used by this module.

For main requests, the L<srcache_fetch> directive works at the end of the access phase, so the L<standard access module|http://nginx.org/en/docs/http/ngx_http_access_module.html>'s L<allow|http://nginx.org/en/docs/http/ngx_http_access_module.html#allow> and L<deny|http://nginx.org/en/docs/http/ngx_http_access_module.html#deny> direcives run I<before> ours, which is usually the desired behavior for security reasons.

The workflow of this module looks like below:

!L<srcache flowchart|http://agentzh.org/misc/image/srcache-flowchart.png "srcache flowchart">




=head2 Subrequest caching

For I<subrequests>, we explicitly B<disallow> the use of this module because it's too difficult to get right. There used to be an implementation but it was buggy and I finally gave up fixing it and abandoned it.

However, if you're using L<lua-nginx-module|https://github.com/openresty/lua-nginx-module>, it's easy to do subrequest caching in Lua all by yourself. That is, first issue a subrequest to an L<memc-nginx-module|https://github.com/openresty/memc-nginx-module> location to do an explicit cache lookup, if cache hit, just use the cached data returned; otherwise, fall back to the true backend, and finally do a cache insertion to feed the data into the cache.

Using this module for main request caching and Lua for subrequest caching is the approach that we're taking in our business. This hybrid solution works great in production.




=head2 Distributed Memcached Caching

Here is a simple example demonstrating a distributed memcached caching mechanism built atop this module. Suppose we do have three different memcached nodes and we use simple modulo to hash our keys.


     http {
         upstream moon {
             server 10.62.136.54:11211;
             server unix:/tmp/memcached.sock backup;
         }
    
         upstream earth {
             server 10.62.136.55:11211;
         }
    
         upstream sun {
             server 10.62.136.56:11211;
         }
    
         upstream_list universe moon earth sun;
    
         server {
             memc_connect_timeout 100ms;
             memc_send_timeout 100ms;
             memc_read_timeout 100ms;
    
             location = /memc {
                 internal;
    
                 set $memc_key $query_string;
                 set_hashed_upstream $backend universe $memc_key;
                 set $memc_exptime 3600; # in seconds
                 memc_pass $backend;
             }
    
             location / {
                 set $key $uri;
                 srcache_fetch GET /memc $key;
                 srcache_store PUT /memc $key;
    
                 # proxy_pass/fastcgi_pass/content_by_lua/drizzle_pass/...
             }
         }
     }

Here's what is going on in the sample above:

=over


=item 1.

We first define three upstreams, C<moon>, C<earth>, and C<sun>. These are our three memcached servers.

=item 2.

And then we group them together as an upstream list entity named C<universe> with the C<upstream_list> directive provided by L<set-misc-nginx-module|https://github.com/openresty/set-misc-nginx-module>.

=item 3.

After that, we define an internal location named C</memc> for talking to the memcached cluster.

=item 4.

In this C</memc> location, we first set the C<$memc_key> variable with the query string (C<$args>), and then use the L<set_hashed_upstream|https://github.com/openresty/set-misc-nginx-module#set_hashed_upstream> directive to hash our L<$memc_key|https://github.com/openresty/memc-nginx-module#memc_key> over the upsteam list C<universe>, so as to obtain a concrete upstream name to be assigned to the variable C<$backend>.

=item 5.

We pass this C<$backend> variable into the L<memc_pass|https://github.com/openresty/memc-nginx-module#memc_pass> directive. The C<$backend> variable can hold a value among C<moon>, C<earth>, and C<sun>.

=item 6.

Also, we define the memcached caching expiration time to be 3600 seconds (i.e., an hour) by overriding the L<$memc_exptime|https://github.com/openresty/memc-nginx-module#memc_exptime> variable.

=item 7.

In our main public location C</>, we configure the C<$uri> variable as our cache key, and then configure L<srcache_fetch> for cache lookups and L<srcache_store> for cache updates. We're using two subrequests to our C</memc> location defined earlier in these two directives.


=back

One can use L<lua-nginx-module|https://github.com/openresty/lua-nginx-module>'s L<set_by_lua|https://github.com/openresty/lua-nginx-module#set_by_lua> or L<rewrite_by_lua|https://github.com/openresty/lua-nginx-module#rewrite_by_lua> directives to inject custom Lua code to compute the C<$backend> and/or C<$key> variables in the sample above.

One thing that should be taken care of is that memcached does have restriction on key lengths, i.e., 250 bytes, so for keys that may be very long, one could use the L<set_md5|https://github.com/openresty/set-misc-nginx-module#set_md5> directive or its friends to pre-hash the key to a fixed-length digest before assigning it to C<$memc_key> in the C</memc> location or the like.

Further, one can utilize the L<srcache_fetch_skip> and L<srcache_store_skip> directives to control what to cache and what not on a per-request basis, and Lua can also be used here in a similar way. So the possibility is really unlimited.

To maximize speed, we often enable TCP (or Unix Domain Socket) connection pool for our memcached upstreams provided by L<HttpUpstreamKeepaliveModule|http://wiki.nginx.org/HttpUpstreamKeepaliveModule>, for example,


     upstream moon {
         server 10.62.136.54:11211;
         server unix:/tmp/memcached.sock backup;
         keepalive 10;
     }

where we define a connection pool which holds up to 10 keep-alive connections (per nginx worker process) for our C<moon> upstream (cluster).




=head2 Caching with Redis

Redis is an alternative key-value store with many additional features.

Here is a working example using the lua-resty-redis module:


      location ~ '\.php$|^/update.php' {
        # cache setup
        set $key $request_uri;
        try_files $uri =404;
    
        srcache_fetch_skip $skip_cache;
        srcache_store_skip $skip_cache;
    
        srcache_response_cache_control off;
        srcache_store_statuses 200 201 301 302 307 308 404 503;
    
        set_escape_uri $escaped_key $key;
    
        srcache_fetch GET /redis-fetch $key;
        srcache_store PUT /redis-store key=$escaped_key;
    
        more_set_headers 'X-Cache-Fetch-Status $srcache_fetch_status';
        more_set_headers 'X-Cache-Store-Status $srcache_store_status';
    
        fastcgi_split_path_info ^(.+?\.php)(|/.*)$;
        # Security note: If you're running a version of PHP older than the
        # latest 5.3, you should have "cgi.fix_pathinfo = 0;" in php.ini.
        # See http://serverfault.com/q/627903/94922 for details.
        include fastcgi_params;
        # Block httproxy attacks. See https://httpoxy.org/.
        fastcgi_param HTTP_PROXY "";
        fastcgi_param SCRIPT_FILENAME /var/www/html/$fastcgi_script_name;
        fastcgi_param PATH_INFO $fastcgi_path_info;
        fastcgi_param QUERY_STRING $query_string;
        fastcgi_intercept_errors on;
    
        fastcgi_pass upstream-name;
      }
    
      location /redis-fetch {
        internal;
    
        resolver 8.8.8.8 valid=300s;
        resolver_timeout 10s;
    
        content_by_lua_block {
          local key = assert(ngx.var.request_uri, "no key found")
          local redis = require "resty.redis"
          local red, err = redis:new()
          if not red then
            ngx.log(ngx.ERR, "Failed to create redis variable, error -> ", err)
            ngx.exit(500)
          end
          assert(red:connect("redis-master.default.svc.cluster.local", 6379))
          if not red then
            ngx.log(ngx.ERR, "Failed to connect to redis, error -> ", err)
            ngx.exit(500)
          end
          local res, err = red:auth("redispassword")
          if not res then
            ngx.say("failed to authenticate, ", err)
            ngx.exit(500)
          end
          local data = assert(red:get(key))
          assert(red:set_keepalive(10000, 100))
          if res == ngx.null then
            return ngx.exit(404)
          end
          ngx.print(data)
        }
      }
    
      location /redis-store {
        internal;
    
        resolver 8.8.8.8 valid=300s;
        resolver_timeout 10s;
    
        content_by_lua_block {
          local value = assert(ngx.req.get_body_data(), "no value found")
          local key = assert(ngx.var.request_uri, "no key found")
          local redis = require "resty.redis"
          local red, err = redis:new()
          if not red then
            ngx.log(ngx.ERR, "Failed to create redis variable, error -> ", err)
            ngx.exit(500)
          end
          assert(red:connect("redis-master.default.svc.cluster.local", 6379))
          if not red then
            ngx.log(ngx.ERR, "Failed to connect to redis, error -> ", err)
            ngx.exit(500)
          end
          local res, err = red:auth("redispassword")
          if not res then
            ngx.say("failed to authenticate, ", err)
            ngx.exit(500)
          end
          local data = assert(red:set(key, value))
          assert(red:set_keepalive(10000, 100))
          if res == ngx.null then
            return ngx.exit(404)
          end
        }
      }

Here is a working example by using the HTTPRedis (fetch) and Redis2 (store) modules:


     location /api {
         default_type text/css;
    
         set $key $uri;
         set_escape_uri $escaped_key $key;
    
         srcache_fetch GET /redis $key;
         srcache_store PUT /redis2 key=$escaped_key&exptime=120;
    
         # fastcgi_pass/proxy_pass/drizzle_pass/postgres_pass/echo/etc
     }
    
     location = /redis {
         internal;
    
         set_md5 $redis_key $args;
         redis_pass 127.0.0.1:6379;
     }
    
     location = /redis2 {
         internal;
    
         set_unescape_uri $exptime $arg_exptime;
         set_unescape_uri $key $arg_key;
         set_md5 $key;
    
         redis2_query set $key $echo_request_body;
         redis2_query expire $key $exptime;
         redis2_pass 127.0.0.1:6379;
     }

This example makes use of the L<$echo_request_body|https://github.com/openresty/echo-nginx-module#echo_request_body> variable provided by L<echo-nginx-module|https://github.com/openresty/echo-nginx-module>. Note that you need the latest version of L<echo-nginx-module|https://github.com/openresty/echo-nginx-module>, C<v0.38rc2> because earlier versions may not work reliably.

Also, you need both L<HttpRedisModule|http://wiki.nginx.org/HttpRedisModule> and L<redis2-nginx-module|https://github.com/openresty/redis2-nginx-module>. The former is used in the L<srcache_fetch> subrequest and the latter is used in the L<srcache_store> subrequest.

The Nginx core also has a bug that could prevent L<redis2-nginx-module|https://github.com/openresty/redis2-nginx-module>'s pipelining support from working properly in certain extreme conditions. And the following patch fixes this:

http://mailman.nginx.org/pipermail/nginx-devel/2012-March/002040.html

Note that, however, if you are using the L<OpenResty|http://openresty.org/> 1.0.15.3 bundle or later, then you already have everything that you need here in the bundle.




=head2 Cache Key Preprocessing

It is often desired to preprocess the cache key to exclude random noises that may hurt the cache hit rate. For example, random session IDs in the URI arguments are usually desired to get removed.

Consider the following URI querystring

    SID=BC3781C3-2E02-4A11-89CF-34E5CFE8B0EF&UID=44332&L=EN&M=1&H=1&UNC=0&SRC=LK&RT=62

we want to remove the C<SID> and C<UID> arguments from it. It is easy to achieve if you use L<lua-nginx-module|https://github.com/openresty/lua-nginx-module> at the same time:


     location = /t {
         rewrite_by_lua '
             local args = ngx.req.get_uri_args()
             args.SID = nil
             args.UID = nil
             ngx.req.set_uri_args(args)
         ';
    
         echo $args;
     }

Here we use the L<echo|https://github.com/openresty/echo-nginx-module#echo> directive from L<echo-nginx-module|https://github.com/openresty/echo-nginx-module> to dump out
the final value of L<$args|http://nginx.org/en/docs/http/ngx_http_core_module.html#var_args> in the end. You can replace it with your
L<srcache-nginx-module|https://github.com/openresty/srcache-nginx-module> configurations and upstream configurations instead for
your case. Let's test this /t interface with curl:

    $ curl 'localhost:8081/t?RT=62&SID=BC3781C3-2E02-4A11-89CF-34E5CFE8B0EF&UID=44332&L=EN&M=1&H=1&UNC=0&SRC=LK'
    M=1&UNC=0&RT=62&H=1&L=EN&SRC=LK

It is worth mentioning that, if you want to retain the order of the URI
arguments, then you can do string substitutions on the value of L<$args|http://nginx.org/en/docs/http/ngx_http_core_module.html#var_args>
directly, for example,

    location = /t {
        rewrite_by_lua '
            local args = ngx.var.args
            newargs, n, err = ngx.re.gsub(args, [[\b[SU]ID=[^&]*&?]], "", "jo")
            if n and n > 0 then
                ngx.var.args = newargs
            end
        ';

        echo $args;
    }

Now test it with the original curl command again, we get exactly what
we would expect:

    RT=62&L=EN&M=1&H=1&UNC=0&SRC=LK

But for caching purposes, it's good to normalize the URI argument
order so that you can increase the cache hit rate. And the hash table
entry order used by LuaJIT or Lua can be used to normalize the order
as a nice side effect.




=head1 Directives




=head2 srcache_fetch

B<syntax:> I<srcache_fetch E<lt>methodE<gt> E<lt>uriE<gt> E<lt>argsE<gt>?>

B<default:> I<no>

B<context:> I<http, server, location, location if>

B<phase:> I<post-access>

This directive registers an access phase handler that will issue an Nginx subrequest to lookup the cache.

When the subrequest returns status code other than C<200>, than a cache miss is signaled and the control flow will continue to the later phases including the content phase configured by L<ngx_http_proxy_module|http://nginx.org/en/docs/http/ngx_http_proxy_module.html>, L<ngx_http_fastcgi_module|http://nginx.org/en/docs/http/ngx_http_fastcgi_module.html>, and others. If the subrequest returns C<200 OK>, then a cache hit is signaled and this module will send the subrequest's response as the current main request's response to the client directly.

This directive will always run at the end of the access phase, such that L<ngx_http_access_module|http://nginx.org/en/docs/http/ngx_http_access_module.html>'s L<allow|http://nginx.org/en/docs/http/ngx_http_access_module.html#allow> and L<deny|http://nginx.org/en/docs/http/ngx_http_access_module.html#deny> will always run I<before> this.

You can use the L<srcache_fetch_skip> directive to disable cache look-up selectively.




=head2 srcache_fetch_skip

B<syntax:> I<srcache_fetch_skip E<lt>flagE<gt>>

B<default:> I<srcache_fetch_skip 0>

B<context:> I<http, server, location, location if>

B<phase:> I<post-access>

The C<< <flag> >> argument supports nginx variables. When this argument's value is not empty I<and> not equal to C<0>, then the fetching process will be unconditionally skipped.

For example, to skip caching requests which have a cookie named C<foo> with the value C<bar>, we can write


     location / {
         set $key ...;
         set_by_lua $skip '
             if ngx.var.cookie_foo == "bar" then
                 return 1
             end
             return 0
         ';
    
         srcache_fetch_skip $skip;
         srcache_store_skip $skip;
    
         srcache_fetch GET /memc $key;
         srcache_store GET /memc $key;
    
         # proxy_pass/fastcgi_pass/content_by_lua/...
     }

where L<lua-nginx-module|https://github.com/openresty/lua-nginx-module> is used to calculate the value of the C<$skip> variable at the (earlier) rewrite phase. Similarly, the C<$key> variable can be computed by Lua using the L<set_by_lua|https://github.com/openresty/lua-nginx-module#set_by_lua> or L<rewrite_by_lua|https://github.com/openresty/lua-nginx-module#rewrite_by_lua> directive too.

The standard L<map|http://nginx.org/en/docs/http/ngx_http_map_module.html#map> directive can also be used to compute the value of the C<$skip> variable used in the sample above:


     map $cookie_foo $skip {
         default     0;
         bar         1;
     }

but your L<map|http://nginx.org/en/docs/http/ngx_http_map_module.html#map> statement should be put into the C<http> config block in your C<nginx.conf> file though.




=head2 srcache_store

B<syntax:> I<srcache_store E<lt>methodE<gt> E<lt>uriE<gt> E<lt>argsE<gt>?>

B<default:> I<no>

B<context:> I<http, server, location, location if>

B<phase:> I<output-filter>

This directive registers an output filter handler that will issue an Nginx subrequest to save the response of the current main request into a cache backend. The status code of the subrequest will be ignored.

You can use the L<srcache_store_skip> and L<srcache_store_max_size> directives to disable caching for certain requests in case of a cache miss.

Since the C<v0.12rc7> release, both the response status line, response headers, and response bodies will be put into the cache. By default, the following special response headers will not be cached:


=over


=item *

Connection

=item *

Keep-Alive

=item *

Proxy-Authenticate

=item *

Proxy-Authorization

=item *

TE

=item *

Trailers

=item *

Transfer-Encoding

=item *

Upgrade

=item *

Set-Cookie


=back

You can use the L<srcache_store_pass_header> and/or L<srcache_store_hide_header> directives to control what headers to cache and what not.

The original response's data chunks get emitted as soon as
they arrive. C<srcache_store> just copies and collects the data in an output filter without postponing them from being sent downstream.

But please note that even though all the response data will be sent immediately, the current Nginx request lifetime will not finish until the srcache_store subrequest completes. That means a delay in closing the TCP connection on the server side (when HTTP keepalive is disabled, but proper HTTP clients should close the connection actively on the client side, which adds no extra delay or other issues at all) or serving the next request sent on the same TCP connection (when HTTP keepalive is in action).




=head2 srcache_store_max_size

B<syntax:> I<srcache_store_max_size E<lt>sizeE<gt>>

B<default:> I<srcache_store_max_size 0>

B<context:> I<http, server, location, location if>

B<phase:> I<output-header-filter>

When the response body length is exceeding this size, this module will not try to store the response body into the cache using the subrequest template that is specified in L<srcache_store>.

This is particular useful when using a cache storage backend that does have a hard upper limit on the input data. For example, the Memcached server has a default limit of C<1 MB> by item.

When C<0> is specified (the default value), there's no limit check at all.




=head2 srcache_store_skip

B<syntax:> I<srcache_store_skip E<lt>flagE<gt>>

B<default:> I<srcache_store_skip 0>

B<context:> I<http, server, location, location if>

B<phase:> I<output-header-filter>

The C<< <flag> >> argument supports Nginx variables. When this argument's value is not empty I<and> not equal to C<0>, then the storing process will be unconditionally skipped.

Starting from the C<v0.25> release, the C<< <flag> >> expression (possibly containing Nginx variables) can be evaluated up to twice: the first time is right after the response header is being sent and when the C<< <flag> >> expression is not evaluated to true values it will be evaluated again right after the end of the response body data stream is seen. Before C<v0.25>, only the first time evaluation is performed.

Here's an example using Lua to set $nocache to avoid storing URIs that contain the string "/tmp":


     set_by_lua $nocache '
         if string.match(ngx.var.uri, "/tmp") then
             return 1
         end
         return 0';
    
     srcache_store_skip $nocache;




=head2 srcache_store_statuses

B<syntax:> I<srcache_store_statuses E<lt>status1E<gt> E<lt>status2E<gt> ..>

B<default:> I<srcache_store_statuses 200 301 302 307 308>

B<context:> I<http, server, location, location if>

B<phase:> I<output-header-filter>

This directive controls what responses to store to the cache according to their status code.

By default, only C<200>, C<301>, C<302>, C<307> and C<308> responses will be stored to cache and any other responses will skip L<srcache_store>.

You can specify arbitrary positive numbers for the response status code that you'd like to cache, even including error code like C<404> and C<503>. For example:


     srcache_store_statuses 200 201 301 302 307 308 404 503;

At least one argument should be given to this directive.

This directive was first introduced in the C<v0.13rc2> release.




=head2 srcache_store_ranges

B<syntax:> I<srcache_store_ranges on|off>

B<default:> I<srcache_store_ranges off>

B<context:> I<http, server, location, location if>

B<phase:> I<output-body-filter>

When this directive is turned on (default to C<off>), L<srcache_store> will also store 206 Partial Content responses generated by the standard C<ngx_http_range_filter_module>. If you turn this directive on, you MUST add C<$http_range> to your cache keys. For example,


     location / {
         set $key "$uri$args$http_range";
         srcache_fetch GET /memc $key;
         srcache_store PUT /memc $key;
     }

This directive was first introduced in the C<v0.27> release.




=head2 srcache_header_buffer_size

B<syntax:> I<srcache_header_buffer_size E<lt>sizeE<gt>>

B<default:> I<srcache_header_buffer_size 4k/8k>

B<context:> I<http, server, location, location if>

B<phase:> I<output-header-filter>

This directive controles the header buffer when serializing response headers for L<srcache_store>. The default size is the page size, usually C<4k> or C<8k> depending on specific platforms.

Note that the buffer is not used to hold all the response headers, but just each individual header. So the buffer is merely needed to be big enough to hold the longest response header.

This directive was first introduced in the C<v0.12rc7> release.




=head2 srcache_store_hide_header

B<syntax:> I<srcache_store_hide_header E<lt>headerE<gt>>

B<default:> I<no>

B<context:> I<http, server, location, location if>

B<phase:> I<output-header-filter>

By default, this module caches all the response headers except the following ones:


=over


=item *

Connection

=item *

Keep-Alive

=item *

Proxy-Authenticate

=item *

Proxy-Authorization

=item *

TE

=item *

Trailers

=item *

Transfer-Encoding

=item *

Upgrade

=item *

Set-Cookie


=back

You can hide even more response headers from L<srcache_store> by listing their names (case-insensitive) by means of this directive. For examples,


     srcache_store_hide_header X-Foo;
     srcache_store_hide_header Last-Modified;

Multiple occurrences of this directive are allowed in a single location.

This directive was first introduced in the C<v0.12rc7> release.

See also L<srcache_store_pass_header>.




=head2 srcache_store_pass_header

B<syntax:> I<srcache_store_pass_header E<lt>headerE<gt>>

B<default:> I<no>

B<context:> I<http, server, location, location if>

B<phase:> I<output-header-filter>

By default, this module caches all the response headers except the following ones:


=over


=item *

Connection

=item *

Keep-Alive

=item *

Proxy-Authenticate

=item *

Proxy-Authorization

=item *

TE

=item *

Trailers

=item *

Transfer-Encoding

=item *

Upgrade

=item *

Set-Cookie


=back

You can force L<srcache_store> to store one or more of these response headers from L<srcache_store> by listing their names (case-insensitive) by means of this directive. For examples,


     srcache_store_pass_header Set-Cookie;
     srcache_store_pass_header Proxy-Autenticate;

Multiple occurrences of this directive are allowed in a single location.

This directive was first introduced in the C<v0.12rc7> release.

See also L<srcache_store_hide_header>.




=head2 srcache_methods

B<syntax:> I<srcache_methods E<lt>methodE<gt>...>

B<default:> I<srcache_methods GET HEAD>

B<context:> I<http, server, location>

B<phase:> I<post-access, output-header-filter>

This directive specifies HTTP request methods that are considered by either L<srcache_fetch> or L<srcache_store>. HTTP request methods not listed will be skipped completely from the cache.

The following HTTP methods are allowed: C<GET>, C<HEAD>, C<POST>, C<PUT>, and C<DELETE>. The C<GET> and C<HEAD> methods are always implicitly included in the list regardless of their presence in this directive.

Note that since the C<v0.17> release C<HEAD> requests are always skipped by L<srcache_store> because their responses never carry a response body.

This directive was first introduced in the C<v0.12rc7> release.




=head2 srcache_ignore_content_encoding

B<syntax:> I<srcache_ignore_content_encoding on|off>

B<default:> I<srcache_ignore_content_encoding off>

B<context:> I<http, server, location, location if>

B<phase:> I<output-header-filter>

When this directive is turned C<off> (which is the default), non-empty C<Content-Encoding> response header will cause L<srcache_store> skip storing the whole response into the cache and issue a warning into nginx's C<error.log> file like this:

    [warn] 12500#0: *1 srcache_store skipped due to response header "Content-Encoding: gzip"
                (maybe you forgot to disable compression on the backend?)

Turning on this directive will ignore the C<Content-Encoding> response header and store the response as usual (and also without warning).

It's recommended to always disable gzip/deflate compression on your backend server by specifying the following line in your C<nginx.conf> file:


     proxy_set_header  Accept-Encoding  "";

This directive was first introduced in the C<v0.12rc7> release.




=head2 srcache_request_cache_control

B<syntax:> I<srcache_request_cache_control on|off>

B<default:> I<srcache_request_cache_control off>

B<context:> I<http, server, location>

B<phase:> I<post-access, output-header-filter>

When this directive is turned C<on>, the request headers C<Cache-Control> and C<Pragma> will be honored by this module in the following ways:


=over


=item 1.

L<srcache_fetch>, i.e., the cache lookup operation, will be skipped when request headers C<Cache-Control: no-cache> and/or C<Pragma: no-cache> are present.

=item 2.

L<srcache_store>, i.e., the cache store operation, will be skipped when the request header C<Cache-Control: no-store> is specified.


=back

Turning off this directive will disable this functionality and is considered safer for busy sites mainly relying on cache for speed.

This directive was first introduced in the C<v0.12rc7> release.

See also L<srcache_response_cache_control>.




=head2 srcache_response_cache_control

B<syntax:> I<srcache_response_cache_control on|off>

B<default:> I<srcache_response_cache_control on>

B<context:> I<http, server, location>

B<phase:> I<output-header-filter>

When this directive is turned C<on>, the response headers C<Cache-Control> and C<Expires> will be honored by this module in the following ways:


=over


=item *

C<Cache-Control: private> skips L<srcache_store>,

=item *

C<Cache-Control: no-store> skips L<srcache_store>,

=item *

C<Cache-Control: no-cache> skips L<srcache_store>,

=item *

C<Cache-Control: max-age=0> skips L<srcache_store>,

=item *

and C<< Expires: <date-no-more-recently-than-now> >> skips L<srcache_store>.


=back

This directive takes priority over the L<srcache_store_no_store>, L<srcache_store_no_cache>, and L<srcache_store_private> directives.

This directive was first introduced in the C<v0.12rc7> release.

See also L<srcache_request_cache_control>.




=head2 srcache_store_no_store

B<syntax:> I<srcache_store_no_store on|off>

B<default:> I<srcache_store_no_store off>

B<context:> I<http, server, location>

B<phase:> I<output-header-filter>

Turning this directive on will force responses with the header C<Cache-Control: no-store> to be stored into the cache when L<srcache_response_cache_control> is turned C<on> I<and> other conditions are met. Default to C<off>.

This directive was first introduced in the C<v0.12rc7> release.




=head2 srcache_store_no_cache

B<syntax:> I<srcache_store_no_cache on|off>

B<default:> I<srcache_store_no_cache off>

B<context:> I<http, server, location>

B<phase:> I<output-header-filter>

Turning this directive on will force responses with the header C<Cache-Control: no-cache> to be stored into the cache when L<srcache_response_cache_control> is turned C<on> I<and> other conditions are met. Default to C<off>.

This directive was first introduced in the C<v0.12rc7> release.




=head2 srcache_store_private

B<syntax:> I<srcache_store_private on|off>

B<default:> I<srcache_store_private off>

B<context:> I<http, server, location>

B<phase:> I<output-header-filter>

Turning this directive on will force responses with the header C<Cache-Control: private> to be stored into the cache when L<srcache_response_cache_control> is turned C<on> I<and> other conditions are met. Default to C<off>.

This directive was first introduced in the C<v0.12rc7> release.




=head2 srcache_default_expire

B<syntax:> I<srcache_default_expire E<lt>timeE<gt>>

B<default:> I<srcache_default_expire 60s>

B<context:> I<http, server, location, location if>

B<phase:> I<output-header-filter>

This directive controls the default expiration time period that is allowed for the L<$srcache_expire> variable value when neither C<Cache-Control: max-age=N> nor C<Expires> are specified in the response headers.

The C<< <time> >> argument values are in seconds by default. But it's wise to always explicitly specify the time unit to avoid confusion. Time units supported are "s"(seconds), "ms"(milliseconds), "y"(years), "M"(months), "w"(weeks), "d"(days), "h"(hours), and "m"(minutes). For example,


     srcache_default_expire 30m; # 30 minutes

This time must be less than 597 hours.

The semantics of a zero expiration time depends on the actual cache backend storage you are currently using, which is agnostic to this
module. In the case of memcached, for example, zero expiration times mean that the item will never expire.

This directive was first introduced in the C<v0.12rc7> release.




=head2 srcache_max_expire

B<syntax:> I<srcache_max_expire E<lt>timeE<gt>>

B<default:> I<srcache_max_expire 0>

B<context:> I<http, server, location, location if>

B<phase:> I<output-header-filter>

This directive controls the maximal expiration time period that is allowed for the L<$srcache_expire> variable value. This setting takes priority over other calculating methods.

The C<< <time> >> argument values are in seconds by default. But it's wise to always explicitly specify the time unit to avoid confusion. Time units supported are "s"(seconds), "ms"(milliseconds), "y"(years), "M"(months), "w"(weeks), "d"(days), "h"(hours), and "m"(minutes). For example,


     srcache_max_expire 2h;  # 2 hours

This time must be less than 597 hours.

When C<0> is specified, which is the default setting, then there will be I<no> limit at all.

This directive was first introduced in the C<v0.12rc7> release.




=head1 Variables




=head2 $srcache_expire

B<type:> I<integer>

B<cacheable:> I<no>

B<writable:> I<no>

This Nginx variable gives the recommended expiration time period (in seconds) for the current response being stored into the cache. The algorithm of computing the value is as follows:


=over


=item 1.

When the response header C<Cache-Control: max-age=N> is specified, then C<N> will be used as the expiration time,

=item 2.

otherwise if the response header C<Expires> is specified, then the expiration time will be obtained by subtracting the current time stamp from the time specified in the C<Expires> header,

=item 3.

when neither C<Cache-Control: max-age=N> nor C<Expires> headers are specified, use the value specified in the L<srcache_default_expire> directive.


=back

The final value of this variable will be the value specified by the L<srcache_max_expire> directive if the value obtained in the algorithm above exceeds the maximal value (if any).

You don't have to use this variable for the expiration time.

This variable was first introduced in the C<v0.12rc7> release.




=head2 $srcache_fetch_status

B<type:> I<string>

B<cacheable:> I<no>

B<writable:> I<no>

This Nginx variable is evaluated to the status of the "fetch" phase for the caching system. Three values are possible, C<HIT>, C<MISS>, and C<BYPASS>.

When the "fetch" subrequest returns status code other than C<200> or its response data is not well-formed, then this variable is evaluated to the value C<MISS>.

The value of this variable is only meaningful after the C<access> request processing phase, or C<BYPASS> is always given.

This variable was first introduced in the C<v0.14> release.




=head2 $srcache_store_status

B<type:> I<string>

B<cacheable:> I<no>

B<writable:> I<no>

This Nginx variable gives the current caching status for the "store" phase. Two possible values, C<STORE> and C<BYPASS> can be obtained.

Because the responses for the "store" subrequest are always discarded, so the value of this variable will always be C<STORE> as long as the "store" subrequest is actually issued.

The value of this variable is only meaningful at least when the request headers of the current (main) request are being sent. The final result can only be obtained after all the response body has been sent if the C<Content-Length> response header is not specified for the main request.

This variable was first introduced in the C<v0.14> release.




=head1 Known Issues


=over


=item *

On certain systems, enabling aio and/or sendfile may stop L<srcache_store> from working. You can disable them in the locations configured by L<srcache_store>.

=item *

The L<srcache_store> directive can not be used to capture the responses generated by L<echo-nginx-module|https://github.com/openresty/echo-nginx-module>'s subrequest directivees like L<echo_subrequest_async|https://github.com/openresty/echo-nginx-module#echo_subrequest_async> and L<echo_location|https://github.com/openresty/echo-nginx-module#echo_location>. You are recommended to use HttpLuaModule to initiate and capture subrequests, which should work with L<srcache_store>.


=back




=head1 Caveats


=over


=item *

It is recommended to disable your backend server's gzip compression and use nginx's L<ngx_http_gzip_module|http://nginx.org/en/docs/http/ngx_http_gzip_module.html> to do the job. In case of L<ngx_http_proxy_module|http://nginx.org/en/docs/http/ngx_http_proxy_module.html>, you can use the following configure setting to disable backend gzip compression:

     proxy_set_header  Accept-Encoding  "";


=back


=over


=item *

Do I<not> use L<ngx_http_rewrite_module|http://nginx.org/en/docs/http/ngx_http_rewrite_module.html>'s L<if|http://nginx.org/en/docs/http/ngx_http_rewrite_module.html#if> directive in the same location as this module's, because "L<if|http://nginx.org/en/docs/http/ngx_http_rewrite_module.html#if> is evil". Instead, use L<ngx_http_map_module|http://nginx.org/en/docs/http/ngx_http_map_module.html> or L<lua-nginx-module|https://github.com/openresty/lua-nginx-module> combined with this module's L<srcache_store_skip> and/or L<srcache_fetch_skip> directives. For example:

     map $request_method $skip_fetch {
         default     0;
         POST        1;
         PUT         1;
     }
    
     server {
         listen 8080;
    
         location /api/ {
             set $key "$uri?$args";
    
             srcache_fetch GET /memc $key;
             srcache_store PUT /memc $key;
    
             srcache_methods GET PUT POST;
             srcache_fetch_skip $skip_fetch;
    
             # proxy_pass/drizzle_pass/content_by_lua/echo/...
         }
     }


=back




=head1 Trouble Shooting

To debug issues, you should always check your Nginx C<error.log> file first. If no error messages are printed, you need to enable the Nginx debugging logs to get more details, as explained in L<debugging log|http://nginx.org/en/docs/debugging_log.html>.

Several common pitfalls for beginners:


=over


=item *

The original response carries a C<Cache-Control> header that explicitly disables caching and you do not configure directives like L<srcache_response_cache_control>.

=item *

The original response is already gzip compressed, which is not cached by default (see L<srcache_ignore_content_encoding>).

=item *

Memcached might return C<CLIENT_ERROR bad command line format> when using a too long key (250 chars as of version 1.4.25). It is thus safer to use C<set_md5 $key $uri$args;> instead of C<set $key $uri$args;>. The C<set_md5> directive (and more) is available from L<OpenResty's set-misc module|https://github.com/openresty/set-misc-nginx-module>.

=item *

Nginx might return C<client intended to send too large body> when trying to store objects larger than 1m to the storage backend, in which case nginx C<client_max_body_size> must be set to a higher value.

=item *

Memcached might fail to store objects larger than 1m, causing errors like C<srcache_store subrequest failed status=502>. Since version 1.4.2, memcached supports a command-line C<-I> option to override the default size of each slab page. Please read its manpage for more information.


=back




=head1 Installation

It is recommended to install this module as well as the Nginx core and many other goodies via the L<OpenResty bundle|http://openresty.org>. It is the easiest way and most safe way to set things up. See OpenResty's L<installation instructions|http://openresty.org/#Installation> for details.

Alternatively, you can build Nginx with this module all by yourself:


=over


=item *

Grab the nginx source code from L<nginx.org|http://nginx.org>, for example, the version 1.9.15 (see L<Nginx Compatibility>),

=item *

and then apply the patch to your nginx source tree that fixes an important bug in the mainline Nginx core: E<lt>https://raw.github.com/openresty/openresty/master/patches/nginx-1.4.3-upstream_truncation.patchE<gt> (you do NOT need this patch if you are using nginx 1.5.3 and later versions.)

=item *

after that, download the latest version of the release tarball of this module from srcache-nginx-module L<file list|https://github.com/openresty/srcache-nginx-module/tags>,

=item *

and finally build the Nginx source with this module


=back


     wget 'http://nginx.org/download/nginx-1.9.15.tar.gz'
     tar -xzvf nginx-1.9.15.tar.gz
     cd nginx-1.9.15/
    
     # Here we assume you would install you nginx under /opt/nginx/.
     ./configure --prefix=/opt/nginx \
          --add-module=/path/to/srcache-nginx-module
    
     make -j2
     make install

Starting from NGINX 1.9.11, you can also compile this module as a dynamic module, by using the C<--add-dynamic-module=PATH> option instead of C<--add-module=PATH> on the
C<./configure> command line above. And then you can explicitly load the module in your C<nginx.conf> via the L<load_module|http://nginx.org/en/docs/ngx_core_module.html#load_module>
directive, for example,


    load_module /path/to/modules/ngx_http_srcache_filter_module.so;




=head1 Compatibility

The following versions of Nginx should work with this module:


=over


=item *

1.9.x (last tested: 1.9.15)

=item *

1.8.x

=item *

1.7.x (last tested: 1.7.10)

=item *

1.5.x (last tested: 1.5.12)

=item *

1.4.x (last tested: 1.4.4)

=item *

1.3.x (last tested: 1.3.7)

=item *

1.2.x (last tested: 1.2.9)

=item *

1.1.x (last tested: 1.1.5)

=item *

1.0.x (last tested: 1.0.11)

=item *

0.9.x (last tested: 0.9.4)

=item *

0.8.x E<gt>= 0.8.54 (last tested: 0.8.54)


=back

Earlier versions of Nginx like 0.7.x, 0.6.x and 0.5.x will I<not> work.

If you find that any particular version of Nginx above 0.7.44 does not work with this module, please consider reporting a bug.




=head1 Community




=head2 English Mailing List

The L<openresty-en|https://groups.google.com/group/openresty-en> mailing list is for English speakers.




=head2 Chinese Mailing List

The L<openresty|https://groups.google.com/group/openresty> mailing list is for Chinese speakers.




=head1 Bugs and Patches

Please submit bug reports, wishlists, or patches by


=over


=item 1.

creating a ticket on the L<GitHub Issue Tracker|https://github.com/openresty/srcache-nginx-module/issues>,

=item 2.

or posting to the L<OpenResty community>.


=back




=head1 Source Repository

Available on github at L<openrestyE<sol>srcache-nginx-module|https://github.com/openresty/srcache-nginx-module>.




=head1 Test Suite

This module comes with a Perl-driven test suite. The L<test cases|https://github.com/openresty/srcache-nginx-module/tree/master/test/t> are L<declarative|https://github.com/openresty/srcache-nginx-module/blob/master/test/t/main-req.t> too. Thanks to the L<Test::Nginx|http://search.cpan.org/perldoc?Test::Base> module in the Perl world.

To run it on your side:

     $ PATH=/path/to/your/nginx-with-srcache-module:$PATH prove -r t

You need to terminate any Nginx processes before running the test suite if you have changed the Nginx server binary.

Because a single nginx server (by default, C<localhost:1984>) is used across all the test scripts (C<.t> files), it's meaningless to run the test suite in parallel by specifying C<-jN> when invoking the C<prove> utility.

Some parts of the test suite requires modules L<ngx_http_rewrite_module|http://nginx.org/en/docs/http/ngx_http_rewrite_module.html>, L<echo-nginx-module|https://github.com/openresty/echo-nginx-module>, L<rds-json-nginx-module|https://github.com/openresty/rds-json-nginx-module>, and L<drizzle-nginx-module|https://github.com/openresty/drizzle-nginx-module> to be enabled as well when building Nginx.




=head1 TODO


=over


=item *

add gzip compression and decompression support.

=item *

add new nginx variable C<$srcache_key> and new directives C<srcache_key_ignore_args>, C<srcache_key_filter_args>, and C<srcache_key_sort_args>.


=back




=head1 Getting involved

You'll be very welcomed to submit patches to the author or just ask for a commit bit to the source repository on GitHub.




=head1 Author

Yichun "agentzh" Zhang (章亦春) E<lt>agentzh@gmail.comE<gt>, CloudFlare Inc.




=head1 Copyright & License

Copyright (c) 2010-2016, Yichun "agentzh" Zhang (章亦春) E<lt>agentzh@gmail.comE<gt>, CloudFlare Inc.

This module is licensed under the terms of the BSD license.

Redistribution and use in source and binary forms, with or without modification, are permitted provided that the following conditions are met:


=over


=item *

Redistributions of source code must retain the above copyright notice, this list of conditions and the following disclaimer.

=item *

Redistributions in binary form must reproduce the above copyright notice, this list of conditions and the following disclaimer in the documentation and/or other materials provided with the distribution.


=back

THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.




=head1 See Also


=over


=item *

L<memc-nginx-module|https://github.com/openresty/memc-nginx-module>

=item *

L<lua-nginx-module|https://github.com/openresty/lua-nginx-module>

=item *

L<set-misc-nginx-module|https://github.com/openresty/set-misc-nginx-module>

=item *

The L<openresty bundle|http://openresty.org>


=back