[LNMP]Nginx访问日志

简介:

访问日志

1、定义日志格式

1
2
3
4
  [root@plinuxos ~] # vi /usr/local/nginx/conf/nginx.conf
     log_format log001  '$remote_addr $http_x_forwarded_for [$time_local]'
     ' $host "$request_uri" $status'
     ' "$http_referer" "$http_user_agent"' ;

▎Nginx日志格式:

$remote_addr

客户端IP(公网IP)

$http_x_forwarded_for

代理服务器的IP

$time_local

服务器本地时间

$host

访问主机名(域名)

$request_uri

访问的url地址

$status

状态码

$http_referer

referer

$http_user_agent

user_agent

2、增加访问日志项

1
2
3
4
5
6
7
8
9
[root@plinuxos ~] # vi /usr/local/nginx/conf/vhost/default.conf
server
{
     listen 80 default_server;  
     server_name aaa.com;
     index index.html index.htm index.php;
     root  /data/wwwroot/default ;
     access_log  /tmp/default .log log001; ##增加该行,该行元素:访问日志,存储地址,格式名称
}

3、检查与重载

1
2
3
4
[root@plinuxos ~] # /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@plinuxos ~] # /usr/local/nginx/sbin/nginx -s reload

4、测试效果

1
2
3
4
5
6
7
[root@plinuxos ~] # curl -x127.0.0.1:80 aaa.com/index.html
“This is a default site.”
[root@plinuxos ~] # curl -x127.0.0.1:80 aaa1.com/index.html
“This is a default site.”
[root@plinuxos ~] # cat /tmp/default.log 
127.0.0.1 - [12 /Aug/2017 :10:34:45 +0800] aaa.com  "/index.html"  200  "-"  "curl/7.29.0"
127.0.0.1 - [12 /Aug/2017 :10:34:52 +0800] aaa1.com  "/index.html"  200  "-"  "curl/7.29.0"


日志切割

1、日志切割脚本

1
2
3
4
5
6
7
8
9
10
11
[root@plinuxos ~] # vi /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`

▎KILL参数:

  • INT:快速关闭,是当用户键入<Control-C>时由终端驱动程序发送的信号。

  • TERM:快速关闭,请求彻底终止某项执行操作,它期望接收进程清除自给的状态并退出。

  • HUP:平滑启动,重新加载配置文件。

  • QUIT:从容关闭。


2、执行脚本

1
2
3
4
5
6
7
8
9
10
11
[root@plinuxos tmp] # sh -x /usr/local/sbin/nginx_log_rotate.sh 
++  date  -d  '-1 day'  +%Y%m%d
+ d=20170811
+ logdir= /tmp/
+ nginx_pid= /usr/local/nginx/logs/nginx .pid
cd  /tmp/
++  ls  default.log
for  log  in  '`ls *.log`'
mv  default.log default.log-20170811
++  cat  /usr/local/nginx/logs/nginx .pid
/bin/kill  -HUP 89689

3、检查效果

1
2
[root@plinuxos tmp] # ls
default.log  default.log-20170811  mysql.sock  pear  php-fcgi.sock

4、定期任务计划

1
2
[root@plinuxos tmp] # crontab -e
0 0 * * *  /bin/bash  /usr/local/sbin/nginx_log_rotate .sh

5、清理过期日志

1
2
[root@plinuxos tmp] # find /tmp/ -name *.log-* -type f -mtime +10 |xagrs rm 
##也可以放在脚本里,定期清除


静态文件不记录日志和过期时间

1、编辑配置文件

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
[root@plinuxos default] # vi /usr/local/nginx/conf/vhost/default.conf
 
server
{
     listen 80 default_server;
     server_name aaa.com;
     index index.html index.htm index.php;
     root  /data/wwwroot/default ;
     access_log  /tmp/default .log log001;
     location ~ .*\.(gif|jpg|jpeg|png|bmp|swf)$
     {
           expires      7d;
           access_log off;
     }
     location ~ .*\.(js|css)$
     {
           expires      12h;
           access_log off;
     }
}

2、检查与重载

1
2
3
4
[root@plinuxos default] # /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@plinuxos default] # /usr/local/nginx/sbin/nginx -s reload

3、检查效果

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
[root@plinuxos default] # ls
index.html  pic001.gif
[root@plinuxos default] # curl -x127.0.0.1:80 aaa.com/pic001.gif -I
HTTP /1 .1 200 OK
Server: nginx /1 .12.1
Date: Sat, 12 Aug 2017 03:31:08 GMT
Content-Type: image /gif
Content-Length: 66698
Last-Modified: Sat, 12 Aug 2017 03:29:18 GMT
Connection: keep-alive
ETag:  "598e760e-1048a"
Expires: Sat, 19 Aug 2017 03:31:08 GMT
Cache-Control: max-age=604800
Accept-Ranges: bytes
 
[root@plinuxos tmp] # curl -x127.0.0.1:80 aaa.com/pic001 -I
HTTP /1 .1 404 Not Found
Server: nginx /1 .12.1
Date: Sat, 12 Aug 2017 03:34:43 GMT
Content-Type: text /html
Content-Length: 169
Connection: keep-alive
 
[root@plinuxos tmp] # cat /tmp/default.log
127.0.0.1 - [12 /Aug/2017 :11:34:43 +0800] aaa.com  "/pic001"  404  "-"  "curl/7.29.0"









本文转自Grodd51CTO博客,原文链接:http://blog.51cto.com/juispan/1955696 ,如需转载请自行联系原作者
相关实践学习
日志服务之使用Nginx模式采集日志
本文介绍如何通过日志服务控制台创建Nginx模式的Logtail配置快速采集Nginx日志并进行多维度分析。
相关文章
|
17天前
|
运维 应用服务中间件 Linux
LNMP详解(十三)——Nginx子页面详解
LNMP详解(十三)——Nginx子页面详解
15 3
|
24天前
|
运维 前端开发 应用服务中间件
LNMP详解(八)——Nginx动静分离实战配置
LNMP详解(八)——Nginx动静分离实战配置
27 0
|
23天前
|
运维 负载均衡 应用服务中间件
LNMP详解(九)——Nginx虚拟IP实战
LNMP详解(九)——Nginx虚拟IP实战
35 2
|
16天前
|
网络协议 应用服务中间件 Linux
centos7 Nginx Log日志统计分析 常用命令
centos7 Nginx Log日志统计分析 常用命令
29 2
|
17天前
|
运维 监控 应用服务中间件
LNMP详解(十四)——Nginx日志详解
LNMP详解(十四)——Nginx日志详解
16 2
|
18天前
|
应用服务中间件 网络安全 nginx
nginx配置https访问
nginx配置https访问
29 0
|
24天前
|
存储 监控 数据可视化
Nginx+Promtail+Loki+Grafana Nginx日志展示
通过这些步骤,你可以将Nginx的日志收集、存储、查询和可视化整合在一起。这样,你就可以在Grafana中轻松地创建和展示Nginx日志的图表和面板。
31 3
|
7月前
|
关系型数据库 MySQL 应用服务中间件
手动部署LNMP环境(Alibaba Cloud Linux 2)
本场景带您体验如何在Alibaba Cloud Linux 2.1903 LTS 64位操作系统的云服务器上搭建LNMP环境。
203 0
|
4月前
|
关系型数据库 应用服务中间件 nginx
基于Docker的LNMP环境微服务搭建
基于Docker的LNMP环境微服务搭建
基于Docker的LNMP环境微服务搭建
|
7月前
|
应用服务中间件 PHP nginx
基于Anolis OS 3快速搭建LNMP环境制作KodBox
本教程介绍如何搭建LNMP环境,其中本实验的LNMP分别代表Anolis OS 3、Nginx、Mariadb和PHP。
135 0