LNMP schema access log, log cutting, static file not recording and expiration time setting

November 27 mission 12.10 Nginx access log 12.11 Nginx log cutting 12.12 static files do not record logs and expiration times Nginx access log Modify ...

November 27 mission

12.10 Nginx access log

12.11 Nginx log cutting

12.12 static files do not record logs and expiration times

Nginx access log

  • Modify nginx configuration file
[root@localhost vhost]# vim /usr/local/nginx/conf/nginx.conf # Search / log? Format # In nginx, it ends with; as a line, so the following code is a configuration # Format: log format log format name format log_format test '$remote_addr $http_x_forwarded_for [$time_local]' ' $host "$request_uri" $status' ' "$http_referer" "$http_user_agent"';

The variables used in the format are described as follows:

Variable name Explain $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 visited $status Status code $http_referer referer $http_user_agent user_agent
  • Define the log path within the virtual host
# Insert in server block access_log /tmp/test.com.log test; # The format is access log path log format name (defined in the main configuration file)
  • Restart service
[root@localhost vhost]# /usr/local/nginx/sbin/nginx -t nginx: the configuration file /usr/local/nginx/conf/nginx.conf syntax is ok nginx: configuration file /usr/local/nginx/conf/nginx.conf test is successful [root@localhost vhost]# /usr/local/nginx/sbin/nginx -s reload
  • Validation effect
[root@localhost vhost]# curl -x 127.0.0.1:80 test.com [root@localhost vhost]# curl -x 127.0.0.1:80 test2.com/index.php [root@localhost vhost]# curl -x 127.0.0.1:80 test2.com/index1.php # Successful record [root@localhost vhost]# cat /tmp/test.com.log 127.0.0.1 - [...:19:05:22 +0800] test.com "/" 401 "-" "curl/7.29.0" 127.0.0.1 - [...:19:05:34 +0800] test2.com "/index.php" 301 "-" "curl/7.29.0" 127.0.0.1 - [...:19:05:39 +0800] test2.com "/index1.php" 301 "-" "curl/7.29.0"

Nginx log cutting

nginx does not have the rotatelog log cutting command in apache. We can implement the log cutting function by customizing the shell script.

  • Create custom script
[root@localhost vhost]# vim /usr/local/sbin/nginx_log_rotate.sh [root@localhost vhost]# cat /usr/local/sbin/nginx_log_rotate.sh #!/bin/bash # date +%Y%m%d shows today's date # Add - d "-1 day" to show yesterday's date d=`date -d "-1 day" +%Y%m%d` # Define the path of log storage, defined in the virtual host configuration file logdir="/tmp" # pid file nginx_pid="/usr/local/nginx/logs/nginx.pid" cd $logdir # Cycle to change the log file name under the log storage path for log in `ls *.log` do mv $log $log-$d done # Restart without shutting down the process, equivalent to nginx -s reload /bin/kill -HUP `cat $nginx_pid`
  • Script testing
# sh -x can display the process of script execution [root@localhost vhost]# sh -x /usr/local/sbin/nginx_log_rotate.sh ++ date -d '-1 day' +%Y%m%d + d=20180102 + 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-20180102 ++ cat /usr/local/nginx/logs/nginx.pid + /bin/kill -HUP 1299 # Check whether the function is realized [root@localhost vhost]# ls /tmp/*.log* /tmp/test.com.log /tmp/test.com.log-20180102
  • Periodic execution with crontab command
[root@localhost vhost]# crontab -e 0 0 * * * /bin/bash /usr/local/sbin/nginx_log_rotate.sh

Static files do not log and expire

  • Modify virtual host profile
[root@localhost vhost]# vim /usr/local/nginx/conf/vhost/test.com.conf # ~Matching subsequent regular representations # Use \ escape. To match. jpg and other files location ~ .*\.(gif|jpg|jpeg|png|bmp|swf)$ { # expires set expiration time expires 7d; # Turn off logging access_log off; } location ~ .*\.(css|js)$ { expires 12h; access_log off; }
  • test
  1. Verify that static files do not log
[root@localhost vhost]# curl -x 127.0.0.1:80 test.com/index.php <html> <head><title>401 Authorization Required</title></head> <body bgcolor="white"> <center><h1>401 Authorization Required</h1></center> <hr><center>nginx/1.12.2</center> </body> </html> [root@localhost vhost]# curl -x 127.0.0.1:80 test.com/1.gif jhasdifhoai [root@localhost vhost]# curl -x 127.0.0.1:80 test.com/2.js abdsuofghan # gif/js file log does not record access log [root@localhost vhost]# cat /tmp/test.com.log 127.0.0.1 - [...:19:36:25 +0800] test.com "/index.php" 401 "-" "curl/7.29.0"
  1. Validation expiration time
  • Expiration time of test gif
[root@localhost vhost]# curl -x 127.0.0.1:80 test.com/1.gif -I HTTP/1.1 200 OK Server: nginx/1.12.2 Date: Wed, ... 11:36:52 GMT Content-Type: image/gif Content-Length: 12 Last-Modified: Wed, ... 11:35:29 GMT Connection: keep-alive ETag: "5a4cc001-c" Expires: Wed, ... 11:36:52 GMT # Max age time displayed in the message Cache-Control: max-age=604800 Accept-Ranges: bytes
  • Test js file expiration time
[root@localhost vhost]# curl -x 127.0.0.1:80 test.com/2.js -I HTTP/1.1 200 OK Server: nginx/1.12.2 Date: Wed, ... 11:36:58 GMT Content-Type: application/javascript Content-Length: 12 Last-Modified: Wed, ... 11:35:44 GMT Connection: keep-alive ETag: "5a4cc010-c" Expires: Wed, ... 23:36:58 GMT # Max age time displayed in the message Cache-Control: max-age=43200 Accept-Ranges: bytes
  • Test PHP file, no max age information
[root@localhost vhost]# curl -x 127.0.0.1:80 test.com/index.php HTTP/1.1 200 OK Server: nginx/1.12.2 Date: Wed, ... 11:46:58 GMT Content-Type: application/octet-stream Content-Length: 19 Last-Modified: Wed, ... 11:36:44 GMT Connection: keep-alive ETag: "5a4e1572-13" Accept-Ranges: bytes

4 December 2019, 09:23 | Views: 8374

Add new comment

For adding a comment, please log in
or create account

0 comments