【问题标题】:nginx + PHP + docker + ubuntu - 502 Bad gateway Linux/Ubuntu 21nginx + PHP + docker + ubuntu - 502 Bad gateway Linux/Ubuntu 21
【发布时间】:2022-01-24 13:43:48
【问题描述】:

我正在尝试使用 nginx 和 php 设置 docker。如果在 docker-compose.yml 我只添加 nginx 服务并打开 http://localhost:80,我会看到来自 nginx 的 hello-world。添加php服务后,总是报502网关超时。

  • 尝试连接到 nginx 容器 ip = 相同的结果。
  • 发现,PHP-FPM 可以使用两种方法来监听 fastcgi 请求。使用 TCP 套接字或 Unix 套接字。此配置可以在 /etc/php/7.4/fpm/pool.d/www.conf 找到,但在 php74 容器中我没有路径 /etc/php/ - 也许这是问题所在?
  • 尝试使用来自 github 的一些 repos,没有一个对我有用 - 结果相同,除了一个使用 caddy 图像的 symfony (https://github.com/dunglas/symfony-docker

nginx docker_access 日志:

192.168.80.1 - - [23/Dec/2021:15:20:23 +0000] "GET / HTTP/1.1" 504 167 "-" "Mozilla/5.0 (X11; Linux x86_64; rv:95.0) Gecko/20100101 Firefox/95.0"

Nginx docker_error 日志:

2021/12/23 15:02:53 [error] 31#31: *3 upstream timed out (110: Operation timed out) while connecting to upstream, client: 192.168.80.1, server: , request: "GET / HTTP/1.1", upstream: "fastcgi://192.168.80.2:9000", host: "localhost"

我的 docker-compose.yml

version: '3'

networks:
    nginx-php74-mysql8-node:

services:
    nginx-service:
        image: nginx:alpine
        container_name: nginx-container
        restart: unless-stopped
        tty: true
        ports:
            - "80:80"
            - "443:443"
        volumes:
            - ./apps/docker/:/var/www/docker
            - ./nginx/:/etc/nginx/conf.d/
        depends_on:
            - php74-service
        networks:
            - nginx-php74-mysql8-node

    php74-service:
        build:
            context: .
            dockerfile: ./php/Dockerfile
        container_name: php74-container
        restart: unless-stopped
        tty: true
        ports:
            - "9000:9000"
        volumes:
            - ./apps/docker/:/var/www/docker
        networks:
            - nginx-php74-mysql8-node

PHP Dockerfile:

FROM php:7.4-fpm

RUN apt-get update && apt-get install -y zlib1g-dev g++ git libicu-dev zip libzip-dev zip \
    && docker-php-ext-install intl opcache pdo pdo_mysql \
    && pecl install apcu \
    && docker-php-ext-enable apcu \
    && docker-php-ext-configure zip \
    && docker-php-ext-install zip

WORKDIR /var/www/docker

RUN curl -sS https://getcomposer.org/installer | php -- --install-dir=/usr/local/bin --filename=composer

RUN curl -sS https://get.symfony.com/cli/installer | bash
RUN mv /root/.symfony/bin/symfony /usr/local/bin/symfony

nginx default.conf

server {
    listen 80;
    index index.php index.html;
    error_log /var/log/nginx/docker_error.log;
    access_log /var/log/nginx/docker_access.log;
    root /var/www/docker;

    location ~ \.php$ {
          fastcgi_split_path_info  ^(.+\.php)(/.+)$;
        fastcgi_index            index.php;
        fastcgi_pass php74-service:9000;
        include                  fastcgi_params;
        fastcgi_param   PATH_INFO       $fastcgi_path_info;
        fastcgi_param   SCRIPT_FILENAME $document_root$fastcgi_script_name;
    }

    location / {
        try_files $uri $uri/ /index.php?$query_string;
        gzip_static on;
    }
}

文件夹结构:

- apps
-- docker
--- index.php
--- index.html
- nginx
-- default.conf
- php
-- Dockerfile
- docker-compose.yml

【问题讨论】:

  • 不确定有什么区别,但你有: container_name: php74-container & fastcgi_pass php74-service:9000 ?您是否尝试将其更改为 fastcgi_pass php74-container:9000 ?
  • 据我所知,需要传递服务名称,但我尝试传递容器名称并没有改变
  • 查看php-fpm 日志。
  • 显然 fastcgi://192.168.80.2:9000 没有响应。您是否使用该 IP 地址配置 PHP-FPM 并侦听该端口? PHP 7 也是生命的尽头。为什么要设置它的新安装?

标签: php docker nginx docker-compose


【解决方案1】:

我真的什么都没做,但它起作用了......

我尝试将 Dockerfile 更改为

FROM php:7.4-fpm
RUN apt-get update && apt-get install -y \
        libfreetype6-dev \
        libjpeg62-turbo-dev \
        libpng-dev \
    && docker-php-ext-configure gd --with-freetype --with-jpeg \
    && docker-php-ext-install -j$(nproc) gd

什么都没有改变。

我还尝试将 fastcgi_pass 更改为 127.0.0.1:9000192.168.80.2:9000,但没有任何改变。

我已经在 docker-composer 文件中注释掉了 nginx 服务并将其返回,我还做了docker system prune -a

然后我将所有内容都恢复原样,运行 docker-compose up -d --build 并开始工作。还是不知道为什么。

【讨论】:

    【解决方案2】:

    不是一个真正的答案,但 cmets 缺乏我认为的代码格式。我在同一个容器中运行 php 和 nginx,我的 nginx.conf 具有以下位置块。看起来很像你的,但它有 try_files,而 fastcgi_pass 会有所不同,就像你一样。

    location ~ \.php$ {
    
        try_files $uri = 404;
        fastcgi_split_path_info ^(.+\.php)(/.+)$;
        fastcgi_pass 127.0.0.1:9000;
        fastcgi_index index.php;
        include fastcgi_params;
        fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
        fastcgi_param PATH_INFO $fastcgi_path_info;
    
    }
    

    【讨论】:

    • 不确定您是否希望 php 在主机上监听 9000。我认为最好将 php 处理器与容器隔离。
    • "fastcgi://192.168.80.2:9000" ??那可能无法解决?
    猜你喜欢
    • 2022-08-16
    • 1970-01-01
    • 2017-06-29
    • 2016-09-14
    • 2017-08-20
    • 2022-11-22
    • 1970-01-01
    • 2015-05-25
    • 2020-08-01
    相关资源
    最近更新 更多