openresty / openresty/srcache-nginx-module

Reverse proxy with caching is not working.

Open
#83 0 comments 0 reactions 0 assignees View on GitHub

Nobody has claimed this yet.

Dominant language
C
Stars
486
Forks
101
Avg merge
1h 28m
Merged PRs (30d)
2

Description

Before explaining my issue, let me clear that I am very new to nginx/openresty work.  

I am trying to see how I can use Redis to cache the most frequently requested pages and serve them from cache to improve the performance.  I am following this example: https://github.com/openresty/srcache-nginx-module#caching-with-redis

instead of redis2 openresty module I am using lua-redis module. Issue is every time when we hit same page request instead of serving response from cache it still serves from the source url.

Below is sample script from my config to create reverse proxy 

`server {

listen 80 default_server;
listen [::]:80 default_server;

root /usr/local/openresty/nginx/html/default;

index index.html index.htm;

server_name _;

location / {
 default_type application/json;

 set $key $request_uri;
 set_escape_uri $escaped_key $key;

 srcache_fetch GET /redis-fetch $key;
 srcache_store PUT /redis-store key=$escaped_key&exptime=3600;

 proxy_set_header Host $host;
 proxy_set_header X-Real-IP $remote_addr;
 proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
 proxy_pass http://application-server:8200;

}

location = /redis-fetch {
    internal  ;
    set_md5  $redis_key $args;
    content_by_lua_block {
            local redis = require "resty.redis"
            local red = redis:new()
            red:set_timeouts(1000, 1000, 1000)
            local ok, err = red:connect("Redis Server", 16318)
            if not ok then
                ngx.say("failed to connect: ", err)
                return
            end
            ok, err = red:get(ngx.var.redis_key)
            if not ok then
                ngx.say("failed to get key: ", err)
                return
            end
    }
}
location = /redis-store {
internal;
set_unescape_uri $exptime $arg_exptime;
set_unescape_uri $key $arg_key;
set_md5 $key;
   content_by_lua_block {
           local redis = require "resty.redis"
           local red = redis:new()
           red:set_timeouts(1000, 1000, 1000)
           local ok, err = red:connect("Redis Server", 16318)
           if not ok then
               ngx.say("failed to connect: ", err)
               return
           end
           ok, err = red:set(ngx.var.key,ngx.var.echo_request_body)
           if not ok then
               ngx.say("failed to set key: ", err)
               return
           end

           ok, err = red:expire(ngx.var.key,ngx.var.exptime)

 }

}
location /example {
default_type 'text/plain';
set $key "nginx-cache:$scheme $request_method $arg_key $host $request_uri $args";
return 200 $key;
content_by_lua_block {
ngx.say('Hello, Sammy!')
}
}

}
`
Any help is appreciated. 

Contributor guide

No contributing guide indexed for this repository

First steps

  1. Read the whole issue, then the project's contributing guide.
  2. Comment on the issue to say you are picking it up — it saves two people doing the same work.
  3. Fork the repository and make your change on a branch.
  4. Open a pull request that references the issue number.

Research direction

Start with the srcache-nginx-module caching-with-Redis example linked in the issue, then inspect the /redis-fetch and /redis-store locations in the supplied nginx configuration. Reproduce repeated requests while checking whether the Redis handlers are reached and whether the cached response is returned. Done means the same page request is served from Redis rather than the source URL.

Written by the indexing model from the issue text.

Assessment

Tech stack
lua, nginx, redis
Domain
backend, databases
Issue type
Bug
Difficulty
4/5
Estimated time
3-5 days
Activity status
Stale
Clarity
Needs clarification
Newbie friendliness
25/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.