使用docker-compose.yml 配置php环境的时候,特别是要注意php的路径。如果nginx和php的路径配置错误,容易出现File not found ,出现这个问题的主要原因是php-fpm和nginx在不同的容器中,nginx通过fastcgi_pass发起代理请求。php-fpm收到请求时,是在php-fpm容器中,nginx的根目录与php-fpm的处理文件对应的目录相同,而需要在两个容器中配置卷映射。
docker-compose.yml文件配置如下:
version: "3.9"
services:
nginx:
container_name: nginx
image: nginx:latest
restart: always
volumes:
- /home/www:/www
ports:
- 80:80
depends_on:
- my_php
php:
container_name: my_php
image: php:8.2.9-fpm
restart: always
volumes:
- /home/php:/www
nginx和php都需要指向同一个根目录 ,例如/www。
nginx配置文件:
server {
listen 80;
server_name _;
root /www/test;
index index.php index.html
location ~* \.php$ {
fastcgi_pass my_php:9000;
fastcgi_index index.php;
include fastcgi_params;
fastcgi_param SCRIPT_FILENAME /www/test$fastcgi_script_name;
}
}
注意:使用docker compose 方式配置的容器,那么配置文件中fastcgi_pass
需要使用php的容器名。