skip to Main Content

I’m trying to setup the docker with nginx and php. 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.

  • Tried to connect to nginx container ip = same result.
  • Found out, that PHP-FPM can listen using two method for accepting fastcgi request. using TCP Socket or with Unix Socket. 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?
  • 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 )

nginx docker_access log:

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:

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

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;
    }
}

Folder structure:

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

2

Answers


  1. Chosen as BEST ANSWER

    I literally changed nothing and it worked...

    I tried to change the Dockerfile to

    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.

    I've commented out the nginx service in docker-composer file and returned it back, and also I did the docker system prune -a.

    Then I returned back everything as it was before, runned docker-compose up -d --build and it started to work. Still don't know why.


  2. 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. Looks pretty much like yours, but it has the try_files, and the fastcgi_pass would be different, like you have.

    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;
    
    }
    
    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search