【问题标题】:Multiple versions of PHP through Nginx in Docker在 Docker 中通过 Nginx 实现 PHP 的多个版本
【发布时间】:2017-04-26 21:40:15
【问题描述】:

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

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 容器:

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/*;

}

使用站点 1 配置:

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;
        }
}

和站点 2 配置:

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 并得到正确响应:

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

对于site1的请求:

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

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

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

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

【问题讨论】:

    标签: php docker fastcgi


    【解决方案1】:

    好的,我解决了,相当愚蠢的错误。出于某种原因,我假设如果我像这样公开端口 9001:

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

    那么这个端口 9001 应该在连接到另一个 php5 容器的 Nginx 容器中使用。这是错误的,因为暴露的网络连接与链接的不同。

    所以正确的site1配置应该是这样的(端口也是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;
            }
    }
    

    【讨论】:

    • 所以你是说不需要为每个 PHP 版本分配不同的端口,只需按原样使用服务名称和默认端口?
    猜你喜欢
    • 2021-06-01
    • 1970-01-01
    • 2020-05-19
    • 1970-01-01
    • 2019-07-21
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2016-03-02
    相关资源
    最近更新 更多