繁体   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