簡體   English   中英

Docker中的nginx和php-fpm

[英]nginx and php-fpm in Docker

我正在使用Dockerising我的webserver / php工作流程。

但因為我在Windows上,我需要使用虛擬機。 我選擇了boot2docker,這是一個在Virtualbox中運行並適用於Docker的Tiny Core Linux。

我選擇了三個容器:

在boot2docker中, /www/包含我的web項目和conf/ ,它有以下樹:

conf
│
├───fig
│       fig.yml
│
└───nginx
        nginx.conf
        servers-global.conf
        servers.conf

因為docker docker-compose不能用於boot2docker,所以我必須使用fig來自動化所有東西。 這是我的fig.xml

mysql:
 image: mysql
 environment:
  - MYSQL_ROOT_PASSWORD=root
 ports:
  - 3306:3306

php:
 image: jprjr/php-fpm
 links:
  - mysql:mysql
 volumes:
  - /www:/srv/http:ro
 ports:
  - 9000:9000

nginx:
 image: nginx
 links:
  - php:php
 volumes:
  - /www:/www:ro
 ports:
  - 80:80
 command: nginx -c /www/conf/nginx/nginx.conf

這是我的nginx.conf

daemon off;

user  nginx;
worker_processes  1;

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

events {
    worker_connections  1024;
}

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

    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;

    sendfile        off;

    keepalive_timeout  65;

    index    index.php index.html index.htm;

    include /www/conf/nginx/servers.conf;

    autoindex on;
}

servers.conf

server {
    server_name lab.dev;
    root /www/lab/;

    include /www/conf/nginx/servers-global.conf;
}

# Some other servers (vhosts)

servers-global.conf

listen 80;

location ~* \.php$ {
    fastcgi_index   index.php;
    fastcgi_pass    php:9000;
    include         /etc/nginx/fastcgi_params;
    fastcgi_param   SCRIPT_FILENAME     /srv/http$fastcgi_script_name;
}

所以現在的問題(抱歉所有配置,我認為需要清楚地解釋問題):如果我訪問lab.dev ,沒問題(這表明主機是在Windows中創建的)但是如果我嘗試訪問lab.dev/test_autoload/ ,我File not found. 我知道這來自php-fpm無法訪問文件,nginx日志確認了這一點:

nginx_1 | 2015/05/28 14:56:02 [error] 5#5: *3 FastCGI sent in stderr:
"Primary script unknown" while reading response header from upstream,
client: 192.168.59.3, server: lab.dev, request: "GET /test_autoload/ HTTP/1.1",
upstream: "fastcgi://172.17.0.120:9000", host: "lab.dev", referrer: "http://lab.dev/"

我知道在兩個容器的lab/test_autoload/中都有一個index.php ,我已經檢查過了。 nginx ,它位於/www/lab/test_autoload/index.php/srv/http/lab/test_autoload/index.phpphp

我相信問題來自root和/或fastcgi_param SCRIPT_FILENAME ,但我不知道如何解決它。

我嘗試過很多東西,比如修改root ,使用rewrite規則,添加/刪除some / s等,但沒有任何改變。

再次,抱歉所有這個配置,但我認為需要描述我所處的環境。

我終於找到了答案。

變量$fastcgi_script_name沒有考慮根(邏輯,因為它包含www/ otherwise。這意味着全局文件不能工作。例如:

# "generated" vhost
server {
    server_name lab.dev;
    root /www/lab/;

    listen 80;

    location ~* \.php$ {
        fastcgi_index   index.php;
        fastcgi_pass    php:9000;
        include         /etc/nginx/fastcgi_params;
        fastcgi_param   SCRIPT_FILENAME     /srv/http$fastcgi_script_name;
        # I need to add /lab after /srv/http because it requests PHP to look at the root of the web files, not in the lab/ folder
        # fastcgi_param   SCRIPT_FILENAME     /srv/http/lab$fastcgi_script_name;
    }
}

這意味着我無法在一個地方編寫lab/ ,我需要在兩個不同的地方(root和fastcgi_param)編寫,所以我決定自己編寫一個使用.json的小PHP腳本並將其轉換為servers.conf文件。 如果有人想看看它,請問,分享它會很愉快。

這里有一個錯誤:

fastcgi_param   SCRIPT_FILENAME     /srv/http/$fastcgi_script_name;

好的方面是:

fastcgi_param   SCRIPT_FILENAME     /srv/http$fastcgi_script_name;

這不是一回事。

你的nginx.conf是完全空的,守護進程在哪里關閉; 用戶nginx行,worker_processes等.nginx在運行http之前需要一些配置文件。

在http conf中,同樣的事情是,缺少mime類型,default_type,配置日志sendfile at on for boot2docker。

你的問題顯然不是Docker的問題,而是使用nginx配置。 在使用無花果之前,您是否在運行docker run時測試了應用程序? 它有用嗎?

暫無
暫無

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

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