簡體   English   中英

Docker,Nginx,PHP-FPM:連接問題

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

我已經進行了一個基於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: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"

有誰知道失敗的原因以及如何解決?

關於這兩項服務的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

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服務的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"

elinoi-webserver的Dockerfile是:

FROM smebberson/alpine-nginx:latest

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

WORKDIR "/var/www/elinoi.com"

Dockerfile中只能有一條CMD指令。 如果您列出多個CMD,則只有最后一個CMD才會生效。

原始Dockerfile結尾為:

CMD /usr/bin/php-fpm

並且elinoi-php-fpm服務的elinoi-php-fpm以以下CMD層結尾:

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

因此,在創建容器后僅啟動sshd php-fpm不在此處啟動。

這就是nginx不斷返回502錯誤的原因,因為php后端根本無法工作。

您可以通過以下方式解決問題:

1. Docker Alpine linux運行2個程序

2.只需從elinoi-php-fpm服務中刪除sshd部分。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM