redis.lua bad argument in rawget

792 views
Skip to first unread message

Bino Oetomo

unread,
Oct 13, 2017, 10:05:05 AM10/13/17
to openresty-en
Dear All .

I need to adopt : http://sosedoff.com/2012/06/11/dynamic-nginx-upstreams-with-lua-and-redis.html



Here is my 'location'


 
       
       
#--------------------------------------
        location
/ {
           
set $upstream "";
            rewrite_by_lua
'
                local args = ngx.req.get_uri_args()
                for key, val in pairs(args) do
                    if key == "token" then
                        ngx.log(ngx.ALERT, "Token IS :".. val)
                        -- load global route cache into current request scope
                        -- by default vars are not shared between requests
                        local routes = _G.routes

                        -- setup routes cache if empty
                        if routes == nil then
                            routes = {}
                            ngx.log(ngx.ALERT, "Route cache is empty.")
                        end

                        -- try cached route first
                        local route = routes[ngx.var.http_host]
                        if route == nil then
                            local redis  = require "resty.redis"
                            local client = redis.connect("localhost", 6379)
                            route        = client:get(val)
                        end

                        -- fallback to redis for lookups
                        if route ~= nil then
                            ngx.var.upstream = route
                            routes[ngx.var.http_host] = route
                            _G.routes = routes
                        else
                            ngx.exit(ngx.HTTP_NOT_FOUND)
                        break
                    end
                end
            end
            '
;

            proxy_buffering             off
;
            proxy_set_header            
Host $host;
            proxy_set_header            X
-Forwarded-For $proxy_add_x_forwarded_for;
            proxy_redirect              off
;
            proxy_connect_timeout      
10;
            proxy_send_timeout          
30;
            proxy_read_timeout          
30;
            proxy_pass                  http
://$upstream;
       
}

       
#---------------------------------------------





but my redis.lua complain about ''rawget' (table expected, got string)'


Here is my debug log ,

       
        2017/10/13 20:48:34 [alert] 389#389: *1 [lua] rewrite_by_lua(nginx.conf:79):5: Token IS :mytoken01, client: 192.168.100.10, server: localhost, request: "GET /?token=mytoken01 HTTP/1.1", host: "web01.internal:89"
        2017/10/13 20:48:34 [alert] 389#389: *1 [lua] rewrite_by_lua(nginx.conf:79):13: Route cache is empty., client: 192.168.100.10, server: localhost, request: "GET /?token=mytoken01 HTTP/1.1", host: "web01.internal:89"
        2017/10/13 20:48:34 [error] 389#389: *1 lua entry thread aborted: runtime error: /usr/local/openresty/lualib/resty/redis.lua:78: bad argument #1 to 'rawget' (table expected, got string)
        stack traceback:
        coroutine 0:
            [C]: in function 'rawget'
            /usr/local/openresty/lualib/resty/redis.lua:78: in function 'connect'
            rewrite_by_lua(nginx.conf:79):20: in function <rewrite_by_lua(nginx.conf:79):1>, client: 192.168.100.10, server: localhost, request: "GET /?token=mytoken01 HTTP/1.1", host: "web01.internal:89"




Kindly please give me any enlightenment to fix this.

Sincerely

-bino-

Yichun Zhang (agentzh)

unread,
Oct 16, 2017, 3:27:47 PM10/16/17
to openresty-en
Hello!

On Fri, Oct 13, 2017 at 7:05 AM, Bino Oetomo wrote:
> local redis = require "resty.redis"
> local client = redis.connect("localhost", 6379)

One obvious mistake is that you should not use `localhost` here.
Rather, you should use 127.0.0.1 explicitly. This is because
`localhost` is usually implemented via the `/etc/hosts` file and
nginx's resolver does not support the local `/etc/hosts` file yet.

Regards,
Yichun

Vivek Wiki

unread,
Jan 4, 2018, 4:01:37 AM1/4/18
to openresty-en
@agentzh, I'm using 127.0.0.1 but still I'm getting the same error.



 
Regards,
Yichun

FQ Liu

unread,
Jan 8, 2018, 11:16:06 PM1/8/18
to openresty-en
1. 最好通过redis:new()生成新的实例再调用。
2. 注意redis:connect和redis.connect的区别

在 2018年1月4日星期四 UTC+8下午5:01:37,Vivek Wiki写道:
Reply all
Reply to author
Forward
0 new messages