Nginx access log
View the current log format in the main profile
Search log? Format
[root@test-a /]# cd /usr/local/nginx/ [root@test-a nginx]# vim conf/nginx.conf include mime.types; default_type application/octet-stream; server_names_hash_bucket_size 3526; server_names_hash_max_size 4096; log_format combined_realip '$remote_addr $http_x_forwarded_for [$time_local]' ' $host "$request_uri" $status' ' "$http_referer" "$http_user_agent"';
Meaning of related fields
Meaning of field
--- | ---
$remote | addr | client IP (public IP)
$http ᠄ forward ᠄ IP of proxy server
$time | local | server local time
$host | access host name (domain name)
$request | URI | url address accessed
$status | status code
$http_refer | referer
$http_user_agent | user_agent
Define log format for virtual machine
For the service configuration corresponding to the virtual host configuration file, add access? Log / TMP / abc.com.log combined? Realip; (the combined? Realip here is the name of the log format defined in nginx.conf); reload the configuration and test
[root@test-a nginx]# vim conf/vhost/abc.com.conf [root@test-a nginx]# cat conf/vhost/abc.com.conf server { listen 80; server_name abc.com ab.com a.com; index index.html index.htm index.php; root /data/wwwroot/abc.com; if ($host != 'abc.com'){ rewrite ^/(.*)$ http://abc.com/$1 permanent; } access_log /tmp/abc.com.log combined_realip; # } [root@test-a 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@test-a nginx]# ./sbin/nginx -s reload [root@test-a nginx]# curl -utest1:test1 -x127.0.0.1:80 abc.com This is abc.com site. [root@test-a nginx]# vim conf/vhost/abc.com.conf [root@test-a nginx]# cat /tmp/abc.com.log 127.0.0.1 - [28/Nov/2018:07:15:06 +0800] abc.com "/" 200 "-" "curl/7.29.0"
Shell script cutting log
Nginx doesn't have its own log cutting tool. Write a script to do the same.
[root@test-a nginx]# vim /usr/local/sbin/nginx_log_rotate.sh [root@test-a nginx]# cat /usr/local/sbin/nginx_log_rotate.sh #!/bin/bash d=`date -d "-1 day" +%Y%m%d` logdir="/tmp" nginx_pid="/usr/local/nginx/logs/nginx.pid" cd $logdir for log in `ls *.log` do mv $log $log-$d done /bin/kill -HUP `cat $nginx_pid` # Make log file rebuild [root@test-a nginx]# ls /tmp/ abc.com.log pear php-fcgi.sock web-1 [root@test-a nginx]# sh /usr/local/sbin/nginx_log_rotate.sh [root@test-a nginx]# ls /tmp/ abc.com.log abc.com.log-20181127 pear php-fcgi.sock web-1 [root@test-a nginx]# date Wed Nov 28 07:37:54 CST 2018
Set the timing task for the script, and execute the script change at zero every day
[root@test-a nginx]# crontab -e 0 0 * * * /bin/bash /usr/local/sbin/nginx_log_rotate.sh
Do not log static files and set expiration time
[root@test-a nginx]# vim conf/vhost/abc.com.conf [root@test-a nginx]# cat conf/vhost/abc.com.conf server { listen 80; server_name abc.com ab.com a.com; index index.html index.htm index.php; root /data/wwwroot/abc.com; if ($host != 'abc.com'){ rewrite ^/(.*)$ http://abc.com/$1 permanent; } location ~ .*\.(gif|jpg|jpeg|png|bmp|swf)$ { expires 7d; access_log off; } location ~ .*\.(js|css)$ { expires 12h; access_log off; } access_log /tmp/abc.com.log combined_realip; } [root@test-a 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@test-a nginx]# ./sbin/nginx -s reload [root@test-a nginx]# cat /tmp/abc.com.log [root@test-a nginx]# curl -utest1:test1 -x127.0.0.1:80 abc.com -I HTTP/1.1 200 OK Server: nginx/1.14.1 Date: Tue, 27 Nov 2018 23:59:05 GMT Content-Type: text/html Content-Length: 22 Last-Modified: Mon, 26 Nov 2018 21:36:51 GMT Connection: keep-alive ETag: "5bfc6773-16" Accept-Ranges: bytes [root@test-a nginx]# cat /tmp/abc.com.log 127.0.0.1 - [28/Nov/2018:07:59:05 +0800] abc.com "/" 200 "-" "curl/7.29.0" [root@test-a nginx]# curl -utest1:test1 -x127.0.0.1:80 abc.com/1.jpg -I HTTP/1.1 404 Not Found Server: nginx/1.14.1 Date: Tue, 27 Nov 2018 23:59:21 GMT Content-Type: text/html Content-Length: 169 Connection: keep-alive [root@test-a nginx]# cat /tmp/abc.com.log 127.0.0.1 - [28/Nov/2018:07:59:05 +0800] abc.com "/" 200 "-" "curl/7.29.0" [root@test-a nginx]# curl -utest1:test1 -x127.0.0.1:80 abc.com/1.js -I HTTP/1.1 404 Not Found Server: nginx/1.14.1 Date: Wed, 28 Nov 2018 00:00:46 GMT Content-Type: text/html Content-Length: 169 Connection: keep-alive [root@test-a nginx]# cat /tmp/abc.com.log 127.0.0.1 - [28/Nov/2018:07:59:05 +0800] abc.com "/" 200 "-" "curl/7.29.0" [root@test-a nginx]# curl -utest1:test1 -x127.0.0.1:80 abc.com/1.jpg -I # After adding the corresponding image resources, you can see the cache control expiration time HTTP/1.1 200 OK Server: nginx/1.14.1 Date: Wed, 28 Nov 2018 00:03:09 GMT Content-Type: image/jpeg Content-Length: 4 Last-Modified: Wed, 28 Nov 2018 00:02:59 GMT Connection: keep-alive ETag: "5bfddb33-4" Expires: Wed, 05 Dec 2018 00:03:09 GMT Cache-Control: max-age=604800 Accept-Ranges: bytes