为什么 Nginx 容器无法通过名称解析 Redis 容器?

我有一个 docker-compose 文件来运行两个容器,一个是带有 Lua 模块的 Nginx,另一个是简单的 Redis,以下是 docker-compose 文件:

version: '3.9'
services:
  nginx:
    container_name: local_nginx
    image: danday74/nginx-lua
    ports:
      - 8089:80
      - 8053:8053
    volumes:
      - /home/navid/lua_project/nginx/nginx.conf:/nginx/conf/nginx.conf
      - /home/navid/lua_project/nginx/www:/nginx/html
      - /home/navid/lua_project/lib/resty/redis.lua:/usr/local/share/luajit-2.0.4/resty/redis.lua
  redis:
    container_name: local_redis
    image: redis:latest
    ports:
      - 6379:6379

在我的 nginx.conf 文件中,我定义了一个路由来执行一个 Lua 脚本文件。这是 conf 文件:

events {}
http {
    server {
        server_name lua;
        listen 8053;
        location /redis-request {
            content_by_lua_file /nginx/html/my_redis.lua;
        }
    }
}

在我的 _redis 文件中,我尝试使用此库连接到 Redis 容器:https://github.com/openresty/lua-resty-redis#connect,我在下面粘贴我的代码:

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

    -- 1 sec
    red:set_timeout(1000)

    -- or connect to a unix domain socket file listened
    -- by a redis server:
    -- local ok, err = red:connect("unix:/path/to/redis.sock")
    local ok, err = red:connect("local_redis.lua_project_default", 6379)
    if not ok then
        ngx.say("failed to connect: ", err)
        return
    end

但是当我尝试通过浏览器向 Nginx 发送请求时,我收到了这样的错误:

failed to connect: no resolver defined to resolve "local_redis.lua_project_default"

我该如何防止 Nginx 尝试使用本地服务名称?

更新 当我在我的 Nginx 配置文件中添加 resolve 127.0.0.11; 标签后,错误变为:

failed to connect: connection refused

但我可以从 Nginx 对 Redis 容器进行 telnet 连接。

点赞
用户1093228
用户1093228

我找到了解决方案,但我没有找到问题的根本原因:

我将我的Nginx-Lua镜像从_danday74/nginx-lua_更改为fabiocicerchia/nginx-lua,然后它像魔法一样运行了。第一个是在Nginx版本1.16上创建的,而最后一个是由Nginx 1.19创建的。

2021-05-02 12:26:37