AWS ECS Docker - nginx:[emerg]主机在上游找不到

时间:2017-03-01 21:32:34

标签: php amazon-web-services nginx docker amazon-ecs

我正在尝试使用docker在AWS ECS上运行php + mysql应用程序。 应用程序堆栈由mysql,elk,php-fpm和nginx docker容器组成。容器都在同一个docker机器上运行。当任务启动时,nginx容器退出时出现以下错误: nginx:[emerg] host在/etc/nginx/conf.d/upstream.conf:1,/strong上游“php:9001”中找不到>

Nginx Dockerfile:

FROM alpine:3.4

RUN apk add --update nginx

#Clear Cache and Temp Data
RUN rm -rf /var/cache/apk/* && rm -rf /tmp/*

ADD nginx.conf /etc/nginx/
ADD symfony.conf /etc/nginx/conf.d/

RUN echo "upstream php-upstream { server php:9001; }" > /etc/nginx/conf.d/upstream.conf

RUN adduser -D -g '' -G www-data www-data

CMD ["nginx"]

EXPOSE 80
EXPOSE 443
  

RUN echo“上游php-upstream {服务器php:9001;}”>   /etc/nginx/conf.d/upstream.conf

php:9001部分引用了php容器,该容器在AWS ECS任务定义中定义。

nginx.conf:

user www-data;
worker_processes 4;
pid /run/nginx.pid;

events {
  worker_connections  2048;
  multi_accept on;
  use epoll;
}

http {
  server_tokens off;
  sendfile on;
  tcp_nopush on;
  tcp_nodelay on;
  keepalive_timeout 15;
  types_hash_max_size 2048;
  include /etc/nginx/mime.types;
  default_type application/octet-stream;
  access_log off;
  error_log off;
  gzip on;
  gzip_disable "msie6";
  include /etc/nginx/conf.d/*.conf;
  include /etc/nginx/sites-enabled/*;
  open_file_cache max=100;
}

daemon off;

symfony.conf:

server {
    server_name _;
    root /var/www/symfony/web;

    location / {
        try_files $uri @rewriteapp;
    }

    location @rewriteapp {
        rewrite ^(.*)$ /app.php/$1 last;
    }

    location ~ ^/(app|app_dev|config)\.php(/|$) {
        fastcgi_pass php-upstream;
        fastcgi_split_path_info ^(.+\.php)(/.*)$;
        include fastcgi_params;
        fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
        fastcgi_param HTTPS off;
    }

    error_log /var/log/nginx/symfony_error.log;
    access_log /var/log/nginx/symfony_access.log;
}

/etc/php7/php-fpm.conf:

[symfony]
user = nobody
group = nobody
listen = 0.0.0.0:9001
pm = dynamic
pm.max_children = 1000
pm.start_servers = 1
pm.min_spare_servers = 1
pm.max_spare_servers = 3
env[DB_1_ENV_MYSQL_DATABASE] = $DB_1_ENV_MYSQL_DATABASE
env[DB_1_ENV_MYSQL_USER] = $DB_1_ENV_MYSQL_USER
env[DB_1_ENV_MYSQL_PASSWORD] = $DB_1_ENV_MYSQL_PASSWORD
catch_workers_output = yes

我还尝试使用对nginx配置的相应更改来更改对listen = /var/run/php-fpm/php-fpm.socklisten = 127.0.0.1:9001的监听,但这导致了各种其他错误消息。

2 个答案:

答案 0 :(得分:2)

我没有在php和nginx容器之间的AWS ECS任务定义中设置链接。

要设置容器的链接,请转到“网络设置”,然后在“链接”字段中输入链接。

答案 1 :(得分:1)

@OnurDemir正确地说,当网络模式为links-awsvpc时,您不能使用"networkMode": "awsvpc",

在这种网络模式下,您可以使用容器的名称来调用它们。

task-definition.json

{
    "family": "app-backend",
    "containerDefinitions": [
        {
            "name": "nginx",
            "image": "my-url-to-the-docker-image",
            "portMappings": [
                {
                    "containerPort": 80,
                    "hostPort": 80,
                    "protocol": "tcp"
                }
            ],
            "dependsOn": [
                {
                    "containerName": "php-fpm",
                    "condition": "START"
                }
            ],
            "essential": true
        },
        {
            "name": "php-fpm",
            "image": "my-url-to-the-docker-image",
            "portMappings": [
                {
                    "containerPort": 9000
                }
            ],
            "essential": true
        }
    ],
    "executionRoleArn": "ecsTaskExecutionRole",
    "cpu": "1024",
    "memory": "2048",
    "networkMode": "awsvpc"
}

在这里您可以看到:

  1. nginx容器dependsOn php-fpm容器。
  2. 要引用其中的php-fpm容器,请f。例如nginx中的虚拟主机文件,您可以使用其名称php-fpm对其进行调用。

示例1:fastcgi_pass php-fpm:9000;

另一种方法,当使用AWS Fargate时,您可以仅调用回送127.0.0.1localhostDescribed in some AWS blog使用awsvpc时,容器间建立连接,因为即使所有容器都在同一容器中,即使它们是单独的容器也是如此。

示例2:fastcgi_pass 127.0.0.1:9000;