Parsing nginx logs using a php script & #26684; type access_log & #26684; type log_formatmain '$ server_name $ remote_addr $ remote_user [$ time_local] & quot; $ requ php script parses nginx logs
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: virtual host name remote_addr: remote client IP address remote_user: remote client user name time_local: access time and time zone status: records the http status code body_bytes_sent returned by the request: size of the file body 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
The log delimiter uses special printable characters ^ A (ctrl + v, ctrl + a) as the log delimiter.
Filter file content based on keywords
Whether the keyword "weibo" exists in the http request to extract the file content
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 ;}