drm16022 2018-12-03 14:54
浏览 273
已采纳

在docker和ubuntu中从上游读取响应头时失败(104:由对等方重置连接)

I created a DockerFile like below

FROM ubuntu:18.04


MAINTAINER Amin Keshavarz <ak_1596@yahoo.com>

# Add your github access token if needed in composer update as arg or env var.
ARG github_access_token
ENV github_access_token=${github_access_token}


ENV DEBIAN_FRONTEND=noninteractive


# Install dependency packages
RUN apt-get update && apt-get install -yq --no-install-recommends \
    git \
    curl \
    ca-certificates \
#    php \
    php7.2-fpm php7.2-common \
    php7.2-mongodb php-pear php7.2-dev

RUN apt-get install -y build-essential
# Install mongodb driver
RUN pecl install mongodb
#RUN echo "extension=mongodb.so" >> /etc/php/7.2/fpm/php.ini
#RUN echo "extension=mongodb.so" >> /etc/php/7.2/cli/php.ini

RUN curl -sS https://getcomposer.org/installer | php -- --install-dir=/usr/local/bin --filename=composer

RUN apt-get install -y php7.2-mbstring \
    php7.2-intl \
    php7.2-soap \
    php7.2-curl \
    php7.2-imap \
    php7.2-zmq \
    php7.2-bcmath \
    php7.2-gd \
    php7.2-zip

# Add working directory and copy files into that.
RUN mkdir /app

VOLUME /app
WORKDIR /app
COPY . /app

# Start application installation by composer update command.
#RUN composer config -g github-oauth.github.com $github_access_token
RUN composer global require fxp/composer-asset-plugin
#RUN composer update -vvv


ENTRYPOINT service php7.2-fpm start && /bin/bash

CMD ["php-fpm"]

EXPOSE 9000

And using below docke-compose.yml

version: "3"
services:
  web:
    build:
      context: .
      dockerfile: ./docker/Dockerfile
    container_name: "crm_web"
    tty: true
    ports:
      - "9000:9000"
    networks:
      - default
    volumes:
      - .:/app
  nginx:
    image: nginx:1.10.3
    container_name: "crm_nginx"
    ports:
      - 8080:80
    restart: always
    volumes:
      - ./docker/nginx.conf:/etc/nginx/conf.d/default.conf
      - .:/app
    links:
      - web
    depends_on:
      - web

And has below nginx.conf

server {
    client_max_body_size 100M;
    set $host_path "/app";
    access_log  /app/log/access.log  main;

    server_name  _ localhost;
    root   $host_path/;
    set $yii_bootstrap "index.php";

    charset utf-8;

    location / {
        index  index.html $yii_bootstrap;
        try_files $uri $uri/ /$yii_bootstrap?$args;
    }

    location ~ ^/(protected|framework|themes/\w+/views) {
        deny  all;
    }

    #avoid processing of calls to unexisting static files by yii
    location ~ \.(js|css|png|jpg|gif|swf|ico|pdf|mov|fla|zip|rar)$ {
        try_files $uri =404;
    }

    # pass the PHP scripts to FastCGI server listening on web:9000
    #
    location ~ \.php {
        fastcgi_split_path_info  ^(.+\.php)(.*)$;

        #let yii catch the calls to unexising PHP files
        set $fsn /$yii_bootstrap;
        if (-f $document_root$fastcgi_script_name){
            set $fsn $fastcgi_script_name;
        }

        fastcgi_pass   web:9000;
        include fastcgi_params;
        fastcgi_param  SCRIPT_FILENAME  $document_root$fsn;

        #PATH_INFO and PATH_TRANSLATED can be omitted, but RFC 3875 specifies them for CGI
        fastcgi_param  PATH_INFO        $fastcgi_path_info;
        fastcgi_param  PATH_TRANSLATED  $document_root$fsn;
    }

    # prevent nginx from serving dotfiles (.htaccess, .svn, .git, etc.)
    location ~ /\. {
        deny all;
        access_log off;
        log_not_found off;
    }
}

But when i try to connect my host http://localhost:8080 i get below error in my console from docker:

crm_nginx | 2018/12/03 14:48:14 [error] 28#28: *17 recv() failed (104: Connection reset by peer) while reading response header from upstream, client: 172.18.0.1, server: _, request: "GET /web/ HTTP/1.1", upstream: "fastcgi://172.18.0.2:9000", host: "localhost:8080"

And get 502 Bad Gateway in browser.

Can you help me solve this problem? What i missed?

  • 写回答

1条回答 默认 最新

  • dongyi1215 2018-12-05 08:16
    关注

    Add below line into your Dockerfile

    RUN sed -i "s|;*listen\s*=\s*/run/php/php7.2-fpm.sock|listen = 9000|g" /etc/php/7.2/fpm/pool.d/www.conf && \
        sed -i "s|;*listen\s*=\s*/||g" /etc/php/7.2/fpm/php-fpm.conf
    

    This will tell to php7.2-fpm listen to port 9000 instead /run/php/php7.2-fpm.sock.

    Be careful do not add ip address before port like 127.0.0.1:9000 because this force php to listen port and ip in same time but you don't your container has wich ip address.

    I found my answer here and you can refer to that:

    https://www.digitalocean.com/community/questions/nginx-error-111-connection-refused

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

报告相同问题?

悬赏问题

  • ¥15 opencv 无法读取视频
  • ¥15 用matlab 实现通信仿真
  • ¥15 按键修改电子时钟,C51单片机
  • ¥60 Java中实现如何实现张量类,并用于图像处理(不运用其他科学计算库和图像处理库))
  • ¥20 5037端口被adb自己占了
  • ¥15 python:excel数据写入多个对应word文档
  • ¥60 全一数分解素因子和素数循环节位数
  • ¥15 ffmpeg如何安装到虚拟环境
  • ¥188 寻找能做王者评分提取的
  • ¥15 matlab用simulink求解一个二阶微分方程,要求截图