PHP + FastCGI + Nginx Dynamic Request Processing configuration, fastcginginx

Source: Internet
Author: User

PHP + FastCGI + Nginx Dynamic Request Processing configuration, fastcginginx


Nginx does not support calling external programs. Therefore, you must use the FastCGI interface to call external programs to process client dynamic page requests.

CGI is called the Common Gateway Interface (Public Gateway Interface) in English and serves as a bridge between Nginx and dynamic scripting programs. Nginx sends dynamic requests to FastCGI through the FastCGI Interface, the Wrapper process in FastCGI generates a thread and sends the request to the script interpreter for execution. Then, the original result is returned to Nginx through the original socket, and then Nginx delivers the result to the client.

Nginx uses the socket file socket to send dynamic requests to wrapper, using the Tcp protocol. Wrapper uses the CGI interface to accept requests. In this way, the web server and the interpreter can be developed independently, which avoids errors, crashes, and security problems caused by the interpreter directly calling the server interface. In addition, Nginx can focus on static page requests and forward dynamic requests, and install the script interpreter on another server, so that the pressure on the server can be apportioned.


CGI is developed as a patch for PHP programs. To install PHP, first install the library on which it depends, then, you can add options such as enable-fpm-enable-CGI when compiling configuration parameters. To compile the PHP extension module, you need to use the phpize tool in php to generate the configure file during module compilation. If you cannot generate the configure file when running phpize, the reason is: the autoconf package is not installed.

The configuration file for the php-fpm process is/usr/local/php/etc/php-fpm.conf which can be configured accordingly for php-fpm.

Nginx configuration supports fastcgi:

Location ~ \. Php $ {

Root html;

Fastcgi_pass unix:/tmp/fastcgi. soke // establish a connection with cgi through the socket file, which is set in the php-fpm.conf

Fastcgi_index index. php;

Fastcgi_param SCRIPT_FILENAME html $ SCRIPT_FILE_NAME; set parameters

Include fastcgi_params; // import the fastcgi parameter configuration file, which is automatically generated during nginx installation.

}



How does php get the header using nginx?

The Nginx http module encapsulates environment variables differently from Apache when processing HTTP requests. In addition to some common variables related to the HTTP protocol, it also supports a series of Nginx self-contained variables, such as $ server_protocol and $ nginx_version in the fastcgi_params.default file under the Nginx configuration directory. As used in the example in this file, these variables can be passed to the cgi program when fastcgi is configured, so that they can be used as the environment variables of the cgi program. However, even with these self-contained variables, Nginx cannot fully meet all requirements.

If you know Jquery, you will find that Jquery uses setRequestHeader ('x-Requested-with', 'xmlhttprequest ') to implement Ajax ') the method automatically adds an X-Requested-With request header With the value "xmlhttprequest" to identify this Ajax request, in this case, the backend that processes the request can identify the request type by judging the identity. In this case, how does PHP obtain the value of this custom parameter?

People familiar with Apache and PHP will think of $ _ SERVER ["HTTP_X_REQUESTED_WITH"] for the first time. Good, this is a perfect solution for prime matches, but Nginx is not, this is determined by Nginx's positioning of its own work-Nginx is only responsible for HTTP. In the eyes of Nginx, PHP is just a backend. In terms of image, it only distributes requests, regardless of who it sends them. This means that we cannot expect Nginx to automatically pass some custom parameters to PHP Like Apache, and only have self-reliance. Simply put, if you want to directly call the value of the custom request header parameter like $ _ SERVER ["HTTP_X_REQUESTED_WITH"], you must manually add it to the fastcgi_params configuration, explicitly inform the cgi program to receive the message, otherwise Nginx will discard it.

For how to configure environment variables, refer to the fastcgi_params.default file, which is also mentioned in the previous blog "how to configure environment variables for virtual hosts in Nginx. For the above example, you only need to add a line to the fastcgi_params file:

? 12 # for Ajax fastcgi_param HTTP_X_REQUESTED_WITH $ http_x_requested_with;
In this way, After reloading Nginx configuration, you can call $ _ SERVER ["HTTP_X_REQUESTED_WITH"] in PHP to determine the request type. Pay attention to the following two points:

1. Names of custom request headers should not contain white spaces, colons, line breaks, or underscores.

When Nginx processes the header of the client request, it replaces the hyphen (-) in the name with the underscore "_", add "$ http _" to the lower case of all letters as the variable name corresponding to the name. For example, in the preceding Jquery example, setRequestHeader ('x-Requested-with', 'xmlhttprequest ') is a line of string in the HTTP request header: "X-Requested-With: xmlhttprequest ", after Nginx processing, a variable named $ http_x_requested_with is automatically generated and its value is "xmlhttprequest ". Note that the rule "-" replaces with underscore "_" indicates that Nginx cannot correctly recognize the request parameter name if it contains an underscore.

Ii. $ _ SERVER ["HTTP_X_REQUESTED_WITH"] ...... the remaining full text>

How does Nginx configure html suffix files to be executed as php dynamic files?

Nginx generally goes through the following phases when processing requests:
Read requests-> select a server based on the request header-> load configuration-> perform location Routing-> request address rewriting-> access permission pre-check-> access permission submission -> configuration item try_files processing-> content generation-> log output.
You can configure another configuration item to route all requests ending with .html to a loaction. The location forwards requests to the backend php-cgi through fastpass.
Add configuration in nginx. conf:
Location ~ *. *\. The port of html $ {# phpcgi can also use the unxi-socket format fastcgi_pass 127.0.0.1: 8900 ;#... other configurations}. All requests ending with html are routed to phpcgi for processing by php-cgi.


Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.