Nginx + PHP-FPM:连接到上游时连接被拒绝(502) - php

我知道关于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

泊坞窗/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

参考方案

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

尝试绑定到0.0.0.0:9000

listen = 0.0.0.0:9000

PHP getallheaders替代 - php

我正在尝试从服务器上的apache切换到nginx。唯一的问题是我在PHP脚本中使用的getallheaders()函数,该函数不适用于Nginx。我已经尝试过用户在getallheaders函数上的php站点上提供的注释,但这并不返回所有请求标头。请告诉我如何解决这个问题。我真的想切换到Nginx。 参考方案 您仍然可以使用它,但是您必须像这里一样重新定义…

正则表达式,用于验证以+254开头的电话号码 - php

我正在尝试创建一个正则表达式以匹配以+254开头的电话号码电话号码示例 +254716370730+254856798768 +254765432135 我努力了preg_match('/^\\+254\\d{9}/,$phonenumber'); 但似乎无效,我们将不胜感激任何帮助 参考方案 正确的语法应为:preg_match(�…

brew链接php71:无法符号链接sbin / php-fpm - php

我正在尝试安装需要PHP 7.1的Laravel Valet,但是当我运行brew install php71时,出现以下错误:==> Pouring php71-7.1.10_21.high_sierra.bottle.tar.gz Error: The `brew link` step did not complete successfully T…

PHP-将日期插入日期时间字段 - php

我已在数据库中使用datetime字段存储日期,使用PHP将“今天的日期”插入该字段的正确方法是什么?干杯, 参考方案 我认为您可以使用php date()函数

PHP-如何获取类的成员函数列表? - php

如果我知道班级的名字。有没有办法知道类的成员函数列表? 参考方案 get_class_methods()是你的朋友