无法让nginx和php-fpm码头工人进行通信

时间:2016-11-01 21:31:42

标签: php nginx docker

我创建了两个docker图像以满足我的需求,这里是nginx one:

FROM alpine:3.3
RUN apk add --update nginx
EXPOSE 80 443
CMD nginx -c /www/dev/nginx/conf/nginx.conf -g 'daemon off;'
然后是php-fpm

FROM php:7.0-fpm
RUN apt-get update
RUN apt-get install -y apt-transport-https ca-certificates dcmtk libgdcm-tools wkhtmltopdf libdbd-freetds libfreetype6-dev \
    libjpeg62-turbo-dev \
    libmcrypt-dev \
    libpng12-dev \
&& docker-php-ext-install -j$(nproc) iconv mcrypt \
&& docker-php-ext-configure gd --with-freetype-dir=/usr/include/ --with-jpeg-dir=/usr/include/ \
&& docker-php-ext-install -j$(nproc) gd
RUN apt-get install -y imagemagick --fix-missing
RUN apt-get clean
RUN rm -rf /var/lib/apt/lists/* /tmp/* /var/tmp/*
WORKDIR /www/public
CMD rm /etc/freetds.conf
ADD freetds.conf /etc/freetds.conf
CMD rm /var/cache/apk/*

然后按以下顺序运行命令

docker create -p 9000:9000 --name php -v /www:/www:rw php
docker start php
docker create --privileged=true -p 80:80 -p 443:443 --name nginx -v /www:/www --link php:php nginx
docker start nginx

到目前为止所有工作都很好,容器正在运行,我甚至可以从nginx获取静态内容,因为任何php脚本都失败了

[error] 6#0: *3 connect() failed (111: Connection refused) while connecting to upstream, client: 172.17.0.1, server: platform.v2.vetology.net, request: "GET /index.php HTTP/1.1", upstream: "fastcgi://172.17.0.2:9000", host: "localhost.localdomain"

cgi-fcgi -bind -connect 127.0.0.1:9000

来自主机工作,php响应,所以我知道两个容器都工作和响应他们只是不通信。

我在另一个帖子中读到这个问题是由php和nginx使用不同的根目录引起的,但事实并非如此,因为两者都安装了/ www目录而/ www / public包含index.php文件我是试图打开。

这是nginx.conf

    user nobody;
error_log /www/dev/nginx/log/error.log;
pid /run/nginx.pid;
include /usr/share/nginx/modules/*.conf;
worker_processes  1;
events {
    worker_connections  1024;
}
http {
    log_format  main  '$remote_addr - $remote_user [$time_local] "$request" '
                      '$status $body_bytes_sent "$http_referer" '
                      '"$http_user_agent" "$http_x_forwarded_for"';
    access_log  /www/dev/nginx/log/access.log  main;
    sendfile            on;
    tcp_nopush          on;
    tcp_nodelay         on;
    keepalive_timeout   65;
    types_hash_max_size 2048;
    include             /etc/nginx/mime.types;
    include /etc/nginx/conf.d/*.conf;
    default_type  application/octet-stream;
    gzip  on;
    ssl_session_cache   shared:SSL:10m;
    ssl_session_timeout 10m;
 include /www/dev/nginx/conf/upstream.conf;
 server {
  listen 80;
  listen 443 ssl;
  listen [::]:443 default ipv6only=on;
  listen [::]:80 default_server ipv6only=on;
  ssl_certificate     /www/dev/ssl/certificate.crt;
  ssl_certificate_key /www/dev/ssl/key.key;
  ssl_protocols       TLSv1 TLSv1.1 TLSv1.2;
  ssl_ciphers         HIGH:!aNULL:!MD5;
  root /www/public;
  index index.php index.html index.htm;
  server_name hostname.net;
  location / {
   try_files $uri $uri/ /index.php?$query_string;
  }
  client_body_buffer_size    128k;
  client_body_temp_path /www/dev/nginx/tmp/client_body_temp;
  proxy_temp_path   /www/dev/nginx/tmp/proxy_temp_path;
  fastcgi_temp_path  /www/dev/nginx/tmp/fastcgi_temp_path;
  location ~ \.php$ {
   try_files $uri /index.php =404;
   fastcgi_split_path_info ^(.+\.php)(/.+)$;
   fastcgi_param HTTP_PROXY "";
   fastcgi_pass vetology;
   fastcgi_index index.php;
   include fastcgi_params;
  }
 }
}

我可能做错了什么想法?

1 个答案:

答案 0 :(得分:0)

首先,我不确定你是否想要使用privileged启动nginx容器。也没有必要使用:rw开关挂载php指令。 rw是默认模式。

其次,看看你告诉nginx发送php请求的位置 - 具体来说,哪个主机是在nginx的fastcgi_pass指令中定义的。您已将php请求发送到名为vetology的计算机。但是,docker将容器与--link php:php链接在一起。这意味着它获取名为php的容器并为其指定名为php的别名。这意味着它将作为名为php的计算机公开给您的nginx容器。

你可以尝试一下:

docker run -it --rm --link php:some_php nginx sh -c 'ping some_php'

更改

fastcgi_pass vetology;

为:

fastcgi_pass php:9000;

你的容器应该可以工作。

对于像这样的复杂容器组件,请查看下一个docker-compose。