dongxizhe9755 2018-01-22 19:33
浏览 300
已采纳

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

I know there's a ton of posts regarding 502 Bad Gateway, but I haven't been able to solve this problem. I'm using Docker Compose to create separate containers for Nginx and PHP-FPM.

Error I get loading PHP files in the browser (HTML files render fine):

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"

I've tried tweaking the various configs, using Unix socket, etc., for hours, and I still get 502 errors with PHP files. Can you spot what's wrong?

Here are all required files..

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
  • 写回答

1条回答 默认 最新

  • duanhuchang5056 2018-01-22 20:15
    关注

    Problem could be in the www.conf file. You are listening to 127.0.0.1:9000 but this way from won't be reachable outside the container.

    Try binding to 0.0.0.0:9000:

    listen = 0.0.0.0:9000
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥200 csgo2的viewmatrix值是否还有别的获取方式
  • ¥15 Stable Diffusion,用Ebsynth utility在视频选帧图重绘,第一步报错,蒙版和帧图没法生成,怎么处理啊
  • ¥15 请把下列每一行代码完整地读懂并注释出来
  • ¥15 pycharm运行main文件,显示没有conda环境
  • ¥15 寻找公式识别开发,自动识别整页文档、图像公式的软件
  • ¥15 为什么eclipse不能再下载了?
  • ¥15 编辑cmake lists 明明写了project项目名,但是还是报错怎么回事
  • ¥15 关于#计算机视觉#的问题:求一份高质量桥梁多病害数据集
  • ¥15 特定网页无法访问,已排除网页问题
  • ¥50 如何将脑的图像投影到颅骨上