php - 如何正确链接 php-fpm 和 Nginx Docker 容器?

标签 php nginx docker dockerfile docker-compose

我正在尝试链接 2 个单独的容器:

问题是 php 脚本不起作用。可能是 php-fpm 配置不正确。 这是源代码,在我的repository .这是文件 docker-compose.yml:

nginx:
    build: .
    ports:
        - "80:80"
        - "443:443"
    volumes:
        - ./:/var/www/test/
    links:
        - fpm
fpm:
    image: php:fpm
    ports:
        - "9000:9000"

Dockerfile 用于构建基于 nginx 的自定义镜像:

FROM nginx

# Change Nginx config here...
RUN rm /etc/nginx/conf.d/default.conf
ADD ./default.conf /etc/nginx/conf.d/

最后,这是我自定义的 Nginx 虚拟主机配置:

server {
    listen  80;

    server_name localhost;
    root /var/www/test;

    error_log /var/log/nginx/localhost.error.log;
    access_log /var/log/nginx/localhost.access.log;

    location / {
        # try to serve file directly, fallback to app.php
        try_files $uri /index.php$is_args$args;
    }

    location ~ ^/.+\.php(/|$) {
        fastcgi_pass 192.168.59.103:9000;
        fastcgi_split_path_info ^(.+\.php)(/.*)$;
        include fastcgi_params;
        fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
        fastcgi_param HTTPS off;
    }
}

谁能帮我正确配置这些容器以执行 php 脚本?

附言 我像这样通过 docker-composer 运行容器:

docker-compose up

来自项目根目录。

最佳答案

我知道这是一篇旧帖子,但我遇到了同样的问题,不明白为什么您的代码不起作用。 经过大量测试,我找到了原因。

fpm 似乎从 nginx 接收到完整路径并尝试在 fpm 容器中查找文件,因此它必须与 nginx 配置中的 server.root 完全相同,即使它在 nginx 容器中不存在。

演示:

docker-compose.yml

nginx:
    build: .
    ports:
        - "80:80"
    links:
        - fpm
fpm:
    image: php:fpm
    ports:
        - ":9000"

    # seems like fpm receives the full path from nginx
    # and tries to find the files in this dock, so it must
    # be the same as nginx.root
    volumes:
        - ./:/complex/path/to/files/

/etc/nginx/conf.d/default.conf

server {
    listen  80;

    # this path MUST be exactly as docker-compose.fpm.volumes,
    # even if it doesn't exist in this dock.
    root /complex/path/to/files;

    location / {
        try_files $uri /index.php$is_args$args;
    }

    location ~ ^/.+\.php(/|$) {
        fastcgi_pass fpm:9000;
        include fastcgi_params;
        fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
    }
}

Dockerfile

FROM nginx:latest
COPY ./default.conf /etc/nginx/conf.d/

关于php - 如何正确链接 php-fpm 和 Nginx Docker 容器?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/29905953/

相关文章:

python - 在 docker 中部署一个最小的 flask 应用程序 - 服务器连接问题

docker - Heroku CLI "No command specified for process type web"释放容器时

php - 在 POST、PayPal 表单之前捕获表单数据

node.js - 如何使用 docker-compose 通过此 mongoDB 连接字符串将我的 mongo 数据库连接到我的 Node 应用程序?

php - 我需要可以下载 php、html、css 教程的地方

php - Nginx 错误 recv() 失败 (104 : Connection reset by peer)

nginx - 在 Centos 6.8 上升级 nginx

c# - 为什么我的 C# AJAX 请求在 HTTP 下可以正常工作,而在 HTTPS 下却失败?

php - Codeigniter get_where 查询

php - 通过在组合框中选择来过滤表