Skip to content

Reverse proxy with caching is not working. #83

@suyogdilipkale

Description

@suyogdilipkale

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. 

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions