我的宿主机部署了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
请问这是什么原因造成的呢?
Copyright 2014-2026 https://www.php.cn/ All Rights Reserved | php.cn | 湘ICP备2023035733号
你的配置中:
改成:
试试;
直接访问nginx就能访问到,nginx会转发请求。
3个步骤实现关联:
nginx容器启动时候,通过
--link参数,--link php-fpm:php-fpm,共享php-fpm容器的网络。nginx.conf配置,设置代理,
fastcgi_pass php-fpm:9000;#此处为关键!!其中php-fpm为php容器的名称,见启动php容器docker run --name指定的值。php容器监听所有9000的访问
listen = 9000,默认只监听本机listen = 127.0.0.1:9000<---------估计你是这步导致访问不了php容器推荐一下这篇文章《Docker多容器部署LNMP环境》,里面有nginx/php/mysql三者间关系及容器通信方法分析