LNMP schema (Nginx access log, Nginx log cutting, static file does not record log and expiration time)

Nginx access log

1. Open the configuration file and search for log? Format

vim /usr/local/nginx/conf/nginx.conf

2. Meaning of common variables in access log

  • $remote_addr: client IP (public IP)
  • $HTTP "X" forwarded "for: IP of proxy server
  • $time_local: server local time
  • $host: access host name (domain name)
  • $request_uri: url address of the visit
  • $status: status code
  • $http_referer : referer
  • $http_user_agent : user_agent

3. Here we change the original rule name combined [realip] to yolks [realip and save it

4. Modify the virtual host configuration file

  1. Edit virtual machine profile
vim /usr/local/nginx/conf/vhost/test.com.conf

2) Add the following code

access_log /tmp/1.log yolks_realip; #The following name represents the log rule name of the main configuration file. If it is not specified here, the default log rule will be used. The display content is simple

5. Check the configuration file and reload it

/usr/local/nginx/sbin/nginx -t #inspect
/usr/local/nginx/sbin/nginx -s reload #restart

6.curl test

[root@yolks2 ~]# curl -x127.0.0.1:80 test2.com/admin/admin.html -I 
HTTP/1.1 301 Moved Permanently
Server: nginx/1.6.3
Date: Tue, 14 Aug 2018 14:45:04 GMT
Content-Type: text/html
Content-Length: 184
Connection: keep-alive
Location: http://test.com/admin/admin.html

7. View error log format

[root@yolks2 ~]# cat /tmp/test.com.log - [14/Aug/2018:22:45:04 +0800] test2.com "/admin/admin.html" 301 "-" "curl/7.29.0"

Nginx log cutting

Log cutting script implementation, namely shell implementation

1. Edit the file / usr / local / SBIN / nginx? Log? Rotate.sh, and add the following code

## Assume that the log storage path of nginx is / data/logs/
d=`date -d "-1 day" +%Y%m%d` #Generate yesterday's date: for example, 20180813
logdir="/tmp/" #Define log directory
nginx_pid="/usr/local/nginx/logs/nginx.pid" #pid number
cd $logdir #Entry directory
for log in `ls *.log` #log from loop ls
    mv $log $log-$d #Modify name
/bin/kill -HUP `cat $nginx_pid` #Kill the process and restart it. Use / usr/local/nginx/sbin/nginx -s reload to replace it

2. Execute script command: sh

  • x: View execution process

Executive order

[root@yolks2 ~]# sh -x /usr/local/sbin/nginx_log_rotate.sh

Execution process

++ date -d '-1 day' +%Y%m%d
+ d=20180813
+ logdir=/tmp/
+ nginx_pid=/usr/local/nginx/logs/nginx.pid
+ cd /tmp/
++ ls test.com.log
+ for log in '`ls *.log`'
+ mv test.com.log test.com.log-20180813
++ cat /usr/local/nginx/logs/nginx.pid
+ /bin/kill -HUP 885

3. View the test * file in the / tmp / directory

[root@yolks2 ~]# ls /tmp/ |grep test*

4. Regularly clean up old logs

find /tmp/ -name *.log-* -type f -mtime +30 |xargs rm #Delete log files 30 days ago

5. Add Scheduled Task Plan

1) Edit scheduled task file

crontab -e

2) The planned task code is as follows

0 0 * * * /bin/bash /usr/local/sbin/nginx_log_rotate.sh #Execute at 0 o'clock every day

Static files do not log and expire

1. Edit the configuration file * * / usr/local/nginx/conf/vhost/test.com.conf * * and add the following code:

	location ~ .*\.(gif|jpg|jpeg|png|bmp|swf)$ #Match static file suffix,. gif|.jpg|.jpeg|.png|.bmp|.swf
          expires      7d; #Expiration date
          access_log off; #Access log switch
	location ~ .*\.(js|css)$ #Match. js and. css files
          expires      12h; #Expiration date
          access_log off; #Access log switch

2. Write test documents, such as pictures

[root@yolks2 ~]# touch /data/wwwroot/test.com/test.jpg
[root@yolks2 ~]# ls /data/wwwroot/test.com/
admin  index.html  test.jpg

3. test

curl -x127.0.0.1:80 test.com/test.jpg -I

View log:

Keywords: Nginx curl vim shell

Added by pietbez on Sat, 04 Jan 2020 21:41:18 +0200