简体   繁体   中英

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. The connection between the Nginx container and the php-fpm container isn't working. Trying to get to the health-check.php file in the php-fpm container just 404s. 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.

Here are the files.

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:

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:

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:

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. Can't work out if this is a red herring.

UPDATE 2

I have managed to get nginx to output this log error

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

[global]
daemonize = no

[www]
listen = 9000

I even tried add listen.allowed_clients = 127.0.0.1 to it too with no luck. Can't see why php-fpm would be refusing the connection??

Update 3

Magically this is working in AWS ECS/Fargate environment now. Still not locally though. PHP is refusing the request from Nginx.

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

fastcgi_pass php:9000;

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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