通过Docker中的Nginx的多个版本的PHP [英] Multiple versions of PHP through Nginx in Docker

查看:377
本文介绍了通过Docker中的Nginx的多个版本的PHP的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我为PHP5.6运行了两个docker容器:

I run two docker containers for PHP5.6:

docker run --name php5 \
    -v /html1:/var/www/html/site1 \
    -d -p 9001:9000 php:5.6-fpm

对于PHP7:

docker run --name php7 \
  -v /html2:/var/www/html/site2 \
  -d -p 9000:9000 php:7-fpm

我用Nginx运行Docker容器:

I run Docker container with Nginx:

docker run --name nginx-cache \
  -v /nginx.conf:/etc/nginx/nginx.conf \
  -v /nginx/html1:/var/www/html/site1 \
  -v /nginx/html2:/var/www/html/site2 \
  -v /sites-enabled:/etc/nginx/sites-enabled/ \
  --link php5 --link php7 -d -p 9999:80 nginx

nginx.conf

user  nginx;
worker_processes  1;

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

events {
    worker_connections  1024;
}

http {
    sendfile on;

    gzip              on;
    gzip_http_version 1.0;
    gzip_proxied      any;
    gzip_min_length   500;
    gzip_disable      "MSIE [1-6]\.";
    gzip_types        text/plain text/xml text/css
                      text/comma-separated-values
                      text/javascript
                      application/x-javascript
                      application/atom+xml;
    gzip_disable      "msie6";


    ##
    # Basic Settings
    ##

    server_names_hash_bucket_size 64;

    tcp_nopush on;
    tcp_nodelay on;
    keepalive_timeout 65;
    types_hash_max_size 2048;
    client_body_buffer_size 128k;

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

    ##
    # Logging Settings
    ##

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

    ##
    # Virtual Host Configs
    ##

    include /etc/nginx/conf.d/*.conf;
    include /etc/nginx/sites-enabled/*;

}

具有site1配置:

server {
        listen 80 default_server;

        server_name site1;

        root /var/www/html/site1/;
        index index.php index.html index.htm default.html default.htm;

        fastcgi_buffers 8 16k;
        fastcgi_buffer_size 32k;
        fastcgi_read_timeout 180;

        location ~ \.php$ {
            fastcgi_pass  php5:9001;
            fastcgi_index index.php;
            include fastcgi_params;
            fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
            fastcgi_param SERVER_NAME $server_name;
        }
}

和site2配置:

server {
        listen 80;

        server_name site2;

        root /var/www/html/site2/;
        index index.php index.html index.htm default.html default.htm;

        fastcgi_buffers 8 16k;
        fastcgi_buffer_size 32k;
        fastcgi_read_timeout 180;

        location ~ \.php$ {
            fastcgi_pass  php7:9000;
            fastcgi_index index.php;
            include fastcgi_params;
            fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
            fastcgi_param SERVER_NAME $server_name;
        }
}

对site2的此请求为200 OK,且响应正确:

This request to site2 is 200 OK with proper response:

curl -X GET \
  http://localhost:9999/index.php \
  -H 'host: site2'

对于site1的请求:

And for the request to site1:

curl -X GET \
  http://localhost:9999/index.php \
  -H 'host: site1'

在Nginx容器日志中,我总是看到:

In the Nginx container logs I always see:


2017/04/26 21:18:27 [错误] 7#7:* 1在连接到上游时connect()失败(111:Connection
被拒绝),客户端:172.17.0.1,服务器:
site1,请求: GET /index.php HTTP / 1.1,上游:
fastcgi://172.17.0.3:9001,主机: site1

2017/04/26 21:18:27 [error] 7#7: *1 connect() failed (111: Connection refused) while connecting to upstream, client: 172.17.0.1, server: site1, request: "GET /index.php HTTP/1.1", upstream: "fastcgi://172.17.0.3:9001", host: "site1"

任何解决此问题的想法将不胜感激。

Any ideas how to troubleshoot this would be highly appreciated.

推荐答案

好,我解决了,这是一个非常愚蠢的错误。由于某种原因,我假设如果我这样暴露9001端口:

Ok I solved it, quite dummy mistake. For some reason I assumed that if I expose the port 9001 like this:

docker run --name php5 \
    -v /html1:/var/www/html/site1 \
    -d -p 9001:9000 php:5.6-fpm

然后应在与其他php5容器连接的Nginx容器中使用此端口9001。这是错误的,因为公开的网络连接与链接的网络连接不同。

then this port 9001 should be used in Nginx container which is connected to the other php5 container. This is wrong because the exposed network connection is different than linked one.

因此正确的site1配置应如下所示(端口也是9000):

So the proper site1 config should be like this (the port is also 9000):

server {
        listen 80 default_server;

        server_name site1;

        root /var/www/html/site1/;
        index index.php index.html index.htm default.html default.htm;

        fastcgi_buffers 8 16k;
        fastcgi_buffer_size 32k;
        fastcgi_read_timeout 180;

        location ~ \.php$ {
            fastcgi_pass  php5:9000; # <-- BOOOM!
            fastcgi_index index.php;
            include fastcgi_params;
            fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
            fastcgi_param SERVER_NAME $server_name;
        }
}

这篇关于通过Docker中的Nginx的多个版本的PHP的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆