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     - How to generate Linux, random password encryption or decryption (Linux)

- Distributed Hadoop1.2.1 cluster installation (Server)

- How to use Android Studio to play more package names APK (Programming)

- Linux, Chrome browser font settings beautification (Linux)

- How to disable IPv6 on Ubuntu, Linux Mint, Debian (Linux)

- Linux System Getting Started Tutorial: Five image processing software (Linux)

- With screenfetch linux logo and basic hardware information display with cool Linux logo (Linux)

- Awk include binding capacity larger than the specified size of all files directory (Linux)

- LNMP Note: Addressing mail function can not send mail (Server)

- Java Concurrency: synchronized (Programming)

- Use of the storage-level replication technology will quickly clone a ASM database to the target environment (Database)

- Android using shape drawable material production (Programming)

- To read the Linux ext3 / ext4 format partitions under Windows system software (Linux)

- Installation and Configuration JDK8 In CentOS 7 (Linux)

- Help you make Git Bisect (Linux)

- Ubuntu Server 14.04 installation Web server (Linux + Apache + MySQL + PHP) (Server)

- CentOS install video converter FFmpeg and cutting tools segmenter (Linux)

- How to run in support UDp Docker container JBoss data virtualization GA (Server)

- Linux port scanning (Linux)

- How to choose the correct HTTP status code (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:      
 
- SecureCRT in Python scripting study guide (Programming)
- Linux batch copy data script (Linux)
- Bash Automated Customization Linux belongs to its own CentOS system (Linux)
- How to add two-factor authentication for Linux systems SSH (Linux)
- Python extension module Ganglia 3.1.x (Linux)
- C language - Traverse pci device (Programming)
- ActionContext and ServletActionContext Summary (Programming)
- extundelete: the Linux-based open source data recovery tools (Linux)
- 10 really interesting Linux command (Linux)
- C ++ How to determine the types of constants (Programming)
- Use source packages compiled and installed GCC5.1 in Mac OS X 10.10.3 (Linux)
- Red Hat Linux mount U disk (Linux)
- Ubuntu 14.04 forget solution root password (Linux)
- Linux Operating System Security Study (Linux)
- Talking about modern programming language syntax and standard library tightly bound phenomenon (Programming)
- Solve the compatibility problem between Linux and Java at the source in bold font (Linux)
- Java implementation chain store binary tree (Programming)
- Root of AVL Tree- achieve balanced search trees AVL tree (Programming)
- Understanding Linux firewall Iptables (Linux)
- Java Virtual Machine Basics (Programming)
     
           
     
  CopyRight 2002-2016 newfreesoft.com, All Rights Reserved.