Home > Article > Backend Development > How is the host's nginx related to the php in the docker container?
My host deployed nginx and started mapping to port 80,
Then I pulled an image of php7-fpm into a new container and started mapping port 9000:9000,
How can I associate nginx on the host with php in the container? Thank you for the answer.
10.07
I accessed the external IP and entered the www root directory in the docker container (default is /var/www/html)
The nginx configuration of my host machine is as follows:
<code> ······· location / { root /var/www/XX; #宿主机的web应用所在目录 index index.html index.php; } ······· location ~ \.php$ { root /var/www/XX; #宿主机的web应用所在目录 fastcgi_pass 192.168.42.18:9000; #docker容器php-fpm分配的内网ip和端口 fastcgi_index index.php; fastcgi_param SCRIPT_FILENAME /var/www/html/$fastcgi_script_name; # /var/www/html 是php容器的页面存放目录 include /usr/local/nginx/conf/fastcgi_params; #宿主机nginx所在目录 } </code>
How can I allow external networks to access the directory /var/www/XX?
Reply content:Then I pulled an image of php7-fpm into a new container and started mapping port 9000:9000,
10.07I accessed the external IP and entered the www root directory in the
docker container (default is /var/www/html)The nginx configuration of my host machine is as follows:
<code> ······· location / { root /var/www/XX; #宿主机的web应用所在目录 index index.html index.php; } ······· location ~ \.php$ { root /var/www/XX; #宿主机的web应用所在目录 fastcgi_pass 192.168.42.18:9000; #docker容器php-fpm分配的内网ip和端口 fastcgi_index index.php; fastcgi_param SCRIPT_FILENAME /var/www/html/$fastcgi_script_name; # /var/www/html 是php容器的页面存放目录 include /usr/local/nginx/conf/fastcgi_params; #宿主机nginx所在目录 } </code>How can I allow external networks to access the directory
/var/www/XX?
First of all, the connection between containers uses the --link parameter, not through IP, nor through -p mapping port.Secondly, the best practice for using Nginx with PHP is that both containers mount the same data volume (that is, the code directory).
It is recommended to familiarize yourself with the basic usage of Docker first.
<code>fastcgi_param SCRIPT_FILENAME /var/www/html/$fastcgi_script_name; </code>
Change to:
<code>fastcgi_param SCRIPT_FILENAME /var/www/XX/$fastcgi_script_name;</code>
Try it;
You can access it by accessing nginx directly, and nginx will forward the request.