繁体   English   中英

Nginx 到 php-fpm Docker 容器未连接

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

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

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

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 默认.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 的请求。

您需要在 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