我正在嘗試使用 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 默認.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
uj5u.com熱心網友回復:
我真的什么都沒改變,它奏效了......
我試圖將 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:9000和192.168.80.2:9000并沒有什么改變。
我已經在 docker-composer 檔案中注釋掉了 nginx 服務并將其回傳,我也做了docker system prune -a.
然后我將所有東西都恢復原樣,運行docker-compose up -d --build并開始作業。還是不知道為什么。
uj5u.com熱心網友回復:
不是真正的答案,但評論缺乏我認為的代碼格式。我在同一個容器中運行 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;
}
轉載請註明出處,本文鏈接:https://www.uj5u.com/net/396820.html
