【问题标题】:Nginx+PHP-FPM: connection refused while connecting to upstream (502)Nginx + PHP-FPM:连接到上游时连接被拒绝(502)
【发布时间】:2020-12-10 04:01:53
【问题描述】:

我知道有大量关于 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 错误。你能看出哪里不对吗?

这里是所有必需的文件..

docker-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

docker/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

docker/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;
}

docker/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;
    }
}

docker/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

docker/php-fpm/php-fpm.conf:

[global]

include=etc/php-fpm.d/www.conf

docker/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

【问题讨论】:

    标签: php docker nginx docker-compose


    【解决方案1】:

    问题可能出在www.conf 文件中。 您正在收听127.0.0.1:9000,但这样服务将无法在容器外访问。

    尝试绑定到0.0.0.0:9000:

    listen = 0.0.0.0:9000
    

    【讨论】:

    • 那么,我在这里指定0.0.0.0 是什么意思?当一台机器需要监听来自另一台机器的请求,但它没有分配特定地址时,这是您使用的通用 IP 吗?
    • 0.0.0.0 是一个特殊地址,意思是:“监听每个网络接口”
    【解决方案2】:

    这听起来很明显,但另一个常见问题是未安装 php-fpm。即使systemctl 可能会说服务正在运行,但实际上并没有运行任何东西,这种情况也可能发生。就我而言,我还安装了 PHP 8 和它自己的 fpm 包,所以如果你有多个 php 安装,可能会与你安装的 fpm 版本混淆。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2020-04-24
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2014-02-26
      • 2012-11-21
      • 2020-10-26
      相关资源
      最近更新 更多