docker-compose nginx将502返回到php-fpm

问题描述

请帮助我。有一个错误,一个小错误。也许我只是看不到它,但我不知道。问题的本质:我正在设置一个docker环境。带来了nginx,fpm。当网址获得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)。

我采取了什么措施来发现错误,但没有找到:

  • curl localhost:80 / index.html >> HELLO_WORLD

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

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

  • curl localhost:80 / index.php + docker-compose log >> 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容器>> 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调试; >> 猫 /var/log/nginx/error.log-空

结构www /-root:root

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

解决方法

我已经解决了这个问题。我不想分阶段写作,但我会简而言之。问题是由于我的操作系统构建为centos 8而引起的。首先,容器的不正确行为将显示Dockerfile的使用,并将确定存储库的问题。可以解决这些问题,但是对我而言,这是一个触发因素,表明我的环境不正确。然后我按照说明在Centos 8下安装了所有docker软件包。

并参阅 https://computingforgeeks.com/install-docker-and-docker-compose-on-rhel-8-centos-8/

相关问答

错误1:Request method ‘DELETE‘ not supported 错误还原:...
错误1:启动docker镜像时报错:Error response from daemon:...
错误1:private field ‘xxx‘ is never assigned 按Alt...
报错如下,通过源不能下载,最后警告pip需升级版本 Requirem...