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

时间:2018-03-08 09:44:36

标签: php docker nginx

我参与了一个基于Docker容器构建的项目,并且让它能够顺利运行。

我的容器构建成功,但是当我尝试访问该网站时,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"

根据我所读到的内容,我的两个容器之间的链接存在问题。

我已经尝试将php-fpm的listen参数直接更改为0.0.0.0:9000,如documentation所示,但这会产生新的错误我不会完全理解:

*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"

有没有人知道什么是失败以及如何解决?

有关这两项服务的码头组成部分是:

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

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;
    }

}

elinoi-php-fpm服务的Dockerfile是:

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"

elinoi-webserver的Dockerfile是:

FROM smebberson/alpine-nginx:latest

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

WORKDIR "/var/www/elinoi.com"

1 个答案:

答案 0 :(得分:1)

  

Dockerfile中只能有一条CMD指令。如果你列出   不止一个CMD,那么只有最后一个CMD才会生效。

原始Dockerfile以:

结尾
id

并且CMD /usr/bin/php-fpm 服务的Dockerfile以以下elinoi-php-fpm图层结束:

CMD

因此,在容器创建后才启动CMD ["/usr/sbin/sshd", "-D"] sshd未在此处启动。

这就是为什么nginx不断返回502错误,因为php后端根本不起作用。

您可以通过以下方式解决问题:

1。 Docker Alpine linux running 2 programs

2。只需从php-fpm服务中删除sshd部分。