Docker中的nginx和php-fpm
作者:互联网
我正在使用Dockerising我的webserver / php工作流程.
但因为我在Windows上,我需要使用虚拟机.我选择了boot2docker,这是一个在Virtualbox中运行并适用于Docker的Tiny Core Linux.
我选择了三个容器:
> 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中创建的)但是如果我尝试访问实验室.dev / test_autoload /,我找不到文件..我知道这来自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.php中.
我相信问题来自root和/或fastcgi_param SCRIPT_FILENAME,但我不知道如何解决它.
我尝试了很多东西,比如修改根,使用重写规则,添加/删除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;
}
}
这意味着我无法在一个地方编写实验室,我需要在两个不同的地方(root和fastcgi_param)编写,所以我决定自己编写一个使用.json的小PHP脚本并将其转换为服务器.conf文件.如果有人想看看它,请问,分享它会很愉快.
标签:nginx,php,docker,boot2docker,fig 来源: https://codeday.me/bug/20190628/1316881.html