繁体   English   中英

Docker php curl 连接被拒绝

[英]Docker php curl connection refused

[下面的演示]

问候开发人员,最近我正在尝试对 php 网站进行 docker 化。 但是当我尝试从一个到另一个 curl 时连接被拒绝。

基本上我有三个站点:

  • api
  • 第一站
  • 第二站点

问题是connection is refused when I try to call api from first-site.

./docker-compose.yml

version: "3.1"
services:
  www:
    build: ./www/.
    ports:
      - "0.0.0.0:8080:80"
      - "0.0.0.0:443:443"
    volumes:
      - ./htdocs:/var/www
      - ./www/conf:/etc/httpd/conf
    networks:
      - local

  php:
    build: ./php/.
    ports:
      - '9000'
    volumes:
      - ./htdocs:/var/www
    networks:
      - local

networks:
  local:
    driver: bridge

./www/Dockerfile

FROM centos:8.2.2004
MAINTAINER kenphanith <https://github.com/kenphanith>

LABEL description="develop environment"
LABEL httpd_version="2.4"

RUN dnf -yq module install httpd:2.4 && \
    dnf -yq install epel-release && \
    dnf -yq install mod_perl ImageMagick && \
    dnf clean all
RUN sscg -q \
    --cert-file     /etc/pki/tls/certs/localhost.crt \
    --cert-key-file /etc/pki/tls/private/localhost.key \
    --ca-file       /etc/pki/tls/certs/localhost.crt \
    --lifetime      365 \
    --hostname      localhost \
    --email         root@localhost


EXPOSE 80/tcp 443/tcp
WORKDIR /var/www
CMD ["/usr/sbin/apachectl","-D","FOREGROUND"]

./www/conf我有

  • /vhost.d
    • /ssl
    • 第一个站点.conf
    • secondsite.conf
    • api.conf
  • httpd.conf

第一个站点.conf

<VirtualHost *:80>
    ServerName firstsite.com
    DocumentRoot /var/www/firstsite

    <Directory "/var/www/firstsite">
        Options -Indexes +FollowSymLinks
        DirectoryIndex index.php index.html

        <FilesMatch \.php$>
            SetHandler "proxy:fcgi://php:9000"
        </FilesMatch>
    </Directory>

</VirtualHost>

<VirtualHost *:443>
    ServerName firstsite
    DocumentRoot /var/www/firstsite

    SSLEngine on
    SSLCertificateFile /etc/httpd/conf/vhost.d/ssl/site.crt
    SSLCertificateKeyFile /etc/httpd/conf/vhost.d/ssl/site.key
    SSLCertificateChainFile /etc/httpd/conf/vhost.d/ssl/site.mid.crt

    <Directory "/var/www/ap.subaru.jp">
        Options -Indexes +FollowSymLinks
        DirectoryIndex index.php index.html

        <FilesMatch \.php$>
            SetHandler "proxy:fcgi://php:9000"
        </FilesMatch>
    </Directory>

</VirtualHost>

api.conf

<VirtualHost *:80>
    ServerName api.com
    DocumentRoot /var/www/api

    <Directory "/var/www/api">
        AllowOverride All
        Options -Indexes +FollowSymLinks
        DirectoryIndex index.php index.html

        <FilesMatch \.php$>
            SetHandler "proxy:fcgi://php:9000"
        </FilesMatch>
    </Directory>

</VirtualHost>

<VirtualHost *:443>
    ServerName api
    DocumentRoot /var/www/api

    SSLEngine on
    SSLCertificateFile /etc/httpd/conf/vhost.d/ssl/site.crt
    SSLCertificateKeyFile /etc/httpd/conf/vhost.d/ssl/site.key
    SSLCertificateChainFile /etc/httpd/conf/vhost.d/ssl/site.mid.crt

    <Directory "/var/www/api">
        AllowOverride All
        Options -Indexes +FollowSymLinks
        DirectoryIndex index.php index.html

        <FilesMatch \.php$>
            SetHandler "proxy:fcgi://php:9000"
        </FilesMatch>
    </Directory>

</VirtualHost>

secondsite.conf

<VirtualHost *:80>
    ServerName secondsite.com
    Include conf/vhost.d/secondsite.inc
</VirtualHost>

<VirtualHost *:443>
    ServerName secondsite.com

    SSLEngine on
    SSLCertificateFile /etc/httpd/conf/vhost.d/ssl/site.crt
    SSLCertificateKeyFile /etc/httpd/conf/vhost.d/ssl/site.key
    SSLCertificateChainFile /etc/httpd/conf/vhost.d/ssl/site.mid.crt

    Include conf/vhost.d/secondsite.inc
</VirtualHost>

./php/Dockerfile

FROM php:7.3.19-fpm-alpine
MAINTAINER KenPhanith <https://github.com/kenphanith>

LABEL description="develop environment"
LABEL httpd_version="7.3.19"

RUN apk update && apk add bash

RUN apk add --no-cache --update libpq && \
    docker-php-ext-install pdo_mysql mysqli
RUN cp $PHP_INI_DIR/php.ini-development $PHP_INI_DIR/php.ini && \
    sed -ie 's/^short_open_tag = Off$/short_open_tag = ON/g' $PHP_INI_DIR/php.ini
RUN sed -ie 's/;security.limit_extensions = .php .php3 .php4 .php5 .php7/security.limit_extensions = .php .php3 .html/g' /usr/local/etc/php-fpm.d/www.conf

./firstsite/index.php我有这个代码

<?php
try {
    date_default_timezone_set('Asia/Tokyo');

    $base = "https://api.com"
    $url = $base."/api/detailparam=1&param2=".$_REQUEST['param2']."&param3=".$_REQUEST['param3'];

    $ch = curl_init();

    // Check if initialization had gone wrong*
    if ($ch === false) {
        throw new Exception('failed to initialize');
    }
    curl_setopt($ch, CURLOPT_URL, $url);
    curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
    curl_setopt($ch,CURLOPT_SSL_VERIFYPEER,true);
    curl_setopt($ch, CURLOPT_VERBOSE, true);
    $response = curl_exec($ch);
    // Check the return value of curl_exec(), too
    if ($response === false) {
        throw new Exception(curl_error($ch), curl_errno($ch));
    }
    curl_close($ch);

    $json = json_decode($response,true);

    if ($json==null) {
        echo "Can't connect server";
        exit;
    }
} catch (Exception $e) {
    trigger_error(sprintf(
        'Curl failed with error #%d: %s',
        $e->getCode(), $e->getMessage()),
        E_USER_ERROR);
}
?>

当我执行这段代码时,我得到了

致命错误:Curl 失败,错误 #56:代理 CONNECT 在第 xx 行的 /var/www/api/xxxxxxp 中中止

我自己的假设是,我在 docker-compose.yml 中拥有的phpwww容器没有连接或链接在同一个网络上,但直到现在我才弄清楚解决方案。 请帮忙!!!! 我很感激<3

您的服务名称是可联网的。 例如,我可以将mariadb放在我的数据库连接中,而不是localhost或 ip。 在下面的示例中, awesome.scot是我的 Apache 服务器,您会注意到另一个名为app的应用程序,它实际上只是挂载文件,因此可以为每个 web 站点添加服务,如果您引用服务名称:通话将毫无问题地完成:-)

这是我的 LAMP sdtack 的 Docker 组合文件。 如果您想尝试它或从中窃取信息,您可以在这里找到它https://github.com/delboy1978uk/lamp ,它还带有 mailhog、自签名 ssl、xdebug 等。

version: '2'

volumes:
    db_data:
        driver: local

services:
    awesome.scot:
        build: ./build/httpd
        links:
            - php
        ports:
            - 80:80
            - 443:443
        volumes_from:
            - app

    php:
        build: ./build/php
        ports:
            - 9000
            - 9001
        volumes_from:
            - app
        links:
            - mariadb
            - mail
        environment:
            APPLICATION_ENV: 'development'
        user: php:staff

    app:
        image: httpd:2.4.38
        volumes:
            - ./:/var/www/html
        command: "echo true"

    mariadb:
        image: mariadb:latest
        volumes:
            - ./build/data:/docker-entrypoint-initdb.d
            - db_data:/var/lib/mysql
        environment:
            MYSQL_ROOT_PASSWORD: '[123456]'
            MYSQL_USER: dbuser
            MYSQL_PASSWORD: '[123456]'
        ports:
            - 3306:3306

    mail:
        image: mailhog/mailhog
        ports:
            - 1025:1025
            - 8025:8025

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM