Home >Backend Development >PHP Tutorial >PHP method to connect to Nginx server and parse Nginx logs_php tips

PHP method to connect to Nginx server and parse Nginx logs_php tips

WBOY
WBOYOriginal
2016-05-16 20:08:291659browse

Integration of php and nginx

PHP-FPM is also a third-party FastCGI process manager. It is developed as a patch for PHP. It also needs to be compiled together with the PHP source code during installation, which means that PHP-FPM is compiled into the PHP kernel. , so it is better in terms of processing performance; at the same time, it is much better than the spawn-fcgi engine in handling high concurrency. Therefore, the Nginx PHP/PHP-FPM combination is recommended to parse PHP.
The main advantage of FastCGI is to separate dynamic languages ​​​​from HTTP Server, so Nginx and PHP/PHP-FPM are often deployed on different servers to share the pressure on the front-end Nginx server, so that Nginx can exclusively handle static requests and forward dynamic requests. request, while the PHP/PHP-FPM server exclusively parses PHP dynamic requests

#fastcgi
FastCGI is a scalable, high-speed interface for communication between HTTP servers and dynamic scripting languages. Most popular HTTP servers support FastCGI, including Apache, Nginx and lighttpd. At the same time, FastCGI is also supported by many scripting languages, including PHP.
FastCGI is developed and improved from CGI. The main disadvantage of the traditional CGI interface method is poor performance, because every time the HTTP server encounters a dynamic program, the script parser needs to be restarted to perform parsing, and then the results are returned to the HTTP server. This is almost unavailable when dealing with high concurrent access. In addition, the traditional CGI interface method has poor security and is rarely used now.
The FastCGI interface adopts a C/S structure, which can separate the HTTP server and the script parsing server, and start one or more script parsing daemons on the script parsing server. Every time the HTTP server encounters a dynamic program, it can be delivered directly to the FastCGI process for execution, and then the result is returned to the browser. This method allows the HTTP server to exclusively 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 principle
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 under Linux (this socket can be a file socket or an ip socket). In order to call a CGI program, a FastCGI wrapper is also needed (a wrapper can be understood as a 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, the wrapper receives the request and then spawns a new thread. This thread calls the interpreter or external program to process the script and read the return data; then, the wrapper The returned data is passed to Nginx along the fixed socket through the FastCGI interface; finally, Nginx sends the returned data to the client. This is the entire operation process of Nginx FastCGI.

php and nginx integration
php.ini: PHP’s main configuration file

[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.ini
cgi.fix_pathinfo=0
date.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 the comment pid = run/php-fpm.pid

php-fpm.conf file parameter analysis
PHP’s global configuration file is php.ini. In the above steps, this file has been copied to /usr/local/lnmp/php/etc/php.ini. php.ini can be configured accordingly according to the needs of each application.
The following focuses on the configuration file of the PHP-FPM engine.
According to the installation path specified above, the default configuration file of PHP-FPM is /usr/local/lnmp/php/etc/php-fpm.conf.
php-fpm.conf is a plain text file in XML format, and its content is easy to understand. Here we focus on several important configuration tags:
The label listen_address configures the IP address and port that the fastcgi process listens to. The default is 127.0.0.1:9000.

listen = 127.0.0.1:9000

The tags user and group are used to set the user and user group running the FastCGI process. It should be noted that the user and user group specified here must be consistent with the user and user group specified in the Nginx configuration file.

user = nginx
group = nginx

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

<value name="max_children">5</value>

The tag request_terminate_timeout is used to set the time for FastCGI to execute the script. The default is 0s, which means it will be executed indefinitely. It can be modified according to the situation.

<value name="request_terminate_timeout">0s</value>

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

<value name="rlimit_files">1024</value>

The tag max_requests specifies the maximum number of requests each child can handle before being closed. The default setting is 500.

pm.max_requests = 500

标签allowed_clients用于设置允许访问FastCGI进程解析器的IP地址。如果不在这里指定IP地址,Nginx转发过来的PHP解析请求将无法被接受。

<value name="allowed_clients">127.0.0.1</value>

5.管理FastCGI进程
在配置完php-fpm后,就可以启动FastCGI进程了。启动fastcgi进程有两种方式:

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

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

建议采用第二种方式启动FastCGI进程。
/usr/local/php/sbin/php-fpm还有其他参数,具体为start|stop|quit|restart|reload|logrotate。
每个启动参数的含义如下:

  • q start,启动PHP的FastCGI进程。
  • q stop,强制终止PHP的FastCGI进程。
  • q quit,平滑终止PHP的FastCGI进程。
  • q restart,重启PHP的FastCGI进程。
  • q reload,重新加载PHP的php.ini。
  • q logrotate,重新启用log文件。
  • reload是个很重要的参数,它可以在PHP的FastCGI进程不中断的情况下重新加载改动过的php.ini,因此通过php-fpm可以平滑变更FastCGI模式下的PHP设置。
[root@server79 etc]# /etc/init.d/php-fpm start

配置nginx的主配置文件,打开与php的接口

[root@server79 conf]# pwd
/usr/local/lnmp/nginx/conf
[root@server79 conf]# vim nginx.conf
user nginx;
#
location ~ \.php$ {
root html; 
fastcgi_pass 127.0.0.1:9000; //本地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
<&#63;php
phpinfo()&#63;>

测试:浏览器中输入192.168.0.179/index.php,出现php页面


PHP解析Nginx日志
nginx日志格式

access_log日志格式

 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"'; 


日志参数

    server_name          : 虚拟主机的主机名称 
    remote_addr          : 远程客户端的ip地址 
    remote_user          : 远程客户端用户名称 
    time_local           : 访问的时间与时区 
    status           : 记录请求返回的http状态码 
    body_bytes_sent      : 发送给客户端的文件主体内容的大小 
    http_referer             : 从哪个页面链接访问过来  
    http_user_agent      : 客户端浏览器信息 
    http_x_forwarded_for     : 客户端的真实ip 


日志分割符
使用特殊的不可打印字符^A(ctrl+v,ctrl+a)作为日志分割符


根据关键字过滤文件内容

需求
根据http的请求里是否有“weibo”这个关键字提取文件的内容

php代码

 /** 
  * Description:按行读取文件内容进行过滤匹配 
  * 
  * @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; 
 } 

Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn