php - docker上的Nginx,curl获取连接被拒绝
问题描述
我正在使用 docker 在本地环境中工作。我有一个 nginx web 容器和一个 php 容器,它们在同一个网络中。
我从自己的 dockerfile 构建 php 容器(使用 phpfpm 和 phpcli);nginx:stable
并且,我在集线器图像的 docker-compose 中编写了 nginx 。
我有 2 个项目:一个 symfony( ) 和一个在其中运行http://i-r4y.kaiza.lh/
的 drupal( )。http://i-z4r4.kaiza.lh/
symfony 公开了一个 api,drupal 必须使用它。问题是当我从drupal调用symfony时出错cURL error 7: Failed to connect to i-r4y.kaiza.lh port 80: Connection refused
我以为是symfony侧api路由的配置;就像它必须是公开的或接受 CORS 等...
但是当我在 php 容器中输入 bash时,我确实 curl 了 symfony 或 drupal url,我有同样的错误。
app@kz-php74:/var/www$ curl http://i-r4y.kaiza.lh
curl: (7) Failed to connect to i-r4y.kaiza.lh port 80: Connection refused
app@kz-php74:/var/www$ curl http://i-z4r4.kaiza.lh
curl: (7) Failed to connect to i-z4r4.kaiza.lh port 80: Connection refused
我检查了主机所在的 php 容器/etc/hosts
app@kz-php74:/var/www$ cat /etc/hosts | grep i-
127.0.0.1 i-r4y.kaiza.lh
127.0.0.1 i-z4r4.kaiza.lh
这是 docker-compose.yml :
version: '2.4'
services:
php7.4:
build:
context: ../../../dockerfile
dockerfile: Dockerfile.php
args:
PHP_VERSION: 7.4
container_name: "kz-php74"
hostname: "kz-php74"
user: 1000:1000
working_dir: /var/www
volumes:
- "${LOCAL_PATH}/../www:/var/www"
extra_hosts:
- "i-r4y.kaiza.lh:127.0.0.1"
- "i-z4r4.kaiza.lh:127.0.0.1"
networks:
- kz_local
mysql:
container_name: kz-mysql
image: mariadb:10.4.0
volumes:
- ${LOCAL_PATH}/.data/mariadb:/var/lib/mysql
- ${LOCAL_PATH}/config/mariadb/conf.d/custom.cnf:/etc/mysql/conf.d/custom.cnf
- ${LOCAL_PATH}/../www:/var/www
ports:
- ${MYSQL_PORT:-3306}:3306
environment:
MYSQL_ROOT_PASSWORD: password
networks:
- kz_local
web:
image: nginx:stable
container_name: kz-web
volumes:
- ${LOCAL_PATH}/config/nginx/conf.d:/etc/nginx/conf.d
- ${LOCAL_PATH}/../www:/var/www
ports:
- 80:80
networks:
- kz_local
networks:
kz_local:
external: true
drupal的nginx配置:
server {
listen 80;
listen [::]:80;
server_name i-z4r4.kaiza.lh;
root /var/www/i-z4r4/web;
resolver 127.0.0.11 ipv6=off;
location @rewrite {
rewrite ^/(.*)$ /index.php?q=$1;
}
# In Drupal 8, we must also match new paths where the '.php' appears in
# the middle, such as update.php/selection. The rule we use is strict,
# and only allows this pattern with the update.php front controller.
# This allows legacy path aliases in the form of
# blog/index.php/legacy-path to continue to route to Drupal nodes. If
# you do not have any paths like that, then you might prefer to use a
# laxer rule, such as:
# location ~ \.php(/|$) {
# The laxer rule will continue to work if Drupal uses this new URL
# pattern with front controllers other than update.php in a future
# release.
location ~ '\.php$|^/update.php' {
set $fastcgi_pass "kz-php74:9000";
fastcgi_split_path_info ^(.+?\.php)(|/.*)$;
# Security note: If you're running a version of PHP older than the
# latest 5.3, you should have "cgi.fix_pathinfo = 0;" in php.ini.
# See http://serverfault.com/q/627903/94922 for details.
include fastcgi_params;
# Block httpoxy attacks. See https://httpoxy.org/.
fastcgi_param HTTP_PROXY "";
fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
fastcgi_param PATH_INFO $fastcgi_path_info;
fastcgi_param QUERY_STRING $query_string;
fastcgi_intercept_errors on;
fastcgi_pass $fastcgi_pass;
}
...
upstream php {
server kz-php74:8080;
}
}
对于 symfony:
server {
listen 80;
listen [::]:80;
server_name i-r4y.kaiza.lh;
root /var/www/i-r4y/public;
resolver 127.0.0.11 ipv6=off;
location / {
# try to serve file directly, fallback to index.php
try_files $uri /index.php$is_args$args;
}
location ~ ^/index\.php(/|$) {
set $fastcgi_pass "kz-php74:9000";
fastcgi_pass $fastcgi_pass;
fastcgi_split_path_info ^(.+\.php)(/.*)$;
include fastcgi_params;
fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
fastcgi_param HTTPS on;
}
location @rewriteapp {
rewrite ^(.*)$ /app.php/$1 last;
}
...
upstream php {
server kz-php74:8080;
}
...
}
有人知道为什么这不起作用吗?
谢谢
解决方案
您需要将端口 80 暴露给您的 docker 主机。
看起来您正试图从 docker 主机(运行 docker 的真实机器)卷曲到在 docker 容器中运行的 nginx。
您可以在 docker-compose 中执行以下操作:
web:
image: nginx:stable
…
ports:
- 80:80
这将使您进入 nginx。但是,您的下一个障碍可能是 ngxinx 到达您的 php 服务。
就像@AmyDev 提到的那样,使用 docker 的名称解析会更好地为您服务。
在您的 nginx 配置中,您需要以下行将 nginx 指向 docker 的内部 DNS:
resolver 127.0.0.11 ipv6=off;
然后,您可以upstream
使用以下内容声明您的:
upstream php {
# use the docker service name. I removed the . b/c I don't know if that works in a docker service name
# This assumes the docker service has been renamed from php7.4 to php74 in docker-compose.yml
server php74:8080; # or whatever port to which php is listening
}
推荐阅读
- awk - 将行号添加到输出文件
- json - 在 YAML 文档中将 Python 嵌入对象作为本机 JSON 发出
- docker - 容器在 Windows Docker 工具箱中的本地主机上不可用
- sql - Oracle SQL - 使用 LPAD 和 RPAD 缺少右括号
- angular - 在 ngFor、Angular5 中迭代枚举
- php - 为什么mysql数据只打印电子邮件正文中的最后一行?
- angular - 在 Angular 项目中使用 Jasmine 调用测试回调函数
- vb.net - 通过 Web 参考的 Soap 请求中缺少命名空间前缀
- google-apps-script - 检查文件夹中的文件名,如果找不到则导出为 PDF
- python - 一组非重叠整数范围的 Python 表示