@@ -264,7 +264,112 @@ Caching with Redis
264
264
265
265
Redis is an alternative key-value store with many additional features.
266
266
267
- Here is a working example by using Redis:
267
+ Here is a working example using the lua-resty-redis module:
268
+
269
+ ```
270
+ location ~ '\.php$|^/update.php' {
271
+ # cache setup
272
+ set $key $request_uri;
273
+ try_files $uri =404;
274
+
275
+ srcache_fetch_skip $skip_cache;
276
+ srcache_store_skip $skip_cache;
277
+
278
+ srcache_response_cache_control off;
279
+ srcache_store_statuses 200 201 301 302 404 503;
280
+
281
+ set_escape_uri $escaped_key $key;
282
+
283
+ srcache_fetch GET /redis-fetch $key;
284
+ srcache_store PUT /redis-store key=$escaped_key;
285
+
286
+ more_set_headers 'X-Cache-Fetch-Status $srcache_fetch_status';
287
+ more_set_headers 'X-Cache-Store-Status $srcache_store_status';
288
+
289
+ fastcgi_split_path_info ^(.+?\.php)(|/.*)$;
290
+ # Security note: If you're running a version of PHP older than the
291
+ # latest 5.3, you should have "cgi.fix_pathinfo = 0;" in php.ini.
292
+ # See http://serverfault.com/q/627903/94922 for details.
293
+ include fastcgi_params;
294
+ # Block httproxy attacks. See https://httpoxy.org/.
295
+ fastcgi_param HTTP_PROXY "";
296
+ fastcgi_param SCRIPT_FILENAME /var/www/html/$fastcgi_script_name;
297
+ fastcgi_param PATH_INFO $fastcgi_path_info;
298
+ fastcgi_param QUERY_STRING $query_string;
299
+ fastcgi_intercept_errors on;
300
+
301
+ fastcgi_pass upstream-name;
302
+ }
303
+
304
+ location /redis-fetch {
305
+ internal;
306
+
307
+ resolver 8.8.8.8 valid=300s;
308
+ resolver_timeout 10s;
309
+
310
+ content_by_lua_block {
311
+ local key = assert(ngx.var.request_uri, "no key found")
312
+ local redis = require "resty.redis"
313
+ local red, err = redis:new()
314
+ if not red then
315
+ ngx.log(ngx.ERR, "Failed to create redis variable, error -> ", err)
316
+ ngx.exit(500)
317
+ end
318
+ assert(red:connect("redis-master.default.svc.cluster.local", 6379))
319
+ if not red then
320
+ ngx.log(ngx.ERR, "Failed to connect to redis, error -> ", err)
321
+ ngx.exit(500)
322
+ end
323
+ local res, err = red:auth("redispassword")
324
+ if not res then
325
+ ngx.say("failed to authenticate, ", err)
326
+ ngx.exit(500)
327
+ end
328
+ local data = assert(red:get(key))
329
+ assert(red:set_keepalive(10000, 100))
330
+ if res == ngx.null then
331
+ return ngx.exit(404)
332
+ end
333
+ ngx.print(data)
334
+ }
335
+ }
336
+
337
+ location /redis-store {
338
+ internal;
339
+
340
+ resolver 8.8.8.8 valid=300s;
341
+ resolver_timeout 10s;
342
+
343
+ content_by_lua_block {
344
+ local value = assert(ngx.req.get_body_data(), "no value found")
345
+ local key = assert(ngx.var.request_uri, "no key found")
346
+ local redis = require "resty.redis"
347
+ local red, err = redis:new()
348
+ if not red then
349
+ ngx.log(ngx.ERR, "Failed to create redis variable, error -> ", err)
350
+ ngx.exit(500)
351
+ end
352
+ assert(red:connect("redis-master.default.svc.cluster.local", 6379))
353
+ if not red then
354
+ ngx.log(ngx.ERR, "Failed to connect to redis, error -> ", err)
355
+ ngx.exit(500)
356
+ end
357
+ local res, err = red:auth("redispassword")
358
+ if not res then
359
+ ngx.say("failed to authenticate, ", err)
360
+ ngx.exit(500)
361
+ end
362
+ local data = assert(red:set(key, value))
363
+ assert(red:set_keepalive(10000, 100))
364
+ if res == ngx.null then
365
+ return ngx.exit(404)
366
+ end
367
+ }
368
+ }
369
+ ```
370
+
371
+
372
+ Here is a working example by using the HTTPRedis (fetch) and Redis2 (store) modules:
268
373
269
374
``` nginx
270
375
0 commit comments