Nginx到php-fpm Docker容器未连接

时间:2020-09-19 02:13:35

标签: php docker nginx

我确定我错过了一些简单的事情或正在做一些愚蠢的事情,但是对于我一生来说,我看不到它是什么。

我有一个通过Cloudformation部署到AWS Fargate的Dockerized Laravel应用程序。 Nginx容器和php-fpm容器之间的连接不起作用。尝试仅通过404s进入php-fpm容器中的health-check.php文件。我通过旋转两个容器并像这样链接它们在本地复制了此信息:

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

进入每个容器,我可以验证所有文件和目录结构是否正确。尝试点击http://127.0.0.1:8000/health-check.php上的health-check.php文件将返回404。

这是文件。

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"]

有人知道我在做什么错吗?

更新的信息

我发现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

我发现了一些有关此的内容,但是请注意,如果我只是重新启动nginx,则不会出错。如果这是一条红色鲱鱼,就无法解决。

更新2

我设法让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"

我已经检查了php-fpm容器中的Docker容器,它具有zz-docker.conf文件的标准设置,并带有

[global]
daemonize = no

[www]
listen = 9000

我甚至都尝试过向其中添加listen.allowed_clients = 127.0.0.1,但是没有运气。无法看到为什么php-fpm会拒绝连接?

更新3

神奇的是,这现在可以在AWS ECS / Fargate环境中使用。仍然不在本地。 PHP拒绝了Nginx的请求。

1 个答案:

答案 0 :(得分:0)

您需要在nginx配置(default.conf)中更新fastcgi_pass,以使其连接到php docker服务而不是本地localhost:

fastcgi_pass php:9000;