Using nginx php as the architectural model of webserver is widely used nowadays. However, the first step that needs to be realized is how to make nginx call php correctly. Because nginx calling php is not as straightforward as calling a static file, it requires dynamic execution of the php script. So it involves the configuration of the nginx.conf file. For novices, this step generally requires online information search. For ordinary veterans, many students do not understand why they need to be configured in this way. The main content of this article is how to correctly configure the php calling method in nginx server, and the basic principles of configuration.
1. nginx php operating principle:
First of all, let’s briefly talk about the principle. The current mainstream nginx php operating principle is as follows:
1 , nginx's worker process directly manages each network request to nginx.
2. For PHP, since PHP plays the role of a CGI program during the entire network request process, a process management program called php-fpm is used to manage these requested PHP programs. . The php-fpm program, like nginx, needs to listen on the port and have master and worker processes. The worker process directly manages each PHP process.
3. About fastcgi: fastcgi is a process manager that manages cgi processes. There are many process managers on the market that implement fastcgi functions, and php-fpm is one of them. One more thing, php-fpm, as a fast-cgi process management service, will listen to the port. Generally, it listens to port 9000 by default, and it monitors the local machine, that is, it only receives port requests from the local machine, so we usually enter the command netstat -nlpt|grep php-fpm will get:
tcp 0 0 127.0.0.1:9000 0.0.0.0:* 1057/php-fpm
Here 127.0.0.1:9000 is monitoring The meaning of port 9000 of this machine.
4. Regarding fastcgi configuration files, currently fastcgi configuration files are generally placed in the same directory as nginx.conf. There are generally two types of configuration files: fastcgi.conf and fastcgi_params. Different nginx versions will have different configuration files. There is a very important difference between these two configuration files: the following configuration is missing in the fastcgi_parames file:
fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
us You can open the fastcgi_parames file and add the above line, or you can add it dynamically where you want to use the configuration. Make this configuration take effect.
5. When a php request needs to be processed, the worker process of nginx will hand over the request to the worker process of php-fpm for processing. That is to say, nginx calls php at the beginning. In fact, strictly speaking, it is nginx calls php indirectly.
After understanding the above five simple principles, configuring the php calling method in nginx becomes easy.
2. Configuration file:
Directly paste the code to explain line by line. Here is the simplest nginx vhost configuration that can start the php script normally:
server { listen 8011; server_name test.cn; location ~ \.php?.*$ { root /share/test; fastcgi_pass 127.0.0.1:9000; fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name; include fastcgi_params; } }
1. The first brace server{ }: Needless to say, it represents an independent server.
2. listen 8011: represents the server listening to port 8011
3. Location ~ \.php?.*${ }: Represents a location that can match the corresponding uri. It is used to match a type of uri and make customized logic and configuration for the matched uri request. The location here matches all uri requests with .php, for example: http://192.168.244.128:8011/test.php/asdasd http://192.168.244.128:8011/index.php, etc.
4. root /share/test: Request the resource root directory and tell the uri matching the location to go to the /share/teset folder to find the resource with the same name.
5. fastcgi_pass 127.0.0.1:9000: The beginning of this line is the focus of this article: this line of code means that the uri request entering the location is regarded as a cgi program and the request is sent to Port 9000 is handled by php-fpm.
6. fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;: This line of configuration means: dynamically add a line of fastcgi configuration, the configuration content is SCRIPT_FILENAME, inform the management process, cgi script name. Since there is only the fastcgi_params file in my nginx and no fastcgi.conf file, in order for php-fpm to know the specific value of SCRIPT_FILENAME, this line of configuration must be added dynamically.
7. include fastcgi_params; introduce fastcgi configuration file
The above is the simplest version of the simplest version of the nginx startup php script. After restarting nginx, create one in the /share/test directory xx.php file, enter 2f2edeb77fbca7b67937954b4089d937 to save it, and then visit localhost:8011/xx.php in the browser to display hello world on the web page.
3. Summary:
In fact, for calling CGI script programs such as php, as long as you understand the 5 principles I mentioned at the beginning, and then combine lines 5-7 With the configuration explanation, you can clearly understand why you need to configure it this way. For novices, they are often confused by the fastcgi and php-fpm cgi programs. They configure them randomly and run them online without delving into their principles. So I hope what I write here can be of some help to readers.