openresty / lua-resty-redis

Lua redis client driver for the ngx_lua based on the cosocket API
1.91k stars 448 forks source link

Connect failed when using hostname instead of IP address #240

Closed czf0613 closed 2 years ago

czf0613 commented 2 years ago

I see the document says you can connect to redis using "red:connect("127.0.0.1", 6379)", but I found something wrong when I use the host name instead of IP address. For example, I bought the redis server from the cloud service, they provided me a connection address "my.redis.aliyuncs.com". But it has an error when I use "red:connect("my.redis.aliyuncs.com", 6379)". Is there something wrong with DNS? Or, I just made a mistake? I am pretty sure that I can connect successfully by using other redis desktop manager.

I am looking forward to your reply. Thanks sincerely!

zhuizhuhaomeng commented 2 years ago

is there any error log?

czf0613 commented 2 years ago

No any, it just shows that failed to connect to "my.redis.aliyuncs.com". It's quite weird.

zhuizhuhaomeng commented 2 years ago

can you post your nginx.conf, so we can reproduce this problem

czf0613 commented 2 years ago

Sure, please view the nginx configuration file in: https://czf-net.xyz/res/nginx.conf The lua script is also included at: https://czf-net.xyz/res/access_control.lua Thanks for your great help!

ryanwohara commented 2 years ago

I am facing this error while using Kong. Is there any additional information I would be able to provide?

zhuizhuhaomeng commented 2 years ago

@czf0613 you need to specify a resolver. eg resolver 4.4.4.4 ipv6=off;

zhuizhuhaomeng commented 2 years ago

http://nginx.org/en/docs/http/ngx_http_core_module.html#resolver

czf0613 commented 2 years ago

It does not work at all... But I tried another case, I add a TCP proxy in openresty like this:

stream { server { listen 30003; proxy_pass r-8vbiib2him1kzqtbhf.redis.zhangbei.rds.aliyuncs.com:6379; } }

This piece of code works no matter whether you specify the resolver or not, which means the default DNS resolver in my server is correct. I think it may still has some bugs in the lua-resty-redis. Thanks for your regard.

@czf0613 you need to specify a resolver. eg resolver 4.4.4.4 ipv6=off;

ryanwohara commented 2 years ago

I agree with @czf0613 that there is a DNS bug here. My resolver is declared properly in /etc/resolv.conf - I shouldn't need to specify it elsewhere.

zhuizhuhaomeng commented 2 years ago

modified the lua code, and got the following error 2021/12/26 18:46:24 [error] 986#986: *6 [lua] access_control.lua:14: redis connection failed!no resolver defined to resolve "r-8vbiib2him1kzqtbhf.redis.zhangbei.rds.aliyuncs.com", client: 127.0.0.1, server: pic-bed.xyz, request: "GET /service HTTP/1.1", host: "127.0.0.1"

local redis_pack = require "resty.redis"
local redis = redis_pack:new()

redis:set_timeouts(1000, 1000, 1000)

-- writing connection address in this way is OK
-- local ok, err = redis:connect("10.0.0.205", 6379)

-- but failed in this way, I was wondering if there is any problem in DNS? 
local ok, err = redis:connect("r-8vbiib2him1kzqtbhf.redis.zhangbei.rds.aliyuncs.com", 6379)

if not ok then
    ngx.log(ngx.ERR, "redis connection failed!", err)
    ngx.exit(502)
    return
end
czf0613 commented 2 years ago

It still shows that 'no resolver defined to resolve "r-8vbiib2him1kzqtbhf.redis.zhangbei.rds.aliyuncs.com", client: 58.63.62.105, server: , request: "GET /res HTTP/1.1"'

ryanwohara commented 2 years ago

@czf0613 Did you find a solution? Why did you close this?

czf0613 commented 2 years ago

Quite weird, I asked my cloud service provider and they gave me a DNS server. I configured my docker DNS resolver, and, it works. I cannot understand why...... I believe that it must be some problems in DNS, maybe some bugs in nginx, not the lua-resty-redis.

ryanwohara commented 2 years ago

Thanks for the insight, appreciated

Edit: I seem to have fixed my issue by upgrading Kong from 2.6.0 to 2.7.0.