2016-11-17 18 views
0

我在設置多碼頭集裝箱環境時遇到了問題。 這個想法是非常標準:帶有PHP7 fpm和nginx的多碼頭容器

  • 一個容器有phpfpm運行
  • 另一種是nginx的代理

我phpfpm泊塢文件很簡單,只要:

FROM php:7.0-fpm 

# install the PHP extensions we need 
RUN apt-get update && apt-get install -y libpng12-dev libjpeg-dev && rm -rf /var/lib/apt/lists/* \ 
    && docker-php-ext-configure gd --with-png-dir=/usr --with-jpeg-dir=/usr \ 
    && docker-php-ext-install gd mysqli opcache 

# set recommended PHP.ini settings 
# see https://secure.php.net/manual/en/opcache.installation.php 
RUN { \ 
     echo 'opcache.memory_consumption=128'; \ 
     echo 'opcache.interned_strings_buffer=8'; \ 
     echo 'opcache.max_accelerated_files=4000'; \ 
     echo 'opcache.revalidate_freq=2'; \ 
     echo 'opcache.fast_shutdown=1'; \ 
     echo 'opcache.enable_cli=1'; \ 
    } > /usr/local/etc/php/conf.d/opcache-recommended.ini 

VOLUME /var/www/html 

CMD ["php-fpm"] 

而Nginx更是如此:

FROM nginx 

COPY conf.d/* /etc/nginx/conf.d/ 

conf.d文件夾中是一個單一的文件default.conf

server { 
    listen 80; 
    server_name priz-local.com; 
    root /var/www/html; 

    index index.php; 

    location/{ 
     proxy_pass http://website:9000; 
     proxy_set_header Connection ""; 
     proxy_http_version 1.1; 
     proxy_set_header  Host   $host; 
     proxy_set_header  X-Real-IP  $remote_addr; 
     proxy_set_header  X-Forwarded-For $proxy_add_x_forwarded_for; 
    } 
} 

和碼頭工人,compose.yml

website: 
    build: ./website/ 
    ports: 
    - "9000:9000" 
    container_name: website 
    external_links: 
    - mysql:mysql 
nginx-proxy: 
    build: ./proxy/ 
    ports: 
    - "8000:80" 
    container_name: proxy 
    links: 
     - website:website 

這個確切的設置工作完全在AWS彈性魔豆。然而,在我的本地碼頭工人,我收到錯誤,如:

2016年11月17日9點55分36秒[錯誤] 6#6:* 1 connect()的失敗(111:連接 拒絕),而連接到上游,客戶端:172.17.0.1,服務器: priz-local.com,請求:「GET/HTTP/1.1」,上游: 「http://127.0.0.1:9000/」,主機:「priz-local.com:8888」 172.17。 (Mozilla/5.0(Macintosh; Intel Mac OS X 10_12_1) AppleWebKit/537.36(KHTML,0.1 - - [17/Nov/2016:09:55:36 +0000]「GET/HTTP/1.1」502 575「 - 」像Gecko)Chrome/54.0.2840.71 Safari/537.36「」 - 「

UPDATE 如果我登錄到代理容器和另一個我得到這個嘗試捲曲:

[email protected]:/# curl http://website 
curl: (7) Failed to connect to website port 80: Connection refused 
[email protected]:/# curl http://website:9000 
curl: (56) Recv failure: Connection reset by peer 

我想另一件事是:

server { 
    listen 80; 
    server_name priz-local.com; 
    root /var/www/html; 

    #index index.php; 
    #charset UTF-8; 

    #gzip on; 
    #gzip_http_version 1.1; 
    #gzip_vary on; 
    #gzip_comp_level 6; 
    #gzip_proxied any; 
    #gzip_types text/plain text/xml text/css application/x-javascript; 

    location = /robots.txt { 
     allow all; 
     log_not_found off; 
     access_log off; 
    } 

    location /nginx_status { 
     stub_status on; 
     access_log off; 
    } 

    location/{ 
     try_files $uri $uri/ /index.php?q=$uri&$args; 
    } 

    location ~ \.php$ { 

     set $nocache ""; 
     if ($http_cookie ~ (comment_author_.*|wordpress_logged_in.*|wp-postpass_.*)) { 
      set $nocache "Y"; 
     } 

     fastcgi_pass website:9000; 
     fastcgi_index index.php; 
     fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name; 
     fastcgi_intercept_errors on; 
     include fastcgi_params; 

     #fastcgi_cache_use_stale error timeout invalid_header http_500; 
     #fastcgi_cache_key $host$request_uri; 
     #fastcgi_cache example; 
     #fastcgi_cache_valid 200 1m; 
     #fastcgi_cache_bypass $nocache; 
     #fastcgi_no_cache $nocache; 
    } 

    location ~* \.(js|css|png|jpg|jpeg|gif|ico)$ { 
     allow all; 
     expires max; 
     log_not_found off; 

     fastcgi_pass wordpress:9000; 
     fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name; 
     fastcgi_intercept_errors on; 
     include fastcgi_params; 
    } 
} 

該網站開始工作,但所有資源(js | css | png | jpg | jpeg | gif | ico)現在都返回403

我缺少什麼?

+0

讓我猜測。你的本地環境是mac嗎? – cari

+0

是的,不知道你是否將其稱爲一般問題... –

+0

'Mozilla/5.0(Macintosh;英特爾Mac OS X 10_12_1)'沒有太多的猜測:p – johnharris85

回答

0

經過與R0MANARMY的長時間交談和他的很多幫助,我想我終於明白了問題的根源。

這裏的主要問題是我沒有使用碼頭工具,因爲它的目的是工作。

另一個原因是,fpm不是一個web服務器,代理它的唯一方法是通過fastcgi(或者不是唯一的,但簡單的proxy_pass在這種情況下不起作用)。

因此,設置它的正確的方法是:

  1. 安裝編碼量成兩個容器。
  2. 配置fastcgi通過nginx將php腳本導入php容器
  3. 配置虛擬主機直接通過nginx提供靜態資源。

這裏有幾個如何做到這一點的例子:

http://geekyplatypus.com/dockerise-your-php-application-with-nginx-and-php7-fpm/

https://ejosh.co/de/2015/08/wordpress-and-docker-the-correct-way/

UPDATE 添加爲我工作的實際解決方案:

爲了更快週轉時,我決定用戶docker-compose和docker-compose.yml的樣子這個:

website: 
    build: ./website/ 
    container_name: website 
    external_links: 
    - mysql:mysql 
    volumes: 
    - ~/Dev/priz/website:/var/www/html 
    environment: 
    WORDPRESS_DB_USER: ** 
    WORDPRESS_DB_PASSWORD: *** 
    WORDPRESS_DB_NAME: *** 
    WORDPRESS_DB_HOST: *** 
proxy: 
    image: nginx 
    container_name: proxy 
    links: 
    - website:website 
    ports: 
    - "9080:80" 
    volumes: 
    - ~/Dev/priz/website:/var/www/html 
    - ./deployment/proxy/conf.d/default.conf:/etc/nginx/conf.d/default.conf 

現在,這裏最重要的一條信息是,我正在爲這兩個容器安裝完全相同的代碼。原因是,因爲fastcgi無法提供靜態文件(至少據我瞭解),所以想法是直接通過nginx提供服務。

default.conf文件看起來像這樣:

server { 
    listen 80; 
    server_name localhost; 
    root /var/www/html; 

    index index.php; 

    location = /robots.txt { 
     allow all; 
     log_not_found off; 
     access_log off; 
    } 

    location /nginx_status { 
     stub_status on; 
     access_log off; 
    } 

    location/{ 
     try_files $uri $uri/ /index.php?q=$uri&$args; 
    } 

    location ~ \.php$ { 
     fastcgi_split_path_info ^(.+\.php)(/.+)$; 
     fastcgi_pass website:9000; 
     fastcgi_index index.php; 
     fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name; 
     #fastcgi_param PATH_INFO $fastcgi_path_info; 
     fastcgi_intercept_errors on; 
     include fastcgi_params; 
    } 
} 

所以,這個配置,通過PHP請求代理由FPM容器進行處理,而其他一切都從本地安裝量服用。

就是這樣。我希望它能幫助別人。

與它唯一的幾個問題:

  1. 只是有時http://localhost:9080下載index.php文件,而不是從PHP腳本到外面的世界
  2. cURL'ing執行它,需要很長一段時間不知道如何甚至在這一點上進行調試。