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

4

我正在处理的项目:

使用nginx-openresty、memcached和docker-compose。

我能够通过在nginx中指定resolver = 127.0.0.11来连接memcached容器,在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
        }
      ]
    }
  ]
}
1个回答

1
您打错了一个字: memc:connect("memcache", 11211) 应该是 memc:connect("memcached", 11211) (您漏掉了一个“d”)。

已更正的 .json 文件。在此添加时出现了错误。 - sandeep
@sandeep,你也遇到了同样的错误吗?“连接失败:无法解析memcache(110:操作超时)”。 - Constantin Galbenu
你可以重新构建图像。 - Constantin Galbenu

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接