我正在Dockerising我的webserver/php工作流。
但是因为我使用的是Windows,所以我需要使用虚拟机。我选择了boot2docker,这是一个运行在Virtualbox上的Tiny Core Linux,适合Docker。
我选择了三个容器:
- nginx:官方的nginx容器; jprjr/php-fpm: php-fpm容器;
- mysql:用于数据库。
在boot2docker中,/www/
包含我的web项目和conf/
,其中有以下树:
conf
│
├───fig
│ fig.yml
│
└───nginx
nginx.conf
servers-global.conf
servers.conf
因为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
, php
中位于/srv/http/lab/test_autoload/index.php
。
我认为问题来自root
和/或fastcgi_param SCRIPT_FILENAME
,但我不知道如何解决。
我尝试了很多事情,例如修改root
s,使用rewrite
规则,添加/删除一些/
s等,但没有任何改变。
再次为所有这些配置道歉,但我认为有必要描述我所处的环境。
我终于找到答案了
变量$fastcgi_script_name
没有考虑到根(逻辑),否则它会包括www/
。这意味着全局文件不能工作。示例:
# "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/
,我需要在两个不同的地方(根和fastcgi_param)写,所以我决定自己写一个小的PHP脚本,使用.json
,并把它变成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配置中,同样的事情,缺少mime类型,default_type,配置日志sendfile at on for boot2docker。
你的问题显然不是Docker的问题,而是nginx配置的问题。在使用fig之前,您是否使用运行docker运行测试您的应用程序?