How to connect PHP to the Nginx server and parse Nginx logs

Source: Internet
Author: User
This article mainly introduces how to connect PHP to the Nginx server and parse Nginx logs. PHP + Nginx is also a very popular server construction solution. For more information, see Php and nginx integration

PHP-FPM is also a third-party FastCGI Process Manager, it is developed as a patch of PHP, also needs to be compiled together with the PHP source code during installation, that is to say, the PHP-FPM is compiled into the PHP kernel, so it is better in terms of processing performance; it is also much better in terms of processing high concurrency than the spawn-fcgi engine, so, it is recommended that Nginx + PHP/PHP-FPM this combination of PHP parsing.
FastCGI's main advantage is the dynamic language and HTTP Server separated, so Nginx and PHP/PHP-FPM are often deployed on different servers, to share the front-end Nginx Server pressure, enables Nginx to specifically handle static requests and forward dynamic requests, while PHP/PHP-FPM servers specifically parse PHP dynamic requests

# Fastcgi
FastCGI is a scalable and fast interface for communication between HTTP server and dynamic scripting language. Most popular HTTP servers support FastCGI, including Apache, Nginx, and lighttpd. FastCGI is also supported by many script languages, including PHP.
FastCGI is improved from CGI development. The main disadvantage of the traditional CGI interface method is poor performance, because every time the HTTP server encounters a dynamic program, it needs to restart the script parser to execute the parsing, and then the result is returned to the HTTP server. This is almost unavailable when processing highly concurrent access. In addition, the traditional CGI interface method has poor security and is rarely used.
The FastCGI interface adopts the C/S structure, which can separate the HTTP server and the script parsing server, and start one or more script parsing daemon processes on the script parsing server. When the HTTP server encounters a dynamic program, it can be directly delivered to the FastCGI process for execution, and then the result is returned to the browser. This method allows the HTTP server to process static requests or return the results of the dynamic script server to the client, which greatly improves the performance of the entire application system.
Nginx + FastCGI operating principles
Nginx does not support direct calling or parsing of external programs. all external programs (including PHP) must be called through the FastCGI interface. The FastCGI interface is a socket in Linux (this socket can be a file socket or an ip socket ). To call the CGI program, you also need a FastCGI wrapper (wrapper can be understood as the program used to start another program). This wrapper is bound to a fixed socket, such as a port or file socket. When Nginx sends a CGI request to this socket, through the FastCGI interface, wrapper accepts the request and then derives a new thread, this thread calls the interpreter or external program to process the script and read the returned data. then, wrapper transmits the returned data to Nginx through the FastCGI interface along a fixed socket. finally, nginx sends the returned data to the client, which is the entire operation process of Nginx + FastCGI.

Php and nginx integration
Php. ini: main configuration file of php

[root@server79 php-5.4.12]# cp php.ini-production /usr/local/lnmp/php/etc/php.ini

Copy the php startup script

[root@server79 fpm]# pwd/root/php-5.4.12/sapi/fpm[root@server79 fpm]# cp init.d.php-fpm /etc/init.d/php-fpm

Add executable permissions to the startup script

[root@server79 fpm]# chmod +x /etc/init.d/php-fpm[root@server79 ~]# vim /usr/local/lnmp/php/etc/php.inicgi.fix_pathinfo=0date.timezone = /Asia/Shanghai[root@server79 ~]# cp /usr/local/lnmp/php/etc/php-fpm.conf.default /usr/local/lnmp/php/etc/php-fpm.conf[root@server79 etc]# vim php-fpm.conf

Open Annotation pid = run/

Php-fpm.conf file parameter parsing
The global configuration file of PHP is php. ini. in the above step, the file has been copied to/usr/local/lnmp/php/etc/php. ini. You can configure php. ini based on different application requirements.
The following describes the configuration files for the PHP-FPM engine.
The default configuration file for the PHP-FPM is/usr/local/lnmp/php/etc/php-fpm.conf, depending on the installation path specified above.
Php-fpm.conf is a plain text file in XML format and its content is easy to understand. Here we will focus on several important configuration labels:
The listen_address label is the IP address and port configured for fastcgi process listening. the default value is 9000.

listen =

The tag user and group are used to set the user and user group for running the FastCGI process. Note that the user and user group specified here must be consistent with the user and user group specified in the Nginx configuration file.

user = nginxgroup = nginx

The label max_children is used to set the number of FastCGI processes. According to official recommendations, for servers with less than 2 GB of memory, only 64 processes can be enabled, and for servers with more than 4 GB of memory, 200 processes can be enabled.


The request_terminate_timeout label is used to set the time for FastCGI to execute the script. The default value is 0 s, that is, it can be infinitely executed and can be modified as needed.


The label rlimit_files is used to set the PHP-FPM's limit on opening file descriptors. the default value is 1024. The value of this label must be associated with the number of files opened in the Linux kernel. for example, to set this value to 65535, you must execute 'ulimit-HSn 65536 'on the Linux command line '.


The max_requests label indicates the maximum number of requests processed by each children. the default value is 500.

pm.max_requests = 500

The label allowed_clients is used to set the IP addresses allowed to access the FastCGI process parser. If the IP address is not specified here, the PHP resolution request forwarded by Nginx will not be accepted.

5. manage FastCGI processes
After configuring php-fpm, you can start the FastCGI process. There are two ways to start the fastcgi process:

/usr/local/php/bin/php-cgi --fpm 


/usr/local/php/sbin/php-fpm start 

We recommend that you use the second method to start the FastCGI process.
/Usr/local/php/sbin/php-fpm has other parameters, such as start | stop | quit | restart | reload | logrotate.
The meanings of each startup parameter are as follows:

  • Q start: start the FastCGI process of PHP.
  • Q stop: forcibly terminate the FastCGI process of PHP.
  • Q quit: smoothly terminate the FastCGI process of PHP.
  • Q restart: restart the FastCGI process of PHP.
  • Q reload: reload PHP. ini.
  • Q logrotate: enable log files again.
  • Reload is a very important parameter. it can reload the modified PHP without interrupting the FastCGI process in php. you can use php-fpm to smoothly change the PHP settings in FastCGI mode.

[root@server79 etc]# /etc/init.d/php-fpm start

Configure the main configuration file of nginx and open the interface with php

[Root @ server79 conf] # pwd/usr/local/lnmp/nginx/conf [root @ server79 conf] # vim nginx. confuser nginx; # location ~ \. Php $ {root html; fastcgi_pass 9000; // Local port 9000 fastcgi_index index. php; # fastcgi_param SCRIPT_FILENAME/scripts $ fastcgi_script_name; include fastcgi. conf ;} [root @ server79 conf] # nginx-t ^ C [root @ server79 conf] # nginx-s reload ^ C [root @ server79 html] # pwd/usr/local/lnmp/ nginx/html [root @ server79 html] # cat index. php <? Phpphpinfo ()?>

Test: enter php in the browser. the php page appears.

Parsing Nginx logs using PHP
Nginx log format

Access_log log format

 log_format main '$server_name$remote_addr$remote_user[$time_local]"$request"'       '$status$body_bytes_sent"$http_referer"'       '"$http_user_agent""$http_x_forwarded_for"'; 

Log parameters

Server_name: host name of the VM
Remote_addr: IP address of the remote client
Remote_user: remote client user name
Time_local: access time and time zone
Status: the http status code returned by the request.
Body_bytes_sent: size of the file body content sent to the client
Http_referer: from which the page link is accessed
Http_user_agent: client browser information
Http_x_forwarded_for: the real ip address of the client

Log delimiter
Use special printable character ^ A (ctrl + v, ctrl + a) as the log delimiter

Filter file content based on keywords

Extract the file content based on the keyword "weibo" in the http request

Php code

/*** Description: read the file content by row for filtering and matching ** @ return array */function readFileContent ($ filename) {$ weibo_content = array (); $ fh = @ fopen ($ filename, 'r'); if ($ fh) {while (! Feof ($ fh) {$ row = fgets ($ fh, 4096); $ row_arr = explode ("", $ row); if (isset ($ row_arr [3]) & preg_match ('/weibo/', $ row_arr [3]) {$ weibo_content [] = $ row_arr ;}} fclose ($ fh); return $ weibo_content ;}
Related Article

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: 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.