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

why Nginx container cannot resolve redis container by name?

我有一个 docker-compose 运行 两个容器,一个是带有 Lua-module 的 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 脚本文件。这是配置文件:

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

并且在 my_redis 文件中,我正在尝试使用 this 库连接到 Redis 容器, 我在下面粘贴我的代码:

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 尝试本地服务名称?

更新 当 id 添加 resolve 127.0.0.11; 标签到我的 Nginx 配置文件时,错误更改为:

failed to connect: connection refused

但我可以从 Nginx 建立到我的 Redis 容器的 telnet 连接。

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

我将 Nginx-Lua 映像从 danday74/nginx-lua 更改为 fabiocicerchia/nginx-lua,并且它就像一个魅力。第一个是在 Nginx 1.16 版本上创建的,最后一个是在 Nginx 1.19 上创建的。