12.10 Nginx Access Log
Log format
vim/usr/local/nginx/conf/nginx.conf//Search Log_format
$remote_addr 客户端IP(公网IP)$http_x_forwarded_for 代理服务器的IP$time_local 服务器本地时间$host 访问主机名(域名)$request_uri 访问的url地址$status 状态码$http_referer referer$http_user_agent user_agent
In addition to defining the log format in the Master Profile nginx.conf, you also need to add the virtual host configuration file
Access_log/tmp/1.log Combined_realip;
The combined_realip here is the name of the log format defined in nginx.conf
-T &&-S reload
Curl-x127.0.0.1:80 Test.com-i
Cat/tmp/1.log
Operation Process
[[email protected] ~]# vim /usr/local/nginx/conf/nginx.conf
Format Combined_realip is the log format represented
Log_format combined_realip ' $remote _addr $http _x_forwarded_for [$time _local] '
' $host ' $request _uri "$status"
' "$http _referer" "$http _user_agent";
[Email protected] ~]# vim/usr/local/nginx/conf/vhost/test.com.conf
Server
{
Listen 80;
server_name test.com test2.com test3.com;
Index index.html index.htm index.php;
root/data/wwwroot/test.com;
if ($host! = ' test.com ') {
Rewrite ^/(. *) $ http://test.com/$1 permanent;
}
Access_log/tmp/test.com.log Combined_realip;
}
[[email protected] ~]#/usr/local/nginx/sbin/nginx-tnginx:the configuration file/usr/local/nginx/conf/ nginx.conf syntax is oknginx:configuration file/usr/local/nginx/conf/nginx.conf test is successful[[email Protected] ~]#/usr/local/nginx/sbin/nginx-s reload[[email protected] ~]# curl-x127.0.0.1:80 test2.com/admin/ index.html-ihttp/1.1 301 Moved Permanentlyserver:nginx/1.14.0date:thu, April 2018 14:28:19 GMTCONTENT-TYPE:TEXT/HTMLC Ontent-length:185connection:keep-alivelocation:http://test.com/admin/index.html[[email protected] ~]# Curl- x127.0.0.1:80 test3.com/admin/index.html-ihttp/1.1 301 Moved Permanentlyserver:nginx/1.14.0date:thu, APR 2018 14:28 : gmtcontent-type:text/htmlcontent-length:185connection:keep-alivelocation:http://test.com/admin/index.html[[ Email protected] ~]# cat/tmp/test.com.log 127.0.0.1-[19/apr/2018:22:28:19 +0800] test2.com "/admin/index.html" 301 "-" "curl/7.29.0" 127.0.0.1-[19/apr/2018:22:28:25 +0800] test3.com "/admin/index.html" 301 "-" "curl/7.29.0"
12.11 Nginx Log Cutting
Custom shell Scripts
Vim/usr/local/sbin/nginx_log_rotate.sh//writes the following:
#! /bin/bash
Assuming that the Nginx log storage path is/data/logs/
D= date -d "-1 day" +%Y%m%d
Logdir= "/data/logs"
Nginx_pid= "/usr/local/nginx/logs/nginx.pid"
CD $logdir
For log inls *.log
Do
MV $log $log-$d
Done
/bin/kill-hupcat $nginx_pid
Task Scheduler
0 0 */bin/bash/usr/local/sbin/nginx_log_rotate.sh
Operation Process
[[email protected] ~]# vim /usr/local/sbin/nginx_logrotate.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 inls *.log
Do
MV $log $log-$d
Done
/bin/kill-hupcat $nginx_pid
[[email protected] ~]# sh -x /usr/local/sbin/nginx_logrotate.sh[[email protected] ~]# crontab -e0 0 * * * /bin/bash /usr/local/sbin/nginx_logrotate.sh
12.12 static files do not log logs and expire time
Configure as follows to add to the virtual host configuration file
Location ~. . (gif|jpg|jpeg|png|bmp|swf) $
{
Expires 7d;
Access_log off;
}
Location ~. (JS|CSS) $
{
Expires 12h;
Access_log off;
}
Operation Process
[[email protected] ~]# vim /usr/local/nginx/conf/vhost/test.com.conf
Server
{
Listen 80;
server_name test.com test2.com test3.com;
Index index.html index.htm index.php;
root/data/wwwroot/test.com;
if ($host! = ' test.com ') {
Rewrite ^/(.) $ http://test.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/test.com.log aming;
}
[[email protected] ~]# cd /data/wwwroot/test.com/[[email protected] test.com]# touch 1.gif[[email protected] test.com]# touch 2.js
Nginx Test not restarted
[[email protected] test.com]# curl -x127.0.0.1:80 test.com/1.gif[[email protected] test.com]# curl -x127.0.0.1:80 test.com/2.js[[email protected] test.com]# curl -x127.0.0.1:80 test.com/index.htmltest.comtest.com[[email protected] test.com]# cat /tmp/test.com.log127.0.0.1 - [20/Apr/2018:17:55:13 +0800] test.com "/1.gif" 200 "-" "curl/7.29.0"127.0.0.1 - [20/Apr/2018:17:55:20 +0800] test.com "/2.js" 200 "-" "curl/7.29.0"127.0.0.1 - [20/Apr/2018:17:55:39 +0800] test.com "/index.html" 200 "-" "curl/7.29.0"
Restart Nginx test
[[email protected] ~]# /usr/local/nginx/sbin/nginx -tnginx: the configuration file /usr/local/nginx/conf/nginx.conf syntax is oknginx: configuration file /usr/local/nginx/conf/nginx.conf test is successful[[email protected] ~]# /usr/local/nginx/sbin/nginx -s reload[[email protected] ~]# curl -x127.0.0.1:80 test.com/1.gif[[email protected] ~]# curl -x127.0.0.1:80 test.com/2.js[[email protected] ~]# curl -x127.0.0.1:80 test.com/index.htmltest.comtest.com[[email protected] ~]# cat /tmp/test.com.log127.0.0.1 - [20/Apr/2018:17:55:13 +0800] test.com "/1.gif" 200 "-" "curl/7.29.0"127.0.0.1 - [20/Apr/2018:17:55:20 +0800] test.com "/2.js" 200 "-" "curl/7.29.0"127.0.0.1 - [20/Apr/2018:17:55:39 +0800] test.com "/index.html" 200 "-" "curl/7.29.0"127.0.0.1 - [20/Apr/2018:17:57:27 +0800] test.com "/index.html" 200 "-" "curl/7.29.0"
Time expiration feature set success max-age=43200
[[email protected] ~]# curl -x127.0.0.1:80 test.com/2.js -IHTTP/1.1 200 OKServer: nginx/1.14.0Date: Fri, 20 Apr 2018 09:58:16 GMTContent-Type: application/javascriptContent-Length: 0Last-Modified: Fri, 20 Apr 2018 09:54:41 GMTConnection: keep-aliveETag: "5ad9b8e1-0"Expires: Fri, 20 Apr 2018 21:58:16 GMTCache-Control: max-age=43200Accept-Ranges: bytes[[email protected] ~]# cat /tmp/test.com.log127.0.0.1 - [20/Apr/2018:17:55:13 +0800] test.com "/1.gif" 200 "-" "curl/7.29.0"127.0.0.1 - [20/Apr/2018:17:55:20 +0800] test.com "/2.js" 200 "-" "curl/7.29.0"127.0.0.1 - [20/Apr/2018:17:55:39 +0800] test.com "/index.html" 200 "-" "curl/7.29.0"127.0.0.1 - [20/Apr/2018:17:57:27 +0800] test.com "/index.html" 200 "-" "curl/7.29.0"
2018-04-25 Linux Learning