我正在尝试链接 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/