2017-05-25 65 views
1

我嘗試了許多很多建議,但無法使其工作。 我想創建一個docker-compose.yml文件與NGINX-PHP合作。讓PHP和Nginx Docker鏡像一起工作

這裏是我做了什麼:

version: "2" 

services: 
    nginx: 
    image: nginx:latest 
    restart: always 
    ports: 
     - "80:80" 
     - "443:443" 
    links: 
     - php 
    depends_on: 
     - php 
    expose: 
     - "80" 
     - "443" 
    volumes: 
     - ./www:/var/www/html 
     - ./config/nginx/site.conf:/etc/nginx/sites-available/default 
     - ./config/nginx/site.conf:/etc/nginx/sites-enabled/default 

    php: 
    image: php:7-fpm 
    restart: always 
    volumes: 
     - ./www:/var/www/html 

Docker圖像無差錯運行,但是當我要訪問Nginx,我得到這個:

歡迎nginx的!

如果您看到此頁面,說明nginx web服務器已成功安裝 並正在運行。需要進一步的配置。

有關在線文檔和支持請參考nginx.org。 nginx.com提供商業支持。

感謝您使用nginx。

我試圖登錄到兩個圖像並檢查卷。我還檢查了是否可以從nginx ping php。一切似乎是罰款......

這裏是我的Nginx站點配置:

server { 
    server_name ncp-docker; 

    listen 80; 
    index index.php index.html index.htm; 
    root /var/www/html; 

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

    location ~ \.php$ { 
     try_files $uri =404; 
     fastcgi_split_path_info ^(.+\.php)(/.+)$; 
     include fastcgi_params; 
     fastcgi_pass php:9000; 
     fastcgi_index index.php; 
     fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name; 
     fastcgi_param PATH_INFO $fastcgi_path_info; 
    } 

    location ~ /(inc|uploads/avatars) { 
     deny all; 
    } 
} 
+1

可能不相關,但你可能想更新來組成版本3,這是當前版本https://docs.docker.com/compose/compose-file/ –

回答

4

你默認爲默認服務器配置,所以你需要覆蓋默認nginx的虛擬主機:

- ./config/nginx/site.conf:/etc/nginx/conf.d/default.conf 

使用它並刪除網站啓用和網站可用卷

2

我想你可能會接近這個錯誤。

nginx配置不應該直接運行PHP。 PHP在單獨的容器上運行。

相反,您應該有PHP應用程序的nginx反向代理。

是這樣的:


server { 
    listen 80; 
    server_name yourdomain.example.com; 
    location/{ 
     proxy_pass http://php:9000/; 
     proxy_redirect off; 
     proxy_set_header X-Real-IP $remote_addr; 
     proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for; 
     proxy_set_header Host $http_host; 
     proxy_set_header X-NginX-Proxy true; 
    } 
} 

有可能是一些細節,你需要添加/修改......但是這是我砍&粘貼當我需要它的基本的反向代理,在nginx的

+1

端口9000不是HTTP端口,它是CGI通信,所以你不能proxy_pass它。因爲他實現了fastcgi_pass。 – Robert

+0

啊 - 我無法記住的php的細節:)謝謝@Robert! –

1

由於Robert建議您必須覆蓋默認的nginx虛擬主機,不過,我想補充一點,您還可以爲nginx容器添加一個額外的Dockerfile並避免使用卷。在你的docker-compose.yml你必須改變image: nginx:latestbuild: ./nginx/。對於Dockerfile這樣的事情應該做的伎倆:

FROM nginx:latest 
RUN rm /etc/nginx/conf.d/default.conf 
ADD conf.d/ /etc/nginx/conf.d/ #Replace the default nginx virtual host 

我覺得這是更容易,因爲以後你可以輕鬆地添加更多的東西到容器的HTTPS連接例如SSL證書:

ADD ssl/ /etc/nginx/ssl/  

這將爲您節省使用卷的空間,並將使您的生活更輕鬆,以適應未來的nginx設置。