[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 ,如需转载请自行联系原作者
相关实践学习
【涂鸦即艺术】基于云应用开发平台CAP部署AI实时生图绘板
【涂鸦即艺术】基于云应用开发平台CAP部署AI实时生图绘板
相关文章
|
数据可视化 关系型数据库 MySQL
ELK实现nginx、mysql、http的日志可视化实验
通过本文的步骤,你可以成功配置ELK(Elasticsearch, Logstash, Kibana)来实现nginx、mysql和http日志的可视化。通过Kibana,你可以直观地查看和分析日志数据,从而更好地监控和管理系统。希望这些步骤能帮助你在实际项目中有效地利用ELK来处理日志数据。
1026 90
|
安全 应用服务中间件 网络安全
如何测试Nginx反向代理实现SSL加密访问的配置是否正确?
如何测试Nginx反向代理实现SSL加密访问的配置是否正确?
1014 60
|
安全 应用服务中间件 网络安全
配置Nginx反向代理实现SSL加密访问的步骤是什么?
我们可以成功地配置 Nginx 反向代理实现 SSL 加密访问,为用户提供更安全、可靠的网络服务。同时,在实际应用中,还需要根据具体情况进行进一步的优化和调整,以满足不同的需求。SSL 加密是网络安全的重要保障,合理配置和维护是确保系统安全稳定运行的关键。
1098 60
|
应用服务中间件 Linux 网络安全
nginx安装部署ssl证书,同时支持http与https方式访问
为了使HTTP服务支持HTTPS访问,需生成并安装SSL证书,并确保Nginx支持SSL模块。首先,在`/usr/local/nginx`目录下生成RSA密钥、证书申请文件及自签名证书。接着,确认Nginx已安装SSL模块,若未安装则重新编译Nginx加入该模块。最后,编辑`nginx.conf`配置文件,启用并配置HTTPS服务器部分,指定证书路径和监听端口(如20000),保存后重启Nginx完成部署。
5508 8
|
监控 应用服务中间件 定位技术
要统计Nginx的客户端IP,可以通过分析Nginx的访问日志文件来实现
要统计Nginx的客户端IP,可以通过分析Nginx的访问日志文件来实现
1630 3
|
Web App开发 算法 应用服务中间件
nginx开启局域网https访问
【10月更文挑战第22天】为了调试WebRTC功能,需要在局域网内搭建HTTPS协议。具体步骤包括:在已部署Nginx和安装OpenSSL的环境中生成私钥、证书签名请求和自签名证书;将生成的文件放置到Nginx的证书目录并修改Nginx配置文件,最后重启Nginx服务。注意,自签名证书不受第三方机构认可,如需正式使用,需向CA申请签名。
1506 2
|
应用服务中间件 Shell PHP
windows系统配置nginx环境运行pbootcms访问首页直接404的问题
windows系统配置nginx环境运行pbootcms访问首页直接404的问题
|
应用服务中间件 nginx
nginx error日志 client intended to send too large body: 1434541 bytes 如何处理?
【8月更文挑战第27天】nginx error日志 client intended to send too large body: 1434541 bytes 如何处理?
1181 6
|
Ubuntu 应用服务中间件 数据库
Nginx配置:阻止非国内IP地址访问的设置方法
此外,出于用户隐私和法律合规性的考虑,应慎重考虑阻止特定国家或地区IP地址的决策。在某些情况下,这可能被视为歧视性或违反当地法律。
1634 3
|
应用服务中间件 Linux nginx
在Linux中,如何统计ip访问情况?分析 nginx 访问日志?如何找出访问页面数量在前十位的ip?
在Linux中,如何统计ip访问情况?分析 nginx 访问日志?如何找出访问页面数量在前十位的ip?