【发布时间】: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