简体   繁体   English

nginx + PHP + docker + Z1D41C853AF58D3A7AE54990CE29417DZ1 坏Linux网关

[英]nginx + PHP + docker + ubuntu - 502 Bad gateway Linux/Ubuntu 21

I'm trying to setup the docker with nginx and php.我正在尝试使用 nginx 和 php 设置 docker。 If in docker-compose.yml I add only the nginx service and open the http://localhost:80, I see the hello-world from nginx. If in docker-compose.yml I add only the nginx service and open the http://localhost:80, I see the hello-world from nginx. After adding the php service, I always get 502 gateway timeout.添加 php 服务后,我总是得到 502 网关超时。

  • Tried to connect to nginx container ip = same result.试图连接到 nginx 容器 ip = 相同的结果。
  • Found out, that PHP-FPM can listen using two method for accepting fastcgi request.发现,PHP-FPM 可以使用两种方法来监听 fastcgi 请求。 using TCP Socket or with Unix Socket.使用 TCP 插座或 Unix 插座。 This configuration can be found at /etc/php/7.4/fpm/pool.d/www.conf , but in php74 container I dont have path /etc/php/ - maybe this is the problem?此配置可以在/etc/php/7.4/fpm/pool.d/www.conf找到,但是在 php74 容器中我没有路径/etc/php/ - 也许这是问题所在?
  • Tried to use some repos from github, none worked for me - had the same result, except one for symfony which uses the caddy image ( https://github.com/dunglas/symfony-docker )尝试使用来自 github 的一些 repos,没有一个对我有用 - 结果相同,除了一个使用caddy图像的symfony

nginx docker_access log: 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 log: 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"

My docker-compose.yml我的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: 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 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;
    }
}

Folder structure:文件夹结构:

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

Not really an answer, but the comments lacks the nice formatting I think for code.不是真正的答案,但评论缺乏我认为的代码格式。 I am running php and nginx in the same container and my nginx.conf has the following for the location block.我在同一个容器中运行 php 和 nginx 并且我的 nginx.conf 具有以下位置块。 Looks pretty much like yours, but it has the try_files, and the fastcgi_pass would be different, like you have.看起来很像你的,但它有 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;

}

I literally changed nothing and it worked...我从字面上没有改变任何东西,它的工作...

I tried to change the Dockerfile to我试图将 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

and nothing changed.什么都没有改变。

I also tried to change the fastcgi_pass to 127.0.0.1:9000 and 192.168.80.2:9000 and nothing changed.我还尝试将 fastcgi_pass 更改为127.0.0.1:9000192.168.80.2:9000并且没有任何改变。

I've commented out the nginx service in docker-composer file and returned it back, and also I did the docker system prune -a .我已经在 docker-composer 文件中注释掉了 nginx 服务并将其返回,并且我还做了docker system prune -a

Then I returned back everything as it was before, runned docker-compose up -d --build and it started to work.然后我把所有东西都恢复原样, docker-compose up -d --build并开始工作。 Still don't know why.还是不知道为什么。

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

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