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

Posted

技术标签:

【中文标题】在 docker 和 ubuntu 中从上游读取响应标头时失败(104:对等方重置连接)【英文标题】:failed (104: Connection reset by peer) while reading response header from upstream in docker and ubuntu 【发布时间】:2019-05-04 21:28:33 【问题描述】:

我创建了一个DockerFile,如下所示

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

并在下面使用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

并且有下面的 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;
    

但是当我尝试连接我的主机 http://localhost:8080 时,我在控制台中从 docker 收到以下错误:

crm_nginx | 2018/12/03 14:48:14 [错误] 28#28: *17 recv() 失败 (104: 对等方重置连接)同时从上游读取响应标头, 客户端:172.18.0.1,服务器:_,请求:“GET /web/ HTTP/1.1”, 上游:“fastcgi://172.18.0.2:9000”,主机:“localhost:8080”

并在浏览器中获取502 Bad Gateway

你能帮我解决这个问题吗? 我错过了什么?

【问题讨论】:

(任何可以运行 docker history 的人现在都拥有一个用于您帐户的 GitHub 访问令牌;如果您在任何地方发布了此图像,您可能应该认为该令牌已被盗用,并仔细检查是否没有做任何事情代表您。) @DavidMaze 感谢您的考虑。 @DavidMaze 暴露问题呢? 【参考方案1】:

将下面的行添加到您的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

这将告诉 php7.2-fpm 监听端口 9000 而不是 /run/php/php7.2-fpm.sock

注意不要在127.0.0.1:9000之类的端口之前添加ip地址,因为这会强制php同时监听端口和ip,但你的容器没有ip地址。

我在这里找到了我的答案,你可以参考一下:

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

【讨论】:

以上是关于在 docker 和 ubuntu 中从上游读取响应标头时失败(104:对等方重置连接)的主要内容,如果未能解决你的问题,请参考以下文章

上游超时(uWSGI + NGINX + Flask)

从上游读取响应头时,Nginx uwsgi(104:由对等方重置连接)

如何在 Docker 中从 Docker 连接到 SQL Server 服务?

nginx - 从上游服务器读取自定义标头

Docker 网络 - nginx:在上游找不到 [emerg] 主机

docker nginx 出现“502”.1 上游服务器在连接上游时暂时禁用