繁体   English   中英

docker-compose nginx 返回 502 到 php-fpm

[英]docker-compose nginx return 502 to php-fpm

请帮我。 有一个错误,一个小错误。 也许我只是没有看到它,但我不知道。 问题本质:我正在搭建一个docker环境。 自带nginx,fpm。 当 url 获取到一个 php 文件时,nginx 返回 502。

结构:

/www
 /app
   /index.php
   /index.html
 /data
   /db
 /etc
   /nginx
      default.conf
   /php
      php.ini
      php-fpm.conf
  1. 撰写 + 环境:
version: '3.5'
services:
    nginx:
        image: nginx:alpine
        volumes:
            - "./etc/nginx/default.conf:/etc/nginx/conf.d/default.conf"
            - "./etc/ssl:/etc/ssl"
            - "./app:/var/www/html"
            - "./etc/nginx/default.template.conf:/etc/nginx/conf.d/default.template"
        ports:
            - "80:80"
        environment:
            - NGINX_HOST=${NGINX_HOST}
        command: /bin/sh -c "envsubst '$$NGINX_HOST' < /etc/nginx/conf.d/default.template > /etc/nginx/conf.d/default.conf && nginx -g 'daemon off;'"
        restart: always
        depends_on:
            - php
            - mysqldb
            - memcached
        networks:
            - app
    php:
        image: php:${PHP_VERSION}-fpm
        restart: always
        volumes:
            - "./etc/php/php.ini:/usr/local/etc/php/conf.d/php.ini"
            - "./etc/php/php-fpm.conf:/usr/local/etc/php-fpm.conf"
            - "./app:/var/www/html"
        networks:
            - app
        ports:
            - "9000:9000"
        networks:
            - app
    mysqldb:
        image: mysql:${MYSQL_VERSION}
        container_name: ${MYSQL_HOST}
        restart: always
        env_file:
            - ".env"
        environment:
            - MYSQL_DATABASE=${MYSQL_DATABASE}
            - MYSQL_ROOT_PASSWORD=${MYSQL_ROOT_PASSWORD}
            - MYSQL_USER=${MYSQL_USER}
            - MYSQL_PASSWORD=${MYSQL_PASSWORD}
        ports:
            - "3306:3306"
        volumes:
            - "./data/db/mysql:/var/lib/mysql"
        networks:
            - app
    memcached:
        image: memcached:${MEMCACHED_VERSION}
        container_name: ${MEMCACHED_HOST}
        ports:
            - "11211:11211"
        networks:
            - app
networks:
    app:
        driver: bridge
#!/usr/bin/env bash

# See https://docs.docker.com/compose/environment-variables/#the-env-file

# Nginx
NGINX_HOST=localhost

# PHP

PHP_VERSION=5.4

# MySQL
MYSQL_VERSION=5.7.22
MYSQL_HOST=mysql
MYSQL_DATABASE=test
MYSQL_ROOT_USER=root
MYSQL_ROOT_PASSWORD=root
MYSQL_USER=dev
MYSQL_PASSWORD=dev

# Memcached
MEMCACHED_VERSION=latest
MEMCACHED_HOST=memcached
  1. Nginx [/etc/nginx/default.conf]:
upstream phpserver {
    server php:9000;
}

server {
    listen 80 default_server;
    listen [::]:80 default_server;
    server_name localhost;

    index index.php index.html;
    error_log  /var/log/nginx/error.log;
    access_log /var/log/nginx/access.log;
    root /var/www/html;

    location ~ \.php$ {
        try_files $uri =404;
        fastcgi_pass phpserver;
        fastcgi_index index.php;
        fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
        include fastcgi_params;
    }
}
  1. PHP [php-fpm.conf]:
[global]

error_log = /proc/self/fd/2
daemonize = no

[www]

; if we send this to /proc/self/fd/1, it never appears
access.log = /proc/self/fd/2

user = www-data
group = www-data

listen = 9000

pm = dynamic
pm.max_children = 5
pm.start_servers = 2
pm.min_spare_servers = 1
pm.max_spare_servers = 3

clear_env = no

; Ensure worker stdout and stderr are sent to the main error log.
catch_workers_output = yes
  1. PHP [php.ini] - ini 文件是默认的。 5.4. 它不包括用户、会话之前的目录等这样的结构。 更多... phpv 应该就是这个 (5,4)。

我采取了哪些步骤来找到我的错误,但没有找到:

  • 卷曲本地主机:80/index.html >> HELLO_WORLD

  • 卷曲本地主机:80/index.php >> 502 nginx

  • netstat -an |grep 9000 >> tcp6 0 0 :::9000 :::* LISTEN

  • curl localhost:80/index.php + nginx_1 | 2020/08/26 16:16:09 [error] 7#7: *3 connect() failed (113: Host is unreachable) while connecting to upstream, client: 172.26.0.1, server: localhost, request: "GET /index.php HTTP/1.1", upstream: "fastcgi://172.26.0.2:9000", host: "localhost" nginx_1 | 172.26.0.1 - - [26/Aug/2020:16:16:09 +0000] "GET /index.php HTTP/1.1" 502 157 "-" "curl/7.61.1" nginx_1 | 2020/08/26 16:16:09 [info] 7#7: *3 client 172.26.0.1 closed keepalive connection -compose 日志>> nginx_1 | 2020/08/26 16:16:09 [error] 7#7: *3 connect() failed (113: Host is unreachable) while connecting to upstream, client: 172.26.0.1, server: localhost, request: "GET /index.php HTTP/1.1", upstream: "fastcgi://172.26.0.2:9000", host: "localhost" nginx_1 | 172.26.0.1 - - [26/Aug/2020:16:16:09 +0000] "GET /index.php HTTP/1.1" 502 157 "-" "curl/7.61.1" nginx_1 | 2020/08/26 16:16:09 [info] 7#7: *3 client 172.26.0.1 closed keepalive connection nginx_1 | 2020/08/26 16:16:09 [error] 7#7: *3 connect() failed (113: Host is unreachable) while connecting to upstream, client: 172.26.0.1, server: localhost, request: "GET /index.php HTTP/1.1", upstream: "fastcgi://172.26.0.2:9000", host: "localhost" nginx_1 | 172.26.0.1 - - [26/Aug/2020:16:16:09 +0000] "GET /index.php HTTP/1.1" 502 157 "-" "curl/7.61.1" nginx_1 | 2020/08/26 16:16:09 [info] 7#7: *3 client 172.26.0.1 closed keepalive connection

  • php 容器root@..:/var/www/html# php index.html *>>* HELLO_WORLD root@..:/var/www/html# php index.php *>>* HELLO_WORLD

  • php conteiner >> root@..:/var/www/html# ls -ll total 13 -rwxrwxrwx. 1 root root 8518 Aug 14 11:36 index.php -rwxrwxrwx. 1 root root 34 Aug 25 21:04 index.html root@..:/var/www/html# ls -ll total 13 -rwxrwxrwx. 1 root root 8518 Aug 14 11:36 index.php -rwxrwxrwx. 1 root root 34 Aug 25 21:04 index.html

  • error_log /var/log/nginx/error.log 调试; >> cat /var/log/nginx/error.log - 空

结构 www/ - root:root

我很抱歉浪费时间。 请帮我。 谢谢!

我已经解决了这个问题。 我不想分阶段写,但我会简单地说。 问题是因为我的操作系统版本是centos 8。首先,容器的错误行为会显示使用Dockerfile,会发现repo的问题。 这些问题是可以解决的,但是对我来说却成了一个触发器,说明我的环境不对。 然后我按照在 Centos 8 下安装的指示安装了 docker 的所有软件包。

& 见https://computingforgeeks.com/install-docker-and-docker-compose-on-rhel-8-centos-8/

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM