Home IT Linux Windows Database Network Programming Server Mobile  
           
  Home \ Server \ Timing Nginx logs cut and remove the log records of the specified number of days before     - Oracle set the timer task JOB scheduler to execute stored procedures or PL / SQL code block (Database)

- Recent Consolidation Using Linux security module (Linux)

- Linux / proc directory Comments (Linux)

- Linux configuration startup mount: fstab file (Linux)

- Monitor traffic Linux Shell Edition (Programming)

- Profile Linux users login shell and login to read (Linux)

- Analysis of C ++ exception mechanism (Programming)

- Performance Optimization: Using Ramlog transfer log files to memory (Linux)

- Linux system package manager -RPM (Linux)

- Linux unpack the tar file to a different directory (Linux)

- LAMP and LNMP automated installation scripts (Server)

- Java implementation heap sort (large root heap) (Programming)

- Linux desktop system using the remote server in clear text ssh password (Server)

- using Docker Kitematic on windows (Linux)

- Compile and install GCC 4.8.1 + GDB 7.6.1 + Eclipse in CentOS 6.4 in (Linux)

- Oracle 12c R2 new feature dbca command to create a standby database (Database)

- Android WebView use layman (Programming)

- Killall five options you might not know (Linux)

- git checkout generated in the use of temporary br (Linux)

- MogileFS system installation configuration example (Server)

 
         
  Timing Nginx logs cut and remove the log records of the specified number of days before
     
  Add Date : 2018-11-21      
         
       
         
  Nginx logs into the log access.log and error.log; wherein access.log record which users, and what pages the user's browser, ip, and other access information; error.log is recording server error log.

Error.log log form as follows:
201.158.69.116 - - [03 / Jan / 2013: 21: 17: 20 -0600] fwf [-] tip [-] 127.0.0.1:9000 0.007 0.007 MX pythontab.com GET /html/test.html HTTP / 1.1 " 200 "2426" http://a.com "" es-ES, es; q = 0.8 "" Mozilla / 5.0 (Windows NT 6.1) AppleWebKit / 537.11 (KHTML, like Gecko) Chrome / 23.0.1271.97 Safari / 537.11 "
187.171.69.177 - - [03 / Jan / 2013: 21: 17: 20 -0600] fwf [-] tip [-] 127.0.0.1:9000 0.006 0.006 MX pythontab.com GET /html/test2.html HTTP / 1.1 " 200 "2426" http://a.com "" es-ES, es; q = 0.8 "" Mozilla / 5.0 (Windows NT 6.1) AppleWebKit / 537.11 (KHTML, like Gecko) Chrome / 23.0.1271.97 Safari / 537.11 "

From the above we can see several pieces of information:

1. The client (user) IP address. Such as: the example of 201.158.69.116

2. The access time. Such as: in the example above [03 / Jan / 2013: 21: 17: 20 -0600]

3. Access Port. Such as: in the example above 127.0.0.1:9000

4. response time. Such as: in the example above 0.007

5. request time. Such as: in the example above 0.007

6. The user location codes (country code). Such as: the example of the MX (Mexico)

url address (target url address) 7. The requested host. Such as: in the example above pythontab.com

8. The request method (GET or POST, etc.). Such as: the example of the GET

9. Requests url address (removing the host portion). Such as: in the example above /html/test.html

10. The status of the request (status code 200 indicates success, 404 page does not exist, 301 permanent redirect, the specific status code can find related articles on the Internet, not repeat them). Such as: the example of the "200"

11. The requested page size, the default is B (byte). Such as: the example of 2426

12. The source of the page, from which page to page, professional name is called the "referer". Such as: the example of the "http://a.com"

13. The user's browser language. Such as: the example of the "es-ES, es; q = 0.8"

14. For additional information the user's browser, browser version, browser type and so on. Such as: the example of "Mozilla / 5.0 (Windows NT 6.1) AppleWebKit / 53 7.11 (KHTML, like Gecko) Chrome / 23.0.1271.97 Safari / 537.11"

Access.log log format is not static, it can be customized. In the nginx nginx.conf profiles found: log_format this is the log format
    #log_format main '$ remote_addr - $ remote_user [$ time_local] "$ request"'
    # '$ Status $ body_bytes_sent "$ http_referer"'
    # ' "$ Http_user_agent" "$ http_x_forwarded_for"';

    #access_log logs / access.log main;

log_format is HttpLog instruction Nginx module for Nginx logs to specify the output format. main name for this log output format can be referenced in the following access_log directive.

eg:
192.168.21.1 - - [27 / Jan / 2014: 11: 28: 53 +0800] "GET /2.php HTTP / 1.1" 200 133 "-" "Mozilla / 5.0 (Windows NT 6.1; WOW64) AppleWebKit / 537.36 ( KHTML, like Gecko) Chrome / 33.0.1707.0 Safari / 537.36 "" - "192.168.21.128 200 127.0.0.1:9000 0.119 0.119

$ Remote_addr: client address 192.168.21.1

$ Remote_user: Client users -

$ Time_local: Time and Time Zone 27 / Jan / 2014: 11: 28: 53 +0800

$ Request: URL path of the request and the HTTP protocol GET /2.php HTTP / 1.1

$ Status: HTTP Status 200

$ Body_bytes_sent: page size sent to the client 133

$ Http_referer: page jump Source -

$ Http_user_agent: User Access Terminal Mozilla / 5.0 (Windows NT 6.1; WOW64) AppleWebKit / 537.36 (KHTML, like Gecko) Chrome / 33.0.1707.0 Safari / 537.36

$ Http_x_forwarded_for: HTTP proxy, the requester's IP -

$ Http_host: user input in the browser URL (IP or a domain name) address 192.168.21.128

$ Upstream_status: upstream state 200

$ Upstream_addr: backend upstream address and port 127.0.0.1:9000

$ Request_time: 0.119 Total time page views

$ Upstream_response_time: page views in upstream response time 0.119

If between the client and the Web server adds an intermediate layer (such as a reverse proxy server), then the Web server can not directly get the client lP, by $ remote_addr variable will get the reverse proxy server IP address. However, the reverse proxy server in the HTTP header information transfer request, can increase the X-Forwarded-For information, to record the server address of the original client lP address and the original client requests.

At this time, you must use log_format command to set the log format, so logging X-Forwarded-For information in the lP address that the client real IP. For example, create a log format called mylogformat, and then $ http_x_forwarded_for variable records the user's X-Forwarded-For lP Address:
    log_format mylogformat '$ http_x_forwarded_for - $ remote_user [$ time_local] "$ request"'
                      '$ Status $ body_bytes_sent "$ http_referer"'
                      ' "$ Http_user_agent" "$ http_x_forwarded_for"';

If you do not want to log, you can use the following instructions to turn off logging:
 access_log off

Cutting nginx log file

Production environment servers, since the access log files grow very fast, server logs too seriously affecting the efficiency of the General Assembly. Meanwhile, in order to facilitate analysis and calculation of the log, the log files need to be regularly cut. Regular monthly cutting methods are cutting, cutting by the day, by the hour cutting. The most common is cut by the day.
Nginx does not support the use of the same as Apache cronolog to rotate the log files, but you can use the following method to achieve the cutting of the log file:
mv /usr/local/nginx/logs/access.log /usr/local/nginx/logs/access_20150519084513.log
 kill -USR1 Nginx master process ID
By mv command log file is renamed to /usr/local/nginx/logs/access_20150519084513.log, and then send a signal to kill -USR1 Nginx master process ID, let Nginx regenerate a new log file / usr / local / nginx /logs/access.log.
If you want to cut logs regularly every day, also need aid crontab. We can write a log cutting by day, by year, month directory for shell scripts log:
[Root @ localhost logs] # vim /usr/local/nginx/sbin/cut_nginx_log.sh

#! / Bin / bash
logs_path = "/ usr / local / nginx / logs /"
DAYS = 30
mv $ {logs_path} access.log $ {logs_path} access _ $ (date -d "yesterday" + "% Y% m% d% H% M% S"). log
#mv $ ​​{logs_path} access.log $ {logs_path} access _ $ (date + "% Y Year% m month% d day% H% M minute% S at second week% w"). log
kill -USR1 `cat / usr / local / nginx / logs / nginx.pid`
find $ {logs_path} -name ". access _ * log" -type f -mtime + $ DAYS -exec rm {} \;
[Root @ localhost logs] # chmod + x /usr/local/nginx/sbin/cut_nginx_log.sh
[Root @ localhost logs] # crontab -e
0 0 * * * /usr/local/nginx/sbin/cut_nginx_log.sh
[Root @ localhost logs] # service crond restart
Stopping crond: [OK]
Starting crond: [OK]
[Root @ localhost logs] # chkconfig crond on
[Root @ localhost logs] #

If the script appears the following error during execution
nginx: [error] open () "/usr/local/nginx/logs/nginx.pid" failed

Solution:
[Root @ localhost logs] # / usr / local / nginx / sbin / nginx -c /usr/local/nginx/conf/nginx.conf
     
         
       
         
  More:      
 
- Linux smart small switch rfkill (Linux)
- CentOS NAT iptables (Linux)
- Use calcurse schedule appointments and to-do in the Linux terminal (Linux)
- Ubuntu cut screen method (Linux)
- Installation Strongswan: on a Linux IPsec-based VPN tool (Linux)
- Configure the ASM process on Red Hat Linux 6.5 (Database)
- Ubuntu 14.04 modify environment variables (Linux)
- 10 Best Swift Tutorial examples (Programming)
- How to configure Ceph stored on CentOS 7.0 (Server)
- CentOS static IP network configuration (Linux)
- RMAN backup file is much larger than the size of the database Cause Analysis (Database)
- Proficient in C / C ++ can also find a very good job (Programming)
- GitLab upgrade to 8.2.0 (Linux)
- ORA-30926 and MERGE tables empty the temporary occupation problem (Database)
- Ubuntu 14.04 to install Blender 2.71 (Linux)
- Nodejs command-line program development tutorial (Programming)
- After installation of Debian 6.0 do a few things first (Linux)
- Oracle archive log deletion (Database)
- Servlet 3.0 interfaces of AsyncListener (Programming)
- Java 8 perspective annotation types (Programming)
     
           
     
  CopyRight 2002-2016 newfreesoft.com, All Rights Reserved.