2017-04-26 24 views
1

我運行PHP5.6 2個泊塢窗容器:多個版本泊塢

docker run --name php5 \ 
    -v /html1:/var/www/html/site1 \ 
    -d -p 9001:9000 php:5.6-fpm 

而對於PHP7:

docker run --name php7 \ 
    -v /html2:/var/www/html/site2 \ 
    -d -p 9000:9000 php:7-fpm 

我跑碼頭工人容器的Nginx:

docker run --name nginx-cache \ 
    -v /nginx.conf:/etc/nginx/nginx.conf \ 
    -v /nginx/html1:/var/www/html/site1 \ 
    -v /nginx/html2:/var/www/html/site2 \ 
    -v /sites-enabled:/etc/nginx/sites-enabled/ \ 
    --link php5 --link php7 -d -p 9999:80 nginx 

nginx.conf

user nginx; 
worker_processes 1; 

error_log /var/log/nginx/error.log warn; 
pid  /var/run/nginx.pid; 

events { 
    worker_connections 1024; 
} 

http { 
    sendfile on; 

    gzip    on; 
    gzip_http_version 1.0; 
    gzip_proxied  any; 
    gzip_min_length 500; 
    gzip_disable  "MSIE [1-6]\."; 
    gzip_types  text/plain text/xml text/css 
         text/comma-separated-values 
         text/javascript 
         application/x-javascript 
         application/atom+xml; 
    gzip_disable  "msie6"; 


    ## 
    # Basic Settings 
    ## 

    server_names_hash_bucket_size 64; 

    tcp_nopush on; 
    tcp_nodelay on; 
    keepalive_timeout 65; 
    types_hash_max_size 2048; 
    client_body_buffer_size 128k; 

    include /etc/nginx/mime.types; 
    default_type application/octet-stream; 

    ## 
    # Logging Settings 
    ## 

    access_log /var/log/nginx/access.log; 
    error_log /var/log/nginx/error.log; 

    ## 
    # Virtual Host Configs 
    ## 

    include /etc/nginx/conf.d/*.conf; 
    include /etc/nginx/sites-enabled/*; 

} 

與site1的配置:

server { 
     listen 80 default_server; 

     server_name site1; 

     root /var/www/html/site1/; 
     index index.php index.html index.htm default.html default.htm; 

     fastcgi_buffers 8 16k; 
     fastcgi_buffer_size 32k; 
     fastcgi_read_timeout 180; 

     location ~ \.php$ { 
      fastcgi_pass php5:9001; 
      fastcgi_index index.php; 
      include fastcgi_params; 
      fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name; 
      fastcgi_param SERVER_NAME $server_name; 
     } 
} 

和Site2配置:

server { 
     listen 80; 

     server_name site2; 

     root /var/www/html/site2/; 
     index index.php index.html index.htm default.html default.htm; 

     fastcgi_buffers 8 16k; 
     fastcgi_buffer_size 32k; 
     fastcgi_read_timeout 180; 

     location ~ \.php$ { 
      fastcgi_pass php7:9000; 
      fastcgi_index index.php; 
      include fastcgi_params; 
      fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name; 
      fastcgi_param SERVER_NAME $server_name; 
     } 
} 

這要求SITE2是200與適當的反應OK:

curl -X GET \ 
    http://localhost:9999/index.php \ 
    -H 'host: site2' 

而且對於請求SITE1 :

curl -X GET \ 
    http://localhost:9999/index.php \ 
    -H 'host: site1' 

在Nginx的容器日誌我總是看到:

2017年4月26日21點18分27秒[錯誤] 7#7:* 1連接()失敗(111:連接 拒絕),同時連接到上游,客戶端:172.17.0.1,服務器: site1,請求:「GET /index.php HTTP/1.1」,上游: 「fastcgi://172.17.0.3:9001」,主機:「site1」

任何想法如何解決這個問題將不勝感激。

回答

3

好吧,我解決了它,很愚蠢的錯誤。出於某種原因,我假設如果我暴露端口9001是這樣的:

docker run --name php5 \ 
    -v /html1:/var/www/html/site1 \ 
    -d -p 9001:9000 php:5.6-fpm 

然後該端口9001應當在其被連接到所述其它的php5容器Nginx的容器中使用。這是錯誤的,因爲暴露的網絡連接不同於鏈接的網絡連接。

所以正確的site1的配置應該是這樣的(端口也是9000):

server { 
     listen 80 default_server; 

     server_name site1; 

     root /var/www/html/site1/; 
     index index.php index.html index.htm default.html default.htm; 

     fastcgi_buffers 8 16k; 
     fastcgi_buffer_size 32k; 
     fastcgi_read_timeout 180; 

     location ~ \.php$ { 
      fastcgi_pass php5:9000; # <-- BOOOM! 
      fastcgi_index index.php; 
      include fastcgi_params; 
      fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name; 
      fastcgi_param SERVER_NAME $server_name; 
     } 
}