dpd3982 2017-04-26 21:40
浏览 165

在Docker中通过Nginx实现PHP的多个版本

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

And for PHP7:

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

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

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

}

with site1 config:

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

and site2 config:

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

This request to site2 is 200 OK with proper response:

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

And for the request to site1:

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

In the Nginx container logs I always see:

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.

  • 写回答

1条回答 默认 最新

  • douxi1738 2017-04-27 16:00
    关注

    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
    

    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.

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

报告相同问题?

悬赏问题

  • ¥15 飞机曲面部件如机翼,壁板等具体的孔位模型
  • ¥15 vs2019中数据导出问题
  • ¥20 云服务Linux系统TCP-MSS值修改?
  • ¥20 关于#单片机#的问题:项目:使用模拟iic与ov2640通讯环境:F407问题:读取的ID号总是0xff,自己调了调发现在读从机数据时,SDA线上并未有信号变化(语言-c语言)
  • ¥20 怎么在stm32门禁成品上增加查询记录功能
  • ¥15 Source insight编写代码后使用CCS5.2版本import之后,代码跳到注释行里面
  • ¥50 NT4.0系统 STOP:0X0000007B
  • ¥15 想问一下stata17中这段代码哪里有问题呀
  • ¥15 flink cdc无法实时同步mysql数据
  • ¥100 有人会搭建GPT-J-6B框架吗?有偿