nginx 和 php-fpm podman 之间通信

问题描述 投票:0回答:3

我想使用 php-fpm 设置 nginx 来运行 symfony。

为此,我创建了一个

pod
调用
webservice
:
podman pod create --name webservice -p 8080:80

我启动了 php-fpm :

podman run -ti \
--name php7 \
--pod webservice \
--volume ~/samus/bare-data/php:/usr/local/etc/php/php.ini \
--volume ~/samus/bare-data/www:/var/www/html:rw \
docker.io/library/php:7.4-fpm-alpine

最后是 nginx :

podman run -ti \
--name nginx \
--pod webservice \
--volume ~/samus/bare-data/nginx:/etc/nginx/conf.d:ro \
--volume ~/samus/bare-data/www:/var/www/html \
docker.io/library/nginx:alpine

当我这样做时,php 告诉我:

[08-Sep-2021 18:05:46] NOTICE: fpm is running, pid 1
[08-Sep-2021 18:05:46] NOTICE: ready to handle connections

和 nginx :

/docker-entrypoint.sh: /docker-entrypoint.d/ is not empty, will attempt to perform configuration
/docker-entrypoint.sh: Looking for shell scripts in /docker-entrypoint.d/
/docker-entrypoint.sh: Launching /docker-entrypoint.d/10-listen-on-ipv6-by-default.sh
10-listen-on-ipv6-by-default.sh: info: /etc/nginx/conf.d/default.conf is not a file or does not exist
/docker-entrypoint.sh: Launching /docker-entrypoint.d/20-envsubst-on-templates.sh
/docker-entrypoint.sh: Launching /docker-entrypoint.d/30-tune-worker-processes.sh
/docker-entrypoint.sh: Configuration complete; ready for start up
2021/09/08 18:05:52 [notice] 1#1: using the "epoll" event method
2021/09/08 18:05:52 [notice] 1#1: nginx/1.21.1
2021/09/08 18:05:52 [notice] 1#1: built by gcc 10.3.1 20210424 (Alpine 10.3.1_git20210424) 
2021/09/08 18:05:52 [notice] 1#1: OS: Linux 5.13.13-200.fc34.x86_64
2021/09/08 18:05:52 [notice] 1#1: getrlimit(RLIMIT_NOFILE): 524288:524288
2021/09/08 18:05:52 [notice] 1#1: start worker processes
2021/09/08 18:05:52 [notice] 1#1: start worker process 17
2021/09/08 18:05:52 [notice] 1#1: start worker process 18
2021/09/08 18:05:52 [notice] 1#1: start worker process 19
2021/09/08 18:05:52 [notice] 1#1: start worker process 20
2021/09/08 18:05:52 [notice] 1#1: start worker process 21
2021/09/08 18:05:52 [notice] 1#1: start worker process 22
2021/09/08 18:05:52 [notice] 1#1: start worker process 23
2021/09/08 18:05:52 [notice] 1#1: start worker process 24

如果我这样做

podman ps -a
我会得到以下结果:

CONTAINER ID  IMAGE                                 COMMAND               CREATED         STATUS                        PORTS                 NAMES
e63300d78306  k8s.gcr.io/pause:3.5                                        23 minutes ago  Up 23 minutes ago             0.0.0.0:8080->80/tcp  0bb2d4aa79a9-infra
aca134de2fb3  docker.io/library/php:7.4-fpm-alpine  php-fpm               23 minutes ago  Up 23 minutes ago             0.0.0.0:8080->80/tcp  php7
f7b19cc296ee  docker.io/library/nginx:alpine        nginx -g daemon o...  23 minutes ago  Up 23 minutes ago             0.0.0.0:8080->80/tcp  nginx

127.0.0.1:8080
给我
The connection was reset

这里我假设没有为 php 共享 9000 端口是不正常的。

但我有点不知道为什么。

我尝试将端口添加到我的 pod 中 (

podman pod create --name webservice -p 8080:80 -p 9000:9000
),但除了到处都是
0.0.0.0:9000->9000/tcp
之外,没有什么新内容。

有关我的 nginx conf 的信息(存储在

~/samus/bare-data/nginx/podman-nginx-confs
:

server {
    listen 80;
    server_name localhost;
    root /var/www/html;

    location / {
        try_files $uri $uri/ /index.php?$uri&$args;
        index index.php index.html;
    }

    location ~ \.php$ {
        try_files $uri =404;
        fastcgi_split_path_info ^(.+\.php)(/.+)$;
        fastcgi_pass    localhost:9000;
        fastcgi_index   index.php;
        fastcgi_param   SCRIPT_FILENAME $document_root$fastcgi_script_name;
        include         fastcgi_params;
        fastcgi_param   PATH_INFO $fastcgi_path_info;
    }

    location ~ /\.ht {
        deny all;
    }
}
docker podman
3个回答
1
投票

我知道这是一个近 2 年前的问题,但我为此苦苦挣扎了 2 天,最近才想通。

如果您检查容器上的 nginx 配置 (

/etc/nginx/nginx.conf
),您会发现它仅包含来自
/etc/nginx/conf.d/
的文件(如果文件扩展名是
conf
)。

include /etc/nginx/conf.d/*.conf

因此将

~/samus/bare-data/nginx/podman-nginx-confs
重命名为
~/samus/bare-data/nginx/podman-nginx-confs.conf
它应该可以工作。

此外,

fastcgi_pass
并不关心您使用
localhost
还是
php7
。两人都决定
127.0.0.1


0
投票

您不需要为 Pod 指定端口 9000。尝试将 nginx.conf 中的这一行更改为:

fastcgi_pass    localhost:9000;

fastcgi_pass    php7:9000;

无论如何,答案已经晚了,但它可能会帮助有同样问题的用户。


0
投票

podman network create webby-web

podman run .... --hostname nginx --network webby-web ....

podman run .... --hostname php   --network webby-web ....

fastcgi_pass php-fpm;

upstream php-fpm {
  server php:9000;  # Using hostname there

  keepalive 10;
}

假设您将每个容器作为单独的 Pod 运行,以实现更好的资源/生命周期控制。 Booth Pod 通过专用网络进行通信以实现隔离。 你永远不要暴露 php-fpm 端口 9000。这是危险!您需要公开的唯一端口是 8080 或 Nginx 使用的任何端口。 对于网络调试,您始终可以创建简单的

iputils
telnet
nmap
iproute
等映像,并在同一网络中运行该容器来测试这个或那个。

© www.soinside.com 2019 - 2024. All rights reserved.