- 12.10 Nginx访问日志
- 12.11 Nginx日志切割
- 12.12 静态文件不记录日志和过期时间# 12.10 Nginx访问日志
- 日志的格式-  vim /usr/local/nginx/conf/nginx.conf //搜索log_format
```
[root@localhost vhost]# vim /usr/local/nginx/conf/nginx.conferror_log /usr/local/nginx/logs/nginx_error.log crit;
pid /usr/local/nginx/logs/nginx.pid;
worker_rlimit_nofile 51200;
events
{use epoll;worker_connections 6000;
}
http
{include mime.types;default_type application/octet-stream;server_names_hash_bucket_size 3526;server_names_hash_max_size 4096;log_format aming  '$remote_addr $http_x_forwarded_for [$time_local]'' $host "$request_uri" $status'' "$http_referer" "$http_user_agent"';sendfile on;tcp_nopush on;keepalive_timeout 30;client_header_timeout 3m;client_body_timeout 3m;send_timeout 3m;connection_pool_size 256;client_header_buffer_size 1k;large_client_header_buffers 8 4k;request_pool_size 4k;17,21         10%[root@localhost vhost]# vim /usr/local/nginx/conf/nginx.conf[root@localhost vhost]# lsaaa.com.conf  test.com.conf[root@localhost vhost]#
```- $remote_addr    |    客户端IP(公网IP)
- $http_x_forwarded_for   | 代理服务器的IP
- $time_local    |    服务器本地时间
- $host    |    访问主机名(域名)
- $request_uri   |  访问的url地址
- $status  |    状态码
- $http_referer  |  referer
- $http_user_agent   |  user_agent- 之前拷贝的nginx.conf下就有关于访问日志的相关
打开配置文件```[root@localhost vhost]# vim test.com.confserver
{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 aming;}
~
:wq
[root@localhost vhost]# vim test.com.conf
```
- 然后检查配置 重新加载,测试
```
[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
[root@localhost vhost]# !curl
curl -x127.0.0.1:80 test4.com/admin/index.html/dkdkdkdk  -I
HTTP/1.1 404 Not Found
Server: nginx/1.12.1
Date: Thu, 19 Oct 2017 13:11:35 GMT
Content-Type: text/html
Content-Length: 169
Connection: keep-alive[root@localhost vhost]# curl -x127.0.0.1:80 test3.com/admin/index.html/dkdkdkdk  -I
HTTP/1.1 301 Moved Permanently
Server: nginx/1.12.1
Date: Thu, 19 Oct 2017 13:11:52 GMT
Content-Type: text/html
Content-Length: 185
Connection: keep-alive
Location: http://test.com/admin/index.html/dkdkdkdk[root@localhost vhost]# curl -x127.0.0.1:80 test2.com/admin/index.html/dkdkdkdk  -I
HTTP/1.1 301 Moved Permanently
Server: nginx/1.12.1
Date: Thu, 19 Oct 2017 13:12:04 GMT
Content-Type: text/html
Content-Length: 185
Connection: keep-alive
Location: http://test.com/admin/index.html/dkdkdkdk
```
- 查看日志文件
```[root@localhost vhost]# cat /tmp/test.com.log
127.0.0.1 - [19/Oct/2017:21:11:52 +0800] test3.com "/admin/index.html/dkdkdkdk" 301 "-" "curl/7.29.0"
127.0.0.1 - [19/Oct/2017:21:12:04 +0800] test2.com "/admin/index.html/dkdkdkdk" 301 "-" "curl/7.29.0"
[root@localhost vhost]# ```# 12.11 Nginx日志切割
- 因为nginx没有自带日志切割工具
所以只能借助系统自带的工具或者使用脚本实现
- 创建shell脚本
- 规则:所有的shell的脚本,以后全部存放在/usr/local/sbin/ 目录下
```
[root@localhost vhost]# vim /usr/local/sbin/nginx_logrotate.sh[1]+  已停止               vim /usr/local/sbin/nginx_logrotate.sh
[root@localhost vhost]# date -d "-1 day"
2017年 10月 18日 星期三 21:22:44 CST
[root@localhost vhost]# date -d "-1 day" +%Y%m%d
20171018
[root@localhost vhost]# date
2017年 10月 19日 星期四 21:23:14 CST
[root@localhost vhost]# fg
vim /usr/local/sbin/nginx_logrotate.sh[1]+  已停止               vim /usr/local/sbin/nginx_logrotate.sh
[root@localhost vhost]# ls /usr/local/nginx/logs/nginx.pid
/usr/local/nginx/logs/nginx.pid
[root@localhost vhost]# fg
vim /usr/local/sbin/nginx_logrotate.sh[1]+  已停止               vim /usr/local/sbin/nginx_logrotate.sh
[root@localhost vhost]# ls
aaa.com.conf  test.com.conf
[root@localhost vhost]# for f in `ls `;do ls -l $f;done
-rw-r--r--. 1 root root 142 10月 17 21:20 aaa.com.conf
-rw-r--r--. 1 root root 287 10月 19 21:11 test.com.conf
[root@localhost vhost]# fg
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 in `ls *.log`
domv $log $log-$d
done
/bin/kill -HUP `cat $nginx_pid`
~                                                                                        ~                                                                                        3,13         全部
:wq
```
- 现在就可以去执行下shell  用sh -x 查看过程
```
[root@localhost vhost]# sh -x  /usr/local/sbin/nginx_logrotate.sh
++ date -d '-1 day' +%Y%m%d
+ d=20171018
+ logdir=/tmp/
+ nginx_pid=/usr/local/nginx/logs/nginx.pid
+ cd /tmp/
++ ls php_errors.log test.com.log
+ for log in '`ls *.log`'
+ mv php_errors.log php_errors.log-20171018
+ for log in '`ls *.log`'
+ mv test.com.log test.com.log-20171018
++ cat /usr/local/nginx/logs/nginx.pid
+ /bin/kill -HUP 965
[root@localhost vhost]# ls /tmp/
mysql.sock
pear
php_errors.log-20171018
php-fcgi.sock
systemd-private-c7ea2fb090494a1192f9d0cfbca1de2c-vmtoolsd.service-c7k0sX
test.com.log
test.com.log-20171018
[root@localhost vhost]#
```
- 长此以往每天会日志切割,过段时间要要做 定时清理日志  (因为没有符合的条件)
```
[root@localhost vhost]# find /tmp/ -name *.log-* -type f -mtime +30 | xargs rm
rm: 缺少操作数
Try 'rm --help' for more information.
[root@localhost vhost]# find /tmp/ -name *.log-* -type f -mtime +30 | xargs rm ^C
[root@localhost vhost]# find /tmp/ -name *.log-* -type f
/tmp/php_errors.log-20171018
/tmp/test.com.log-20171018
[root@localhost vhost]#
```
- 日志脚本
```
[root@localhost vhost]# cat /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 in `ls *.log`
domv $log $log-$d
done
/bin/kill -HUP `cat $nginx_pid`
[root@localhost vhost]#
```
- 写完脚本之后,还需要加个任务计划 crontab -e
```
[root@localhost vhost]# crontab -e
no crontab for root - using an empty one0 0 * * 8 /bin/bash /usr/local/sbin/nginx_logrotate.sh
~
~                                                                                        :q!
```
- 因为是做测试的所以不保存# 12.12 静态文件不记录日志和过期时间
- 打开配置文件 vi test.com.conf
```
[root@localhost vhost]# vi test.com.confserver
{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 aming;}
~
~
"test.com.conf" 12L, 287C
```
- 加入以下配置
```
[root@localhost vhost]# vi test.com.confserver
{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;}
~
~
-- INSERT --[root@localhost vhost]# vi test.com.conf
[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
[root@localhost vhost]#
```
- 先来模拟一个图片
```
[root@localhost vhost]# cd /data/wwwroot//test.com/
[root@localhost test.com]# ls
admin  index.html
[root@localhost test.com]# vim 1.gif
[root@localhost test.com]# vim 2.js
[root@localhost test.com]# curl -x127.0.0.1:80 test.com/1.gif
dkdldldkdkdkd
[root@localhost test.com]# curl -x127.0.0.1:80 test.com/2.js
kdkdkdkdk
[root@localhost test.com]# curl -x127.0.0.1:80 test.com/index.html
test.com
[root@localhost test.com]#
```
- 再来查看日志
```
[root@localhost test.com]# cat /tmp/test.com.log
127.0.0.1 - [19/Oct/2017:22:02:53 +0800] test.com "/index.html" 200 "-" "curl/7.29.0"
[root@localhost test.com]#
```
- 再来crul 一下,,又生成新的一条
```
[root@localhost test.com]# curl -x127.0.0.1:80 test.com/index.html
test.com
[root@localhost test.com]# cat /tmp/test.com.log
127.0.0.1 - [19/Oct/2017:22:02:53 +0800] test.com "/index.html" 200 "-" "curl/7.29.0"
127.0.0.1 - [19/Oct/2017:22:03:58 +0800] test.com "/index.html" 200 "-" "curl/7.29.0"
[root@localhost test.com]#
```- 再来curl -x127.0.0.1:80 test.com/2.js
```
[root@localhost test.com]# curl -x127.0.0.1:80 test.com/2.js
kdkdkdkdk
[root@localhost test.com]# cat /tmp/test.com.log
127.0.0.1 - [19/Oct/2017:22:02:53 +0800] test.com "/index.html" 200 "-" "curl/7.29.0"
127.0.0.1 - [19/Oct/2017:22:03:58 +0800] test.com "/index.html" 200 "-" "curl/7.29.0"
[root@localhost test.com]#
[root@localhost test.com]#
[root@localhost test.com]#
[root@localhost test.com]#
[root@localhost test.com]#
```
- 这个说明访问js的时候 不会记录日志,
- 再来在js后面加点东西 curl -x127.0.0.1:80 test.com/2.jslasdflk 这样就会记录
- 因为这个/2.jslasdflk 不匹配刚刚的语句,所以会记录
```
[root@localhost test.com]# curl -x127.0.0.1:80 test.com/2.jslasdflk
<html>
<head><title>404 Not Found</title></head>
<body bgcolor="white">
<center><h1>404 Not Found</h1></center>
<hr><center>nginx/1.12.1</center>
</body>
</html>
[root@localhost test.com]# cat /tmp/test.com.log
127.0.0.1 - [19/Oct/2017:22:02:53 +0800] test.com "/index.html" 200 "-" "curl/7.29.0"
127.0.0.1 - [19/Oct/2017:22:03:58 +0800] test.com "/index.html" 200 "-" "curl/7.29.0"
127.0.0.1 - [19/Oct/2017:22:06:06 +0800] test.com "/2.jslasdflk" 404 "-" "curl/7.29.0"
[root@localhost test.com]#
```
- 下面来测试下它的过期时间 ,max-age=43200
```
[root@localhost test.com]# curl -x127.0.0.1:80 -I test.com/2.js
HTTP/1.1 200 OK
Server: nginx/1.12.1
Date: Thu, 19 Oct 2017 14:07:53 GMT
Content-Type: application/javascript
Content-Length: 10
Last-Modified: Thu, 19 Oct 2017 14:02:16 GMT
Connection: keep-alive
ETag: "59e8b068-a"
Expires: Fri, 20 Oct 2017 02:07:53 GMT
Cache-Control: max-age=43200
Accept-Ranges: bytes[root@localhost test.com]#
```-
```location ~ .*\.(js|css)${
#          expires      12h;access_log off;}access_log /tmp/test.com.log aming;}
~                                                                                        -- INSERT --[root@localhost test.com]# /usr/local/nginx/sbin/nginx -s reload
[root@localhost test.com]# !curl
curl -x127.0.0.1:80 -I test.com/2.js
HTTP/1.1 200 OK
Server: nginx/1.12.1
Date: Thu, 19 Oct 2017 14:12:15 GMT
Content-Type: application/javascript
Content-Length: 10
Last-Modified: Thu, 19 Oct 2017 14:02:16 GMT
Connection: keep-alive
ETag: "59e8b068-a"
Accept-Ranges: bytes[root@localhost test.com]#
```
- 这就是怎么样定义过期时间,以及指定某些请求不去记录日志

转载于:https://blog.51cto.com/ch71smas/1974739

12.10 Nginx访问日志 12.11 Nginx日志切割 12.12 静态文件不记录日志和过期时间相关推荐

  1. Nginx访问日志、日志切割、静态文件不记录日志和过期时间

    2019独角兽企业重金招聘Python工程师标准>>> 11月27日任务 12.10 Nginx访问日志 12.11 Nginx日志切割 12.12 静态文件不记录日志和过期时间 1 ...

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

    2019独角兽企业重金招聘Python工程师标准>>> 静态文件不记录日志和过期时间目录概要 配置如下 location ~ .*\.(gif|jpg|jpeg|png|bmp|sw ...

  3. Nginx设置静态页面压缩和缓存过期时间的方法 (转)

    使用nginx服务器的朋友可能都知道需要设置html静态页面缓存与页面压缩与过期时间的设置了,下面我来给各位同学介绍一下配置方法,包括对ico,gif,bmp,jpg,jpeg,swf,js,css, ...

  4. (五)nginx设置静态文件不记录日志

    一.配置test.com.conf配置文件 添加下面代码 location ~ .*\.(gif|jpg|jpeg|png|bmp|swf)$ { expires 7d; access_log off ...

  5. 6月8日任务(12.10 Nginx访问日志 12.11 Nginx日志切割 12.12 静态文件)

    课程名称:12.10 Nginx访问日志 笔记内容: [root@localhost ~]# vim /usr/local/nginx/conf/nginx.conf 定义日志格式 combined_ ...

  6. Nginx默认虚拟主机、 Nginx用户认证、Nginx域名重定向、访问日志·····

    2019独角兽企业重金招聘Python工程师标准>>> Ngninx默认虚拟主机 1.vim /usr/local/nginx/conf/nginx.conf //增加 2.incl ...

  7. VMware Workstation 9下基于Ubuntu 12.10服务器版本的Hadoop集群的配置

    (主要参照http://os.51cto.com/art/201211/363116.htm) 一.概述 其实,网上关于Hadoop集群的配置的资料不可谓不少,但是,细细找来,多半是在有图形桌面的环境 ...

  8. Ubuntu 12.10 正式发布

    Canonical今天正式发布了Ubuntu 12.10版本,代号为"Quantal Quetzal",意为量子绿咬鹃,绿咬鹃是一种生活在美洲的颜色极为鲜艳的鸟.Ubuntu的版本 ...

  9. Nginx(四):Nginx配置实战

    相关阅读: Nginx(一):Nginx原理概述 与 安装步骤详解 Nginx(二):反向代理原理 与 配置文件详解 Nginx(三):负载均衡策略 与 Nginx静态服务器 Nginx(四):Ngi ...

最新文章

  1. zabbix 3.0 完全安装全解!
  2. 关于ASP.NET控件DropDownList控件的问题
  3. 090_块元素行内元素行内块元素空元素
  4. VI和VIM编辑器深入学习笔记--基本vi命令
  5. mvc创建连接mysql_MVC+EF6-CodeFirst 连接MySQL并创建数据库和表_Demo
  6. Difference between expressions and statements?
  7. 我的世界boat运行库JAVA10_我的世界boatmod模组大全
  8. android 蓝牙耳机 sco,使用Android识别器内置蓝牙耳机
  9. 关于 ie8不兼容的一些方法
  10. 创建featureclass,为它赋别名,并移动到数据集下
  11. php mysql 复制数据库表结构图_MySQL_Mysql复制表结构、表数据的方法,本文给大家分享了好几种复制 - phpStudy...
  12. Android 系统应用Setting开发总结
  13. i5200u处理器 HD5500集成显卡安装黑苹果教程
  14. 企业外贸网站建设的要求及注意事项
  15. 超长干货!最全数据指标分析!
  16. 什么是C端 什么是B端 这里告诉你
  17. 手把手教你音乐服务器搭建
  18. 2019.12.26
  19. 使用freemarker导出word文档包含多张图片
  20. 【089】MESHY-ios12壁纸风格的渲染图生成器

热门文章

  1. spring25:@Pointcut管理和定义切入点
  2. 从扫描序列的标准化做起,西门子医疗正在中国布这样一盘棋...
  3. python 获取中文文件名的输出
  4. 五种js判断是否为整数类型方式
  5. 计算机网络实验五:虚拟局域网技术
  6. 【微职位公开课】老学长自述:如何成为年薪50W的技术工程师
  7. Linux指定网卡工作模式
  8. GSON使用笔记(1) -- 序列化时排除字段的几种方式
  9. javascript回调函数(模式)原理和示例深入分析
  10. Zabbix监控 之 OID动态索引