Parsing nginx diaries using php scripts

Source: Internet
Author: User
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 ;}



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.