我知道有很多关于502 Bad Gateway的帖子,但我无法解决这个问题。我正在使用Docker Compose为Nginx和PHP-FPM创建单独的容器。
错误我在浏览器中加载PHP文件(HTML文件渲染正常):
tc-web | 2018/01/22 19:22:46 [error] 5#5: *4 connect() failed (111: Connection refused) while connecting to upstream, client: 172.18.0.1, server: localhost, request: "GET /info.php HTTP/1.1", upstream: "fastcgi://172.18.0.2:9000", host: "localhost:8080"
tc-web | 172.18.0.1 - - [22/Jan/2018:19:22:46 +0000] "GET /info.php HTTP/1.1" 502 575 "-" "Mozilla/5.0 (Macintosh; Intel Mac OS X 10_13_2) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/63.0.3239.132 Safari/537.36"
我已经尝试使用Unix套接字等调整各种配置数小时,我仍然使用PHP文件获得502错误。你能发现什么是错的吗?
以下是所有必需的文件..
搬运工-composer.yml:
version: '3'
services:
web:
build:
context: ./docker/nginx
image: tc-web:0.1.0
container_name: tc-web
volumes:
# test files
- ./temp.html:/var/www/html/index.html
- ./temp.php:/var/www/html/info.php
ports:
- 8080:80
depends_on:
- php-fpm
php-fpm:
build:
context: ./docker/php-fpm
image: tc-php:0.1.0
container_name: tc-php
volumes:
- ./temp.html:/var/www/html/index.html
- ./temp.php:/var/www/html/info.php
搬运工/ nginx的/ Dockerfile:
FROM nginx:1.13.8
# Install programs
RUN apt-get update
RUN apt-get install -y nano && \
apt-get install -y git && \
apt-get install -y procps
RUN mkdir -p /var/www/html
COPY nginx.conf /etc/nginx/nginx.conf
COPY default.conf /etc/nginx/conf.d/default.conf
搬运工/ nginx的/ nginx.conf:
user www-data;
worker_processes 1;
# error_log /var/log/nginx/error.log warn;
pid /var/run/nginx.pid;
events {
worker_connections 1024;
}
http {
include /etc/nginx/mime.types;
default_type application/octet-stream;
log_format main '$remote_addr - $remote_user [$time_local] "$request" '
'$status $body_bytes_sent "$http_referer" '
'"$http_user_agent" "$http_x_forwarded_for"';
sendfile on;
#tcp_nopush on;
keepalive_timeout 65;
#gzip on;
include /etc/nginx/conf.d/default.conf;
}
搬运工/ nginx的/ default.conf:
server {
listen 80;
server_name localhost;
root /var/www/html;
location / {
try_files $uri $uri/ /index.php?$query_string;
}
location ~* \.php$ {
fastcgi_split_path_info ^(.+\.php)(/.+)$;
fastcgi_pass php-fpm:9000;
fastcgi_index index.php;
fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
include fastcgi_params;
}
}
搬运工/ PHP-FPM / Dockerfile:
FROM php:7.0-fpm
# Install programs
RUN apt-get update
RUN apt-get install -y nano && \
apt-get install -y procps
RUN mkdir -p /var/www/html
COPY php-fpm.conf /usr/local/etc/php-fpm.conf
COPY www.conf /usr/local/etc/php-fpm.d/www.conf
搬运工/ PHP-FPM / PHP-fpm.conf:
[global]
include=etc/php-fpm.d/www.conf
搬运工/ PHP-FPM / www.conf:
[global]
;daemonize = no
; if we send this to /proc/self/fd/1, it never appears
error_log = /proc/self/fd/2
[www]
user = www-data
group = www-data
listen = 127.0.0.1:9000
;listen = /var/run/php-fpm/php7-fpm.sock
;listen.owner = www-data
;listen.group = www-data
;listen.mode = 0660
access.log = /proc/self/fd/2
pm = dynamic
pm.max_children = 5
pm.start_servers = 2
pm.min_spare_servers = 1
pm.max_spare_servers = 3
; Ensure worker stdout and stderr are sent to the main error log.
catch_workers_output = yes
答案 0 :(得分:3)
问题可能在www.conf文件中。 您正在收听127.0.0.1:9000,但这种方式无法在容器外部访问。
尝试绑定到0.0.0.0:9000:
listen = 0.0.0.0:9000
答案 1 :(得分:0)
这听起来似乎很明显,但是另一个常见的问题是未安装php-fpm。即使systemctl
可能会说该服务正在运行,但实际上并没有运行任何东西,也会发生这种情况。就我而言,我还用自己的fpm软件包安装了PHP 8,因此,如果您安装了多个php,则可能与您安装的fpm版本混淆了。