openresty / lua-resty-redis

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

Redis failed to connect: connection refused #178

Open Iliyass opened 5 years ago

Iliyass commented 5 years ago

I'm struggling with this issue more than a week, and still cannot pull it off. I'm trying to provision our environment using Ansible, and I want to provision a staging server, with the same environment as production, I have setup the Redis server, and it's running listening on 6379 I have nginx up and running and it's serving requests, but when it's got to the part of lua to connect to redis, it throw on me connection refused error. Here is nginx debug log: Link

Redis Listening on 6379

$ sudo lsof -i -P -n | grep LISTEN | grep 6379
redis-ser  1978         redis    4u  IPv6 138447828      0t0  TCP *:6379 (LISTEN)
redis-ser  1978         redis    5u  IPv4 138447829      0t0  TCP *:6379 (LISTEN)

Connecting to Redis through Python

Python 2.7.12 (default, Oct  8 2019, 14:14:10) 
[GCC 5.4.0 20160609] on linux2
Type "help", "copyright", "credits" or "license" for more information.
import redis
redis.Redis(host='127.0.0.1', port=6379, db='0')
r.set("Test", 'value')
True
r.get("Test")
'value'

Lua Code:

local red = redis:new()
red:set_timeout(500)
local ok, err = red:connect("127.0.0.1", 6379)
if not ok then
     ngx.say("Redis failed to connect: ", err)
     return
end

Nginx conf:

server {
    listen 8080;
    server_name xxx.com;
    access_log /var/log/nginx/xxxx_access.log;
    error_log /var/log/nginx/xxxx_error.log debug;

    proxy_redirect     off;
    proxy_set_header Host   $host;
    proxy_set_header REMOTE_ADDR $http_cf_connecting_ip;
    proxy_set_header X-Real-IP  $http_cf_connecting_ip;
    proxy_set_header X-URI  $uri;
    proxy_set_header X-Scheme $scheme;
    proxy_set_header X-Forwarded-Protocol $scheme;

    location / {
        rewrite_by_lua_file '/var/www/xxxx/nginx/add_header_web.lua';
        proxy_pass http://xxxxx/;
    }
}

Environment

Redis 3.2.0
Nginx: openresty/1.7.7.2
configure arguments: --prefix=/usr/local/openresty/nginx --with-debug --with-cc-opt='-I/opt/ngx_openresty-1.7.7.2/build/luajit-root/usr/local/openresty/luajit/include/luajit-2.1 -DNGX_LUA_USE_ASSERT -DNGX_LUA_ABORT_AT_PANIC -O2 -O2' --add-module=../ngx_devel_kit-0.2.19 --add-module=../echo-nginx-module-0.57 --add-module=../xss-nginx-module-0.04 --add-module=../ngx_coolkit-0.2rc2 --add-module=../set-misc-nginx-module-0.28 --add-module=../form-input-nginx-module-0.10 --add-module=../encrypted-session-nginx-module-0.03 --add-module=../srcache-nginx-module-0.28 --add-module=../ngx_lua-0.9.14 --add-module=../ngx_lua_upstream-0.02 --add-module=../headers-more-nginx-module-0.25 --add-module=../array-var-nginx-module-0.03 --add-module=../memc-nginx-module-0.15 --add-module=../redis2-nginx-module-0.11 --add-module=../redis-nginx-module-0.3.7 --add-module=../rds-json-nginx-module-0.13 --add-module=../rds-csv-nginx-module-0.05 --with-ld-opt='-Wl,-rpath,/usr/local/openresty/luajit/lib -L/opt/ngx_openresty-1.7.7.2/build/luajit-root/usr/local/openresty/luajit/lib -Wl,-rpath,/usr/local/lib' --conf-path=/etc/nginx/nginx.conf --with-http_realip_module --with-http_stub_status_module --with-http_geoip_module --with-http_ssl_module --with-http_sub_module --add-module=/opt/nginxmodules/nginx-upload-progress-module --add-module=/opt/nginxmodules/nginx-push-stream-module
platput commented 4 years ago

Did you figure it out? I am having the same issue.

platput commented 4 years ago

Ok I fixed it by adding the options table and pool name. Here is what I did:

local options_table = {}
options_table["pool"] = "docker_server"
local ok, err = red:connect("10.211.55.8", 6379, options_table)
kamly commented 4 years ago

local options_table = {} options_table["pool"] = "docker_server"

i meet same issue

it does't work that adding the options table and pool name.

@Iliyass do you konw how to fix it ?

kamly commented 4 years ago

local options_table = {} options_table["pool"] = "docker_server"

i meet same issue

it does't work that adding the options table and pool name.

@Iliyass do you konw how to fix it ?

I know the reason, check the Nginx version

learngogo123 commented 3 years ago

Ok I fixed it by adding the options table and pool name. Here is what I did:

local options_table = {}
options_table["pool"] = "docker_server"
local ok, err = red:connect("10.211.55.8", 6379, options_table)

thank u for solution my problem.it is ok