Nginx Docker AWS, Nginx 无法在多容器中解析 127.0.0.11

我正在从事的工作:

使用 Docker-compose 部署 Nginx- Openresty 与 Meached 服务。从 Nginx 到 Meached 容器成功连接,并指定 resolver = 127.0.0.11,使用 Docker-compose 也能正常工作。但是在 AWS 多容器 Beanstalk 部署时出现超时错误:

failed to connect: memcache could not be resolved (110: Operation timed out)

但是,我可以从 Nginx 容器中 ping 通 Memcached。以下是 NGINX.conf 文件:

location /health-check {
  resolver 127.0.0.11 ipv6=off;
  access_by_lua_block {

      local memcached = require "resty.memcached"
      local memc, err = memcached:new()
      if not memc then
          ngx.say("failed to instantiate memc: ", err)
          return
      end

      memc: set_timeout(1000) -- 1 sec

      local ok, err = memc:connect("memcache", 11211)
      if not ok then
          ngx.say("failed to connect: ", err)
          return
      end

以下是 DOCKER-COMPOSE.YML 文件:

    version: "3"

services:

  memcache:
    image: memcached:alpine
    container_name: memcached
    ports:
      - "11211:11211"
    expose:
      - "11211"
    networks:
      - default

  nginx:
    image: openresty/openresty:alpine
    container_name: nginx
    volumes:
      # Nginx files
      - ./nginx/:/etc/nginx/:ro
      # Web files
      - ./web/:/var/www/web/:ro
    entrypoint: openresty -c /etc/nginx/nginx.conf
    ports:
      - "8080:8080"
    networks:
      - default

以下是 DOCKERRUN.AWS.JSON 文件:

{
  "AWSEBDockerrunVersion": 2,

  "volumes": [
    {
      "name": "current-nginx",
      "host": {
        "sourcePath": "/var/app/current/nginx"
      }
    },
    {
      "name": "web",
      "host": {
        "sourcePath": "/var/www/web/"
      }
    }
  ],
  "containerDefinitions": [
    {
      "name": "memcache",
      "image": "memcached:alpine",
      "essential": true,
      "memory": 1000,
      "portMappings": [
        {
          "hostPort": 11211,
          "containerPort": 11211
        }
      ]
    },
    {
      "name": "nginx",
      "image": "openresty/openresty:alpine",
      "essential": true,
      "memory": 1000,
      "entryPoint": [
        "openresty",
        "-c",
        "/etc/nginx/nginx.conf"
      ],
      "links": [
        "memcache"
      ],

      "portMappings": [

        {
          "hostPort": 8080,
          "containerPort": 8080
        },
        {
          "hostPort": 80,
          "containerPort": 8080
        }
      ],
      "mountPoints": [
        {
          "sourceVolume": "web",
          "containerPath": "/var/www/web/",
          "readOnly": false
        },
        {
          "sourceVolume": "current-nginx",
          "containerPath": "/etc/nginx",
          "readOnly": false
        }
      ]
    }
  ]
}
点赞
用户2575224
用户2575224

你有一个打字错误:

memc:connect("memcache", 11211)

应该为

memc:connect("memcached", 11211)

(你漏了一个“d”)。

2018-04-16 13:22:22