nginx + PHP + docker + ubuntu - 502 Bad gateway Linux/Ubuntu



我正在尝试用nginx和php设置docker。如果在docker-compose.yml中,我只添加nginx服务并打开http://localhost:80,我从nginx看到了hello世界。添加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

我什么都没改,它起作用了。。。

我试图将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,它开始工作了。还是不知道为什么。

不是一个真正的答案,但注释缺乏我认为的代码格式。我在同一个容器中运行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;
}

最新更新