Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Reverse proxy with caching is not working. #83

Open
suyogdilipkale opened this issue Apr 16, 2020 · 0 comments
Open

Reverse proxy with caching is not working. #83

suyogdilipkale opened this issue Apr 16, 2020 · 0 comments

Comments

@suyogdilipkale
Copy link

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. 

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant