Điểm:0

Docker - Bad Gateway when attempting to proxy nginx container to laravel php-fpm container

lá cờ cn

I'm trying to work out whats causing a 502.

I have a simple application with two containers:

web - running nginx, with a proxy to app: fastcgi_pass app:9000;

app - running php-fpm. I'm not sure how I can pass requests from web but on app/locally if I run php artisan up I get a response of Application is already up.

I can also ping app from web apt-get update && apt-get install iputils-ping -y && ping app

If I attempt to visit artery.local in a browser I can see web in the docker logs write

web | 2021/11/29 21:55:33 [error] 31#31: *9 recv() failed (104: Connection reset by peer) while reading response header from upstream, client: 172.18.0.1, server: artery.local, request: "GET /admin/home HTTP/1.1", upstream: "fastcgi://172.18.0.2:9000", host: "artery.local"

Please can someone help me shed some light on this. I'm not sure what I can do to test that app/php-fpm is listening correctly to requests forwared by web/nginx.

docker-compose.yml

version: '3'
services:

  # The app
  app:
    build:
      context: ./
      dockerfile: ./app-dev.dockerfile
    working_dir: /var/www
    container_name: app
    volumes:
      - ../:/var/www
#    ports:
#     - 9000:9000
    environment:
      - COMPOSER_MEMORY_LIMIT=-1
    networks:
      - app-network

  # A Web Server
  web:
    build:
      context: ./
      dockerfile: ./web.dockerfile
    container_name: web
    working_dir: /var/www
    volumes:
      - ../:/var/www
    ports:
      - 80:80
    networks:
      - app-network

#Docker Networks
networks:
  app-network:
    driver: bridge

app-dev.dockerfile

FROM php:7.4-fpm

RUN set -eux; \
    apt-get update; \
    apt-get upgrade -y; \
    apt-get install -y --no-install-recommends \
            curl \
            libmemcached-dev \
            libz-dev \
            libpq-dev \
            libjpeg-dev \
            libpng-dev \
            libfreetype6-dev \
            libssl-dev \
            libwebp-dev \
            libmcrypt-dev \
            libonig-dev; \
    rm -rf /var/lib/apt/lists/*

RUN apt-get update && apt-get install -y zlib1g-dev libicu-dev g++ libgmp-dev libpng-dev pdftk graphviz
RUN docker-php-ext-install pcntl
RUN docker-php-ext-install sockets
RUN apt-get install git -y
RUN apt-get install libpcre++-dev -y
RUN pecl install oauth
RUN docker-php-ext-enable oauth

#mysql & gd 
RUN set -eux; \
    # Install the PHP pdo_mysql extention
    docker-php-ext-install pdo_mysql; \
    # Install the PHP gd library
    docker-php-ext-configure gd \
            --prefix=/usr \
            --with-jpeg \
            --with-webp \
            --with-freetype; \
    docker-php-ext-install gd; \
    php -r 'var_dump(gd_info());'

RUN apt-get update \
     && apt-get install -y libzip-dev \
     && docker-php-ext-install zip

# Curl
RUN apt-get install libcurl4-gnutls-dev -y
RUN apt-get update && apt-get install -y libmagickwand-dev --no-install-recommends && rm -rf /var/lib/apt/lists/*
RUN printf "\n" | pecl install imagick
RUN docker-php-ext-enable imagick
RUN pear install PHP_CodeSniffer

# PHP REDIS EXTENSION
RUN printf "\n" | pecl install -o -f redis \
    &&  rm -rf /tmp/pear \
    &&  docker-php-ext-enable redis

# Install XDebug
RUN pecl install xdebug && docker-php-ext-enable xdebug
COPY ./xdebug.ini /usr/local/etc/php/conf.d/xdebug.ini

COPY laravel.ini /etc/nginx/conf.d/laravel.conf
COPY ./xlaravel.pool.conf /etc/nginx/conf.d/laravel-pool.conf

COPY laravel.ini /usr/local/etc/php/conf.d
COPY ./xlaravel.pool.conf /usr/local/etc/php-fpm.d/

RUN apt-get update && apt-get install iputils-ping -y

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

EXPOSE 9000
CMD ["php-fpm"]

web.dockerfile

FROM nginx:latest

ADD vhost.conf /etc/nginx/conf.d/default.conf

# forward request and error logs to docker log collector
RUN ln -sf /dev/stdout /var/log/nginx/access.log \
    && ln -sf /dev/stderr /var/log/nginx/error.log

laravel.pool.conf

user = www-data
group = www-data
listen = 0.0.0.0:9000
pm = dynamic
pm.max_children = 20
pm.start_servers = 2
pm.min_spare_servers = 1
pm.max_spare_servers = 3
catch_workers_output = yes
request_terminate_timeout = 600

vhost.conf loaded from web.dockerfile

server {
    listen 80;
    listen [::]:80;

    server_name artery.local;

    root /var/www/public;

    # avoid 413 errors when uploading files up to 5M in size
    client_max_body_size 10M;

    add_header X-Frame-Options "SAMEORIGIN";
    add_header X-XSS-Protection "1; mode=block";
    add_header X-Content-Type-Options "nosniff";

    index index.html index.htm index.php;

    charset utf-8;

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

    location = /favicon.ico { access_log off; log_not_found off; }
    location = /robots.txt  { access_log off; log_not_found off; }

    access_log /var/log/nginx/access.log;
    error_log  /var/log/nginx/error.log;

    error_page 404 /index.php;

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

    location ~ /\.(?!well-known).* {
        deny all;
    }
}

Đăng câu trả lời

Hầu hết mọi người không hiểu rằng việc đặt nhiều câu hỏi sẽ mở ra cơ hội học hỏi và cải thiện mối quan hệ giữa các cá nhân. Ví dụ, trong các nghiên cứu của Alison, mặc dù mọi người có thể nhớ chính xác có bao nhiêu câu hỏi đã được đặt ra trong các cuộc trò chuyện của họ, nhưng họ không trực giác nhận ra mối liên hệ giữa câu hỏi và sự yêu thích. Qua bốn nghiên cứu, trong đó những người tham gia tự tham gia vào các cuộc trò chuyện hoặc đọc bản ghi lại các cuộc trò chuyện của người khác, mọi người có xu hướng không nhận ra rằng việc đặt câu hỏi sẽ ảnh hưởng—hoặc đã ảnh hưởng—mức độ thân thiện giữa những người đối thoại.