簡體   English   中英

無法使Nginx和php-fpm docker通信

[英]Can't make nginx and php-fpm dockers communicate

我創建了兩個泊塢窗圖像來滿足我的需求,這是nginx之一:

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

到目前為止,一切正常,容器正在運行,並且即使任何php腳本失敗,我也可以從nginx獲取靜態內容

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

對我可能做錯的任何想法?

首先,我不確定您是否要使用privileged啟動Nginx容器。 也沒有必要用:rw開關掛載php指令。 rw是默認模式。

第二,看看告訴nginx發送php請求的位置-具體來說,哪個主機由nginx的fastcgi_pass指令定義。 您已將php請求發送到名為vetology的計算機。 但是--link php:php將容器與--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。

暫無
暫無

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

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