DWQA QuestionsCategory: Development ToolHow is the nginx of the host associated with the PHP in the docker container?
jetlag asked 1 month ago

My host deployed nginx and started mapping to port 80,
Then I pull a php7 FPM image into a new container and start the mapping port 9000:9000,
How can I associate the nginx of the host with the PHP in the container? Thank you.

The nginx configuration of my host is as follows:

Root / var / www / XX; directory of the host's Web
        ······· 
        location / {
            index index.html index.php;
        }
        ·······
        location ~ \.php$ {
            Fastcgi ﹐ pass 192.168.42.18:9000; Intranet IP and port allocated by docker container PHP FPM -- 127.0.0.1:9000
            fastcgi_index  index.php;
            fastcgi_param  SCRIPT_FILENAME  $document_root$fastcgi_script_name; 
            include        fastcgi_params;         
        }

hypothesis
1. On the host computer/var/wwwCreate web directory under directory——htmlAnd inhtmlCreate in directoryindex.phpFile, and then access the Internet IP display isNo input file specified, the log of nginx is displayed asFastCGI sent in stderr: "Unable to open primary script: /var/www/html/index.php (Operation not permitted)" while reading response header from upstream
2. On the host computer/var/wwwCreate web directory under directory——html1And inhtml1Create in directoryindex.phpFile, and then access the Internet IP display isFile not found, the log of nginx is displayed asFastCGI sent in stderr: "Primary script unknown" while reading response header from upstream
Known
1. has passeddocker run -vMount the web root of the host to the web root in the PHP FPM docker container
2.nginxandphp-fpmThe user groups of arewww-data
3.Web directory of hostThe user groups and users for arewww-data:www-data, access rights are755
4. The web directory in the PHP FPM docker container is/var/www/html
What is the cause of this?

5 Answers
You Ming answered 1 month ago

First of all, the connection between containers uses the — link parameter, not through IP, nor through the – P mapping port.
Secondly, the best practice of nginx with PHP is that two containers mount the same data volume (that is, code directory).
It is recommended to be familiar with the basic usage of docker.
https://docs.docker.com/

wdy1184 replied 1 month ago

This is the problem of permission. Let’s see if PHP FPM has permission to access PHP files

Ice wings answered 1 month ago

Take a look at this article I wrotehttps://icewing.cc/docker-use…
In short, nginx passes various parameters to FPM process through fastcgi param, and the very important line isfastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
Script filename specifies the path of the PHP file to be executed, which must be the path that can be found in the FPM container, not the path in the host. Give you a simple solution – if the directory in the host is/var/www/html, the path in the container is also renamed/var/www/htmlIn this way, there will be no inconsistency between the paths of PHP files inside and outside the container.

Walking in the wheat field answered 1 month ago

In your configuration:

fastcgi_param  SCRIPT_FILENAME  /var/www/html/$fastcgi_script_name;

Change to:

fastcgi_param  SCRIPT_FILENAME  /var/www/XX/$fastcgi_script_name;

Try;

wdy1184 answered 1 month ago

You can access it by directly accessing nginx, and nginx will forward the request.

addcn answered 1 month ago

Three steps to achieve correlation:

  1. When the nginx container is started, the--linkParameter, — link PHP FPM: PHP FPM, sharing the network of PHP FPM container.
  2. Nginx.conf configuration, setting agent,fastcgi_pass php-fpm:9000;#Here’s the key!! Where PHP FPM is the name of the PHP container, see the value specified in the start PHP container docker run — name.
  3. PHP container listens for all 9000 accesseslisten = 9000, only listen to the local machine by defaultlisten = 127.0.0.1:9000 <———I guess you can't access the PHP container because of this step

Recommend this articleDocker multi container deployment LNMP environment, which contains the relationship among nginx / PHP / MySQL and the analysis of container communication method