简体   繁体   English

Nginx 到 php-fpm Docker 容器未连接

[英]Nginx to php-fpm Docker containers not connecting

I'm sure I'm missing something simple or am doing something stupid but for the life of me I can't see what it is.我确定我错过了一些简单的事情或者正在做一些愚蠢的事情,但对于我的生活,我看不到它是什么。

I have a Dockerised Laravel application deploying via Cloudformation to AWS Fargate.我有一个通过 Cloudformation 部署到 AWS Fargate 的 Dockerised Laravel 应用程序。 The connection between the Nginx container and the php-fpm container isn't working. Nginx 容器和 php-fpm 容器之间的连接不起作用。 Trying to get to the health-check.php file in the php-fpm container just 404s.尝试访问 php-fpm 容器中的 health-check.php 文件仅 404 秒。 I have replicated this locally by spinning up the two containers and linking them like this:我通过旋转两个容器并像这样链接它们在本地复制了这个:

docker build -t nginx-aws-image -f docker/nginx/aws/Dockerfile .
docker build -t php-fpm-aws-image -f docker/php-fpm/aws/Dockerfile .
docker run --publish 9000:9000 --name php php-fpm-aws-image
docker run --link php:php --publish 8000:80 --name nginx nginx-aws-image

Going into each container I can validate all the files and directory structures are as they should.进入每个容器后,我可以验证所有文件和目录结构是否符合预期。 Trying to hit the health-check.php file at http://127.0.0.1:8000/health-check.php returns a 404.尝试点击位于http://127.0.0.1:8000/health-check.php的 health-check.php 文件会返回 404。

Here are the files.这是文件。

Nginx Dockerfile: Nginx Dockerfile:

FROM nginx:1.17

### Install curl for health check
RUN apt-get update && apt-get install --no-install-recommends --no-install-suggests -y curl

### Configure NGINX
COPY docker/nginx/default.conf /etc/nginx/conf.d/default.conf

WORKDIR /var/www/public

HEALTHCHECK --interval=15s --timeout=10s --start-period=60s --retries=2 CMD curl -f http://127.0.0.1/health-check.php || exit 1

Nginx default.conf: Nginx 默认.conf:

server {
    listen 80;
    index index.php index.html;
    error_log  /var/log/nginx/error.log debug;
    access_log /var/log/nginx/access.log;
    root /var/www/public;
    location ~ \.php$ {
        try_files $uri =404;
        fastcgi_split_path_info ^(.+\.php)(/.+)$;
        fastcgi_pass 120.0.0.1:9000;
        error_log  /var/log/nginx/error.log debug;
        access_log /var/log/nginx/access.log;
        fastcgi_index index.php;
        include fastcgi_params;
        fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
        fastcgi_param PATH_INFO $fastcgi_path_info;
    }
    location / {
        try_files $uri $uri/ /index.php?$query_string;
        gzip_static on;
    }
}

Php-fpm Dockerfile: php-fpm Dockerfile:

FROM php:7.2-fpm

# Install system dependencies
RUN apt-get update && apt-get install -y \
    git \
    curl \
    libpng-dev \
    libonig-dev \
    libxml2-dev \
    zip \
    unzip

# Clear cache
RUN apt-get clean && rm -rf /var/lib/apt/lists/*

RUN export CFLAGS="$PHP_CFLAGS" CPPFLAGS="$PHP_CPPFLAGS" LDFLAGS="$PHP_LDFLAGS" \
    && apt-get update \
    && apt-get install -y --no-install-recommends \
        libmagickwand-dev \
    && rm -rf /var/lib/apt/lists/* \
    && pecl install imagick-3.4.4 \
    && docker-php-ext-enable imagick

# Install PHP extensions
RUN docker-php-ext-install pdo_mysql mbstring exif pcntl bcmath gd zip

# Get latest Composer
COPY --from=composer:latest /usr/bin/composer /usr/bin/composer

RUN mkdir -p /home/www-data/.composer

COPY --chown=www-data:www-data . /var/www/

# Set working directory
WORKDIR /var/www

HEALTHCHECK --interval=15s --timeout=10s --start-period=60s --retries=2 CMD curl -f http://127.0.0.1/health-check.php || exit 1

# Start PHP FPM
CMD ["php-fpm"]

Anyone see what I'm doing wrong?有人看到我做错了什么吗?

UPDATED information更新信息

I have discovered that the default.conf file fails the nginx -t -c conf.d/default.conf test with:我发现 default.conf 文件无法通过nginx -t -c conf.d/default.conf测试:

nginx: [emerg] "server" directive is not allowed here in /etc/nginx/conf.d/default.conf:1
nginx: configuration file /etc/nginx/conf.d/default.conf test failed

I've found several things about this, but note that if I just restart nginx then I get no errors.我发现了一些关于此的事情,但请注意,如果我只是重新启动 nginx,则不会出现任何错误。 Can't work out if this is a red herring.无法确定这是不是红鲱鱼。

UPDATE 2更新 2

I have managed to get nginx to output this log error我已经设法让 nginx 输出这个日志错误

2020/09/20 03:13:34 [error] 7#7: *7 connect() failed (111: Connection refused) while connecting to upstream, client: 127.0.0.1, server: , request: "GET /health-check.php HTTP/1.1", upstream: "fastcgi://127.0.0.1:9000", host: "127.0.0.1"

I have checked the Docker container in the php-fpm container and it is has the standard set up of the zz-docker.conf file with the我已经检查了 php-fpm 容器中的 Docker 容器,它具有zz-docker.conf文件的标准设置

[global]
daemonize = no

[www]
listen = 9000

I even tried add listen.allowed_clients = 127.0.0.1 to it too with no luck.我什至尝试添加listen.allowed_clients = 127.0.0.1也没有运气。 Can't see why php-fpm would be refusing the connection??不明白为什么 php-fpm 会拒绝连接??

Update 3更新 3

Magically this is working in AWS ECS/Fargate environment now.神奇的是,这现在在 AWS ECS/Fargate 环境中有效。 Still not locally though.不过本地还是不行。 PHP is refusing the request from Nginx. PHP 拒绝来自 Nginx 的请求。

您需要在 nginx 配置(default.conf)中更新您的fastcgi_pass ,以便它连接到 php docker 服务而不是您的本地本地主机:

fastcgi_pass php:9000;

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM