2015-07-10 39 views
5

我開始與Dockernginx,我試圖設置兩個容器環境中運行:一方面簡單的搬運工,組成:nginx的和PHP

  • nginx:latest
  • php:fpm在另一邊

我遇到了php-fpm的麻煩:我總是得到一個502錯誤的網關錯誤。

我的設置非常簡單($TEST_DIR是我的工作目錄)。

我的碼頭工人組成的配置TEST_DIR/docker-compose.yml

nginx: 
    image: nginx 
    ports: 
     - "8080:80" 
    volumes: 
     - ./www:/usr/share/nginx/html 
     - ./conf/nginx.conf:/nginx.conf 
     - ./logs/nginx:/var/log/nginx 
    links: 
     - php:php 
    command: nginx -c /nginx.conf 

php: 
    image: php:fpm 
    ports: 
     - "9000:9000" 
    volumes: 
     - ./www:/var/www/html 

nginx的配置$TEST_DIR/conf/nginx.conf

user nginx; 
worker_processes 1; 
pid  /var/run/nginx.pid; 

events { 
    worker_connections 2048; 
    multi_accept on; 
    use epoll; 
} 

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 /var/log/nginx/access.log main; 

    server_tokens off; 
    sendfile on; 
    tcp_nopush on; 
    tcp_nodelay on; 
    keepalive_timeout 15; 
    types_hash_max_size 2048; 
    include /etc/nginx/mime.types; 
    default_type application/octet-stream; 
    access_log off; 
    error_log off; 
    gzip on; 
    gzip_disable "msie6"; 
    open_file_cache max=100; 


    upstream php-upstream { 
     server php:9000; 
    } 

    server { 
     listen  80; 
     server_name localhost; 

     location/{ 
      root /usr/share/nginx/html; 
      index index.html index.htm; 
     } 

     # Pass PHP scripts to PHP-FPM 
     location ~* \.php$ { 
      fastcgi_pass php-upstream; 

      include   fastcgi_params; 
      fastcgi_param SCRIPT_FILENAME /var/www/html$fastcgi_script_name; 
      fastcgi_param SCRIPT_NAME  $fastcgi_script_name; 
      fastcgi_param HTTPS off; 
     } 

     error_log /var/log/nginx/php_error.log; 
     access_log /var/log/nginx/php_access.log; 
    } 
} 

daemon off; 

然後,我把我的PHP內容在同一目錄作爲我docker-compose.yml

$TEST_DIR/www/test.php

<?php phpinfo(); ?> 

如果我使用docker-compose up啓動的基礎設施,然後去localhost:8080/test.php,然後我得到502網關錯誤 和nginx的以下錯誤:

[error] 6#6: *1 connect() failed (113: No route to host) while connecting to upstream, client: 172.17.42.1, server: localhost, request: "GET /phpinsfo2.php HTTP/1.1", upstream: "fastcgi://172.17.0.221:9000", host: "localhost:8080" 

是什麼原因造成的錯誤?

+1

我不知道它是否解決了問題,但它似乎沒有在量的定義爲PHP的容器一個錯字'./www:/ var/wwww/html' –

+1

我驗證了修復'./www:/ var/wwww/html'中的拼寫錯誤可以讓我看到'/ test.php'頁面的內容。儘管你的錯誤'connect()失敗(113:沒有路由到主機)'可能表明我無法重現的問題。您是否正在使用碼頭工人和碼頭工人的最新版本? – Thomasleveil

+0

我正在使用** docker 1.7.0 build 0baf609 **和** docker-compose 1.3.1 **。 事件與錯字固定的,我仍然有同樣的問題: nginx的服務器服務於正確的靜態文件,但是PHP文件不被髮送到PHP-FPM,我仍然得到502錯誤網關錯誤 的一點是,我不明白,如果請求路由到PHP FPM或不... ...我怎麼測試? – Clement

回答

2

我終於設法使其工作。

的問題是,我的主機(Fedora的21)有啓用了防火牆。

這樣算下來:systemctl stop firewalld解決我的問題。

顯然,這是在Red Hat Linux的一個衆所周知的問題:
Bug 1098281 - Docker interferes with firewall initialisation via firewalld

+0

我以爲Docker正在更新iptables作爲它的網絡魔法的一部分。我想還有幾個問題需要解決。這幫助了我。感謝發佈! –

+0

禁用firewalld仍然需要嗎?據我所知該bug已關閉。 – MauganRa