我的宿主机部署了nginx,并启动映射到80端口,
然后我pull了一份php7-fpm的镜像到一个新容器里,并启动映射端口9000:9000,
请问如何才能让宿主机的nginx和容器里的php关联起来呢?望解答谢谢。
我宿主机的nginx配置如下:
root /var/www/XX; #宿主机的web所在目录
·······
location / {
index index.html index.php;
}
·······
location ~ \.php$ {
fastcgi_pass 192.168.42.18:9000; #docker容器php-fpm分配的内网ip和端口——127.0.0.1:9000
fastcgi_index index.php;
fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
include fastcgi_params;
}
假设
1.在宿主机的/var/www
目录下创建web目录——html
,并在html
目录里创建index.php
文件,然后访问外网ip显示的是 No input file specified
,nginx的log显示为FastCGI sent in stderr: "Unable to open primary script: /var/www/html/index.php (Operation not permitted)" while reading response header from upstream
2.在宿主机的/var/www
目录下创建web目录——html1
,并在html1
目录里创建index.php
文件,然后访问外网ip显示的是 File not found
,nginx的log显示为 FastCGI sent in stderr: "Primary script unknown" while reading response header from upstream
已知
1.已通过 docker run -v
将宿主机的web根目录挂载到php-fpm docker容器里的web根目录
2.nginx和php-fpm的用户组均是www-data
3.宿主机的web目录的用户组和用户是www-data:www-data
,访问权限是755
4.php-fpm docker容器里的web目录为 /var/www/html
请问这是什么原因造成的呢?
看下我写的这篇文章 https://icewing.cc/docker-use...
简单的说,Nginx 通过 fastcgi_param 传递各种参数给 FPM 进程,而其中非常重要的一行就是
fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
。SCRIPT_FILENAME 指定了要执行的 php 文件的路径,这个路径一定是在 fpm 容器中能够找到的路径,而不是宿主机中的路径。给你一个简单的解决方案——如果宿主机中的目录是
/var/www/html
,那么容器中的路径也改名叫/var/www/html
,这样就不会出现容器内外 php 文件路径不一致的问题了。