简体   繁体   English

Docker,Nginx,PHP-FPM:连接问题

[英]Docker, Nginx, PHP-FPM : problems with connection

I've taken on a project built on Docker containers and I'm having it to run smoothly. 我已经进行了一个基于Docker容器的项目,并且使它能够平稳运行。

My containers build successfully, but when I try getting to the website, nginx gives me a 502 with this error in the logs: 我的容器构建成功,但是当我尝试访问网站时,nginx在日志中给了我502错误消息:

connect() failed (111: Connection refused) while connecting to upstream, client: 172.17.0.1, server: , request: "GET / HTTP/1.1", upstream: "fastcgi://172.17.0.6:9000", host: "localhost:2000"

Which, from what I've read would be a problem with the link between my two containers. 从我的阅读中得知,这两个容器之间的链接会出现问题。

I've tried changing the listen parameter of php-fpm directly to 0.0.0.0:9000 as seen on Nginx+PHP-FPM: connection refused while connecting to upstream (502) but this gave rise to a new error I don't fully understand either: 我尝试将php-fpm的listen参数直接更改为0.0.0.0:9000Nginx + PHP-FPM所示):连接到上游时连接被拒绝(502),但这引起了一个新错误,我没有完全解决了解以下任一情况:

*11 FastCGI sent in stderr: "Primary script unknown" while reading response header from upstream, client: 172.17.0.1, server: , request: "GET / HTTP/1.1", upstream: "fastcgi://172.17.0.6:9000", host: "localhost:2000"

Does anyone has an idea of what is failing and how to fix it ? 有谁知道失败的原因以及如何解决?

The docker-compose part regarding theses two services is: 关于这两项服务的docker-compose部分是:

elinoi-webserver:
  build: .
  dockerfile: docker/Dockerfile.nginx.conf
  container_name: elinoi-webserver
  volumes:
      - .:/var/www/elinoi.com
  ports:
   - "2000:80"
  links:
   - elinoi-php-fpm

elinoi-php-fpm:
  build: .
  dockerfile: docker/Dockerfile.php-fpm.conf
  container_name: elinoi-php-fpm
  volumes:
    - .:/var/www/elinoi.com
    - /var/docker_volumes/elinoi.com/shared:/var/www/elinoi.com/shared
  ports:
   - "22001:22"
  links:
    - elinoi-mailhog
    - elinoi-memcached
    - elinoi-mysql
    - elinoi-redis

The nginx conf file is: Nginx conf文件是:

server {
    listen 80 default;

    root /var/www/elinoi.com/current/web;

    rewrite ^/app\.php/?(.*)$ /$1 permanent;

    try_files $uri @rewriteapp;

    location @rewriteapp {
        rewrite ^(.*)$ /app.php/$1 last;
    }

    # Deny all . files
    location ~ /\. {
        deny all;
    }

    location ~ ^/(app|app_dev)\.php(/|$) {
        fastcgi_pass elinoi-php-fpm:9000;
        fastcgi_split_path_info ^(.+\.php)(/.*)$;
        include fastcgi_params;
        fastcgi_param  SCRIPT_FILENAME $document_root$fastcgi_script_name;
        fastcgi_index app.php;
        send_timeout 1800;
        fastcgi_read_timeout 1800;
    }

    # Statics
        location /(bundles|media) {
        access_log off;
        expires 30d;
        try_files $uri @rewriteapp;
    }

}

The Dockerfile for the elinoi-php-fpm service is : elinoi-php-fpm服务的elinoi-php-fpm是:

FROM phpdockerio/php7-fpm:latest
# Install selected extensions
RUN apt-get update \
    && apt-get -y --no-install-recommends install php7.0-memcached php7.0-mysql php7.0-redis php7.0-gd php7.0-imagick php7.0-intl php7.0-xdebug php7.0-mbstring \
    && apt-get -y --no-install-recommends install nodejs npm nodejs-legacy vim ruby-full git build-essential libffi-dev \
    && apt-get clean \
    && rm -rf /var/lib/apt/lists/* /tmp/* /var/tmp/*

RUN npm install -g bower
RUN npm install -g less
RUN gem install sass

# If you're using symfony and the vagranted environment, I strongly recommend you change your AppKernel to use the following temporary folders
# for cache, logs and sessions, otherwise application performance may suffer due to these being shared over NFS back to the host
RUN mkdir -p "/tmp/elinoi/cache" \
    && mkdir -p "/tmp/elinoi/logs" \
    && mkdir -p "/tmp/elinoi/sessions" \
    && chown www-data:www-data -R "/tmp/elinoi"

RUN apt-get update \
    && apt-get -y --no-install-recommends install openssh-server \
    && apt-get clean \
    && rm -rf /var/lib/apt/lists/* /tmp/* /var/tmp/*

RUN mkdir /var/run/sshd
RUN echo 'root:screencast' | chpasswd
RUN sed -i 's/PermitRootLogin without-password/PermitRootLogin yes/' /etc/ssh/sshd_config

# SSH login fix. Otherwise user is kicked off after login
RUN sed 's@session\s*required\s*pam_loginuid.so@session optional pam_loginuid.so@g' -i /etc/pam.d/sshd

ENV NOTVISIBLE "in users profile"
RUN echo "export VISIBLE=now" >> /etc/profile

EXPOSE 22
ADD docker/.ssh /root/.ssh
RUN chmod 700 /root/.ssh/authorized_keys

CMD ["/usr/sbin/sshd", "-D"]

WORKDIR "/var/www/elinoi.com"

The Dockerfile for the elinoi-webserver is: elinoi-webserver的Dockerfile是:

FROM smebberson/alpine-nginx:latest

COPY /docker/nginx.conf /etc/nginx/conf.d/default.conf

WORKDIR "/var/www/elinoi.com"

There can only be one CMD instruction in a Dockerfile. Dockerfile中只能有一条CMD指令。 If you list more than one CMD then only the last CMD will take effect. 如果您列出多个CMD,则只有最后一个CMD才会生效。

The original Dockerfile ends with: 原始Dockerfile结尾为:

CMD /usr/bin/php-fpm

and the Dockerfile of the elinoi-php-fpm service ends with the following CMD layer: 并且elinoi-php-fpm服务的elinoi-php-fpm以以下CMD层结尾:

CMD ["/usr/sbin/sshd", "-D"]

So, only sshd is started after container creates. 因此,在创建容器后仅启动sshd php-fpm is not started there. php-fpm不在此处启动。

That's why nginx constantly returns 502 error, because php backend is not working at all. 这就是nginx不断返回502错误的原因,因为php后端根本无法工作。

You can fix your issue the following ways: 您可以通过以下方式解决问题:

1. Docker Alpine linux running 2 programs 1. Docker Alpine linux运行2个程序

2. Simply delete sshd part from the elinoi-php-fpm service. 2.只需从elinoi-php-fpm服务中删除sshd部分。

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

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