Laradock php-fpm nginx file_get_contents 連接被拒絕

編輯 : Frank
日期 : 2020/03/27
參考網址


情境

使用 laravel 製作 api 來進行使用,但在使用此 api 時就顯示連線被拒絕的問題。

1
$data = file_get_contents($urlApi);

file_get_contents($urlApi): failed to open stream: Connection refused

發生原因

This seems to be an error where the containers on which the curl request are being made don’t know the proper IP address and thus are making their requests to “localhost”, meaning their own container (and not hitting the nginx container instead).

解決方法

首先為 nginx 定義 DNS

sudo vim /etc/hosts

1
2
# docker laradock nginx use
127.0.0.1 laravel.test

隨後修改 laradock 內的 docker-compose.yml,將 nginx 網路連線設定給予aliase。

vim docker-compose.yml

1
2
3
4
5
6
7
networks:
frontend:
aliases:
- laravel.test
backend:
aliases:
- laravel.test

修改結束後即可重啟服務就能解決問題了。

1
2
docker-compose down
docker-compose up -d nginx mysql redis workspace