connect()失败(111:连接被拒绝),同时为nginx + php-fpm docker

时间:2018-07-01 09:40:36

标签: php symfony docker nginx

简介

我想创建一个由php-fpmnginxdocker驱动的站点,并且我想在单独的容器上放置php-fpmnginx。为此,我遵循了this tutorial,但是现在在访问localhost:8000下的Web服务器时陷入困境。我的浏览器显示502,而我的/var/log/nginx/project_error.log充满

[error] 7#7: *1 connect() failed (111: Connection refused) while connecting to upstream, client: 172.20.0.1, server: app, request: "GET / HTTP/1.1", upstream: "fastcgi://127.0.0.1:9000", host: "localhost:8000"

搜索到的答案

我已经对该主题进行了一些搜索,并找到了以下线程:

但是,他们都没有解决我的问题。

文件

created a GitHub repository包含所有文件,因此任何人都可以在那里简单地将其检出(我认为这比将所有文件转储到此处更好)。为此,只需做

git clone git@github.com:tzfrs/dockertest.git
mkdir -p citynavigator/public
touch citynavigator/public/index.php

除此之外,这是代码

一般

docker-compose.yml

version: "2"
services:
    db:
        image: mysql:5.6
        ports:
          - "3306:3306"
        environment:
          - MYSQL_ROOT_PASSWORD=citynavigatorrootpass
          - MYSQL_DATABASE=citynavigator
          - MYSQL_USER=citynavigator
          - MYSQL_PASSWORD=citynavigatorpass
        volumes:
          - /var/lib/mysql

    nginx:
        image: nginx:latest
        ports:
            - "8000:80"
        volumes:
            - ../citynavigator:/var/www/app
            - ./nginx/nginx.conf:/etc/nginx/nginx.conf
            - ./nginx/app.conf:/etc/nginx/conf.d/default.conf
            - ./nginx/fastcgi_params:/etc/nginx/fastcgi_params
        links:
            - php
        networks:
            - code-network

    php:
        build: fpm/
        volumes:
            - ../citynavigator:/var/www/app
        networks:
            - code-network

networks:
    code-network:
        driver: bridge

fpm/Dockerfile

FROM php:7.2-fpm

LABEL maintainer="Theo Tzaferis<t.tzfrs@gmail.com>"

USER root

RUN rm /usr/local/etc/php-fpm.d/docker.conf \
    && rm /usr/local/etc/php-fpm.d/www.conf \
    && rm /usr/local/etc/php-fpm.d/www.conf.default \
    && rm /usr/local/etc/php-fpm.d/zz-docker.conf \
    && rm -rf /var/www/html

ADD app.pool.conf /usr/local/etc/php-fpm.d/app.pool.conf

fpm/app.pool.conf

[app]
user = www-data
group = www-data

listen = 127.0.0.1:9000

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

catch_workers_output = yes

nginx/app.conf

upstream php-upstream {
    server 127.0.0.1:9000;
}

server {
    server_name app;
    root /var/www/app/public;

    location / {
        try_files $uri /index.php$is_args$args;
    }

    location ~ ^/index\.php(/|$) {
        fastcgi_pass php-upstream;
        fastcgi_split_path_info ^(.+\.php)(/.*)$;
        include fastcgi_params;

        # optionally set the value of the environment variables used in the application
        # fastcgi_param APP_ENV prod;
        # fastcgi_param APP_SECRET <app-secret-id>;
        # fastcgi_param DATABASE_URL "mysql://db_user:db_pass@host:3306/db_name";

        # When you are using symlinks to link the document root to the
        # current version of your application, you should pass the real
        # application path instead of the path to the symlink to PHP
        # FPM.
        # Otherwise, PHP's OPcache may not properly detect changes to
        # your PHP files (see https://github.com/zendtech/ZendOptimizerPlus/issues/126
        # for more information).
        fastcgi_param SCRIPT_FILENAME $realpath_root$fastcgi_script_name;
        fastcgi_param DOCUMENT_ROOT $realpath_root;
        # Prevents URIs that include the front controller. This will 404:
        # http://domain.tld/index.php/some-path
        # Remove the internal directive to allow URIs like this
        internal;
    }

    # return 404 for all other php files not matching the front controller
    # this prevents access to other php files you don't want to be accessible.
    location ~ \.php$ {
        return 404;
    }

    error_log /var/log/nginx/project_error.log;
    access_log /var/log/nginx/project_access.log;
}

nginx/fastcgi_params

fastcgi_buffer_size 128k;
fastcgi_buffers 4 256k;
fastcgi_busy_buffers_size 256k;

fastcgi_param   QUERY_STRING            $query_string;
fastcgi_param   REQUEST_METHOD          $request_method;
fastcgi_param   CONTENT_TYPE            $content_type;
fastcgi_param   CONTENT_LENGTH          $content_length;

fastcgi_param   SCRIPT_FILENAME         $document_root$fastcgi_script_name;
fastcgi_param   SCRIPT_NAME             $fastcgi_script_name;
fastcgi_param   PATH_INFO               $fastcgi_path_info;
fastcgi_param   PATH_TRANSLATED         $document_root$fastcgi_path_info;
fastcgi_param   REQUEST_URI             $request_uri;
fastcgi_param   DOCUMENT_URI            $document_uri;
fastcgi_param   DOCUMENT_ROOT           $document_root;
fastcgi_param   SERVER_PROTOCOL         $server_protocol;

fastcgi_param   GATEWAY_INTERFACE       CGI/1.1;
fastcgi_param   SERVER_SOFTWARE         nginx/$nginx_version;

fastcgi_param   REMOTE_ADDR             $remote_addr;
fastcgi_param   REMOTE_PORT             $remote_port;
fastcgi_param   SERVER_ADDR             $server_addr;
fastcgi_param   SERVER_PORT             $server_port;
fastcgi_param   SERVER_NAME             $server_name;

fastcgi_param   HTTPS                   $https;

# PHP only, required if PHP was built with --enable-force-cgi-redirect
# fastcgi_param   REDIRECT_STATUS         200;

# Allow 10 minute script execution time
fastcgi_read_timeout 600s;

nginx/nginx.conf

user www-data;
worker_processes 4;
pid /run/nginx.pid;

events {
  worker_connections  2048;
  multi_accept on;
  use epoll;
}

http {
  server_tokens off;
  sendfile on;
  tcp_nopush on;
  tcp_nodelay on;
  keepalive_timeout 15;
  types_hash_max_size 2048;
  include /etc/nginx/mime.types;
  default_type application/octet-stream;
  access_log off;
  error_log off;
  gzip on;
  gzip_disable "msie6";
  gzip_vary on;
  gzip_proxied any;
  gzip_comp_level 6;
  gzip_buffers 16 8k;
  gzip_http_version 1.1;
  gzip_types text/plain text/css application/json application/x-javascript text/xml application/xml application/xml+rss text/javascript application/javascript image/svg+xml;
  include /etc/nginx/conf.d/*.conf;
  open_file_cache max=100;
  client_max_body_size 30m;
}

1 个答案:

答案 0 :(得分:1)

这是因为127.0.0.1应该是您的php服务的名称,而不是localhost

所以改变

fastcgi_pass php-upstream;

fastcgi_pass php:9000;

或与所使用的上游指令中的类似。

如果您的nginx在同一网络中(很明显是通过代码网络),则可以从您的nginx访问

php