2

我有一个网站(ZF1)和一个在同一个 Docker(Laradock)容器上运行的 API(Laravel)。我可以通过浏览器分别访问每一个,但是当我从网站向应用程序发出 cURL 请求时,我得到一个空响应并且标头返回 0。如果我输出 cURL 错误,那么我会得到这个:

$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, '[API_ENDPOINT]');
curl_setopt ($ch, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($ch, CURLOPT_HEADER, true);
curl_setopt($ch, CURLOPT_FOLLOWLOCATION, 1);
curl_close ($ch);

echo curl_strerror(curl_errno($ch));

(7) Failed to connect to [API_HOST] port 80: Connection refused

但是,如果我从应用程序向https://www.google.com发出 cURL 请求,那么它会返回一个结果。我也尝试使用 file_get_contents(),但没有收到任何响应和以下警告:

[Thu May 18 21:41:33.828737 2017] [proxy_fcgi:error] [pid 949:tid 139999802541824] [client 172.20.0.1:49652] AH01071: Got error 'PHP message: PHP Warning:  file_get_contents
([API_ENDPOINT]): failed to open stream: Connection refused in /var/www/projects/[APPLICATION_PATH]/[CONTROLLER].php on line 2367\n', referer: [WEBSITE_HOST]/[URI]

我还通过 SSH 连接到 Apache2 容器,并能够成功地对 API_ENDPOINT 进行 cURL 调用并取回预期的数据。然后我尝试使用 wget 获取标题信息并收到以下信息:

root@cd3a4177dcfa:/var/log/apache2# wget --header="Host: http://subdomain.example.dev/api/calendarevents" -Os http://localhost
--2017-05-19 07:28:15--  http://localhost/
Resolving localhost (localhost)... ::1, 127.0.0.1
Connecting to localhost (localhost)|::1|:80... connected.
HTTP request sent, awaiting response... No data received.
Retrying.

--2017-05-19 07:28:16--  (try: 2)  http://localhost/
Connecting to localhost (localhost)|::1|:80... connected.
HTTP request sent, awaiting response... No data received.
Retrying.

如果是 Apache2 问题,这里是我的 API VirtualHost:

Listen 80
<VirtualHost *:80>
  ServerName subdomain.example.dev
  DocumentRoot /var/www/projects/[API_PROJECT]/public/
  CustomLog /var/log/apache2/[API_HOST]-access.log combined
  ErrorLog /var/log/apache2/[API_HOST]-error.log
  Options Indexes FollowSymLinks

  <Directory "/var/www/projects/[API_PROJECT]/public/">
      Options FollowSymLinks
      AllowOverride All
      Require all Granted
  </Directory>

  # set environment
  #SetEnvIf Authorization "(.*)" HTTP_AUTHORIZATION=$1 // Did not notice a difference in behavior when enabled
  #CGIPassAuth on // Sites stopped loading when enabled
</VirtualHost>

和网站虚拟主机:

Listen 80
<VirtualHost *:80>
  ServerName [WEBSITE_HOST]
  DocumentRoot /var/www/projects/[WEBSITE_PROJECT]
  ErrorLog /var/log/apache2/[WEBSITE_HOST]-error.log
  Options Indexes FollowSymLinks

  <Directory "/var/www/projects/[WEBSITE_PROJECT]/repo">
    Options FollowSymLinks
    AllowOverride All
    Require all Granted
  </Directory>

  # set environment
  SetEnv APPLICATION_ENV development
  SetEnv APPLICATION_LOGGING false
  #SetEnvIf Authorization "(.*)" HTTP_AUTHORIZATION=$1 // Did not notice a difference in behavior when enabled
  #CGIPassAuth on // Sites stopped loading when enabled
</VirtualHost>

这是 Docker 信息(Windows 10/Hyper V/Docker 版本 17.03.1-ce-win12 (12058) 频道:稳定)

Containers: 9
 Running: 5
 Paused: 0
 Stopped: 4
Images: 233
Server Version: 17.03.1-ce
Storage Driver: overlay2
 Backing Filesystem: extfs
 Supports d_type: true
 Native Overlay Diff: true
Logging Driver: json-file
Cgroup Driver: cgroupfs
Plugins:
 Volume: local
 Network: bridge host ipvlan macvlan null overlay
Swarm: inactive
Runtimes: runc
Default Runtime: runc
Init Binary: docker-init
containerd version: 4ab9917febca54791c5f071a9d1f404867857fcc
runc version: 54296cf40ad8143b62dbcaa1d90e520a2136ddfe
init version: 949e6fa
Security Options:
 seccomp
  Profile: default
Kernel Version: 4.9.27-moby
Operating System: Alpine Linux v3.5
OSType: linux
Architecture: x86_64
CPUs: 2
Total Memory: 3.837 GiB
Name: moby
ID: PE42:IS45:4OO6:JMEQ:NWNB:NQDF:RPEL:JPHJ:L6OP:A5SL:IDP3:F7SV
Docker Root Dir: /var/lib/docker
Debug Mode (client): false
Debug Mode (server): true
 File Descriptors: 86
 Goroutines: 74
 System Time: 2017-05-19T04:52:50.5943959Z
 EventsListeners: 0
Registry: https://index.docker.io/v1/
Experimental: true
Insecure Registries:
 127.0.0.0/8
Live Restore Enabled: false

这些是我目前正在运行的容器:

CONTAINER ID        IMAGE                COMMAND                  CREATED             STATUS              PORTS                                      NAMES
cd3a4177dcfa        laradock_apache2     "/opt/docker/bin/e..."   8 hours ago         Up 8 hours          0.0.0.0:80->80/tcp, 0.0.0.0:443->443/tcp   laradock_apache2_1
8ef66cb80a2e        laradock_php-fpm     "docker-php-entryp..."   8 hours ago         Up 8 hours          9000/tcp                                   laradock_php-fpm_1
988eff458036        laradock_workspace   "/sbin/my_init"          8 hours ago         Up 8 hours          0.0.0.0:2222->22/tcp                       laradock_workspace_1
8be5253e8622        laradock_redis       "docker-entrypoint..."   8 hours ago         Up 8 hours          0.0.0.0:6379->6379/tcp                     laradock_redis_1
aa6d8d6ae950        laradock_mysql       "docker-entrypoint..."   8 hours ago         Up 8 hours          0.0.0.0:3306->3306/tcp                     laradock_mysql_1

我不确定是什么导致同一容器上的两个站点之间的连接被拒绝。我检查了我的 php.ini 并启用了 curl 和 allow_url_fopen = on。我可以通过浏览器和 CLI cURL 直接访问 API_ENDPOINT 并获得预期的结果。在这一点上,我最好的猜测是某种端口冲突、授权标头被阻止、Apache2 配置或某种奇怪的 Docker/Laradock 问题。我还认为这可能是 Apache2 和 PHP-FPM 之间的冲突,但这里详述的解决方案似乎对我不起作用。

提前感谢您提供的任何帮助。

4

2 回答 2

8

而不是添加到 php-fpm 容器的 extra_hosts 中。修改 Apache2 容器的 Backend 网络定义并添加别名。这是您不必在每次 ip 更改时都不断更改 extra_hosts

    networks:
        frontend:              
        backend:
         aliases:
          - subdomain.example.app
于 2019-07-08T05:56:28.940 回答
3

我弄清楚是什么导致了我的问题。我需要在我的 docker-compose.yml 文件的 PHP-FPM 部分添加一个额外的主机。所以添加:

- "subdomain.example.app:10.0.75.1"

给额外的主人。然后你需要重建你的容器:

docker-compose up -d --build apache2 mysql redis

这就是 Laradock 的 docker-compose.yml 的 PHP-FPM 部分应该是这样的:

php-fpm:
      build:
        context: ./php-fpm
        args:
          - INSTALL_XDEBUG=${PHP_FPM_INSTALL_XDEBUG}
          - INSTALL_BLACKFIRE=${INSTALL_BLACKFIRE}
          - INSTALL_SOAP=${PHP_FPM_INSTALL_SOAP}
          - INSTALL_MONGO=${PHP_FPM_INSTALL_MONGO}
          - INSTALL_ZIP_ARCHIVE=${PHP_FPM_INSTALL_ZIP_ARCHIVE}
          - INSTALL_BCMATH=${PHP_FPM_INSTALL_BCMATH}
          - INSTALL_PHPREDIS=${PHP_FPM_INSTALL_PHPREDIS}
          - INSTALL_MEMCACHED=${PHP_FPM_INSTALL_MEMCACHED}
          - INSTALL_OPCACHE=${PHP_FPM_INSTALL_OPCACHE}
          - INSTALL_EXIF=${PHP_FPM_INSTALL_EXIF}
          - INSTALL_AEROSPIKE_EXTENSION=${PHP_FPM_INSTALL_AEROSPIKE_EXTENSION}
          - INSTALL_MYSQLI=true
          - INSTALL_TOKENIZER=${PHP_FPM_INSTALL_TOKENIZER}
          - INSTALL_INTL=${PHP_FPM_INSTALL_INTL}
          - INSTALL_GHOSTSCRIPT=${PHP_FPM_INSTALL_GHOSTSCRIPT}
        dockerfile: "Dockerfile-${PHP_VERSION}"
      volumes_from:
        - applications
      volumes:
        - ./php-fpm/php${PHP_VERSION}.ini:/usr/local/etc/php/php.ini
      expose:
        - "9000"
      depends_on:
        - workspace
      extra_hosts:
        - "dockerhost:${DOCKER_HOST_IP}"
        - "subdomain.example.app:10.0.75.1"
      environment:
        - PHP_IDE_CONFIG=${PHP_IDE_CONFIG}
      networks:
        - backend
于 2017-05-20T00:15:50.740 回答