前往小程序,Get更优阅读体验!
立即前往
首页
学习
活动
专区
工具
TVP
发布
社区首页 >专栏 >Linux基础(day50)

Linux基础(day50)

作者头像
运维小白
发布2018-02-06 15:54:00
6720
发布2018-02-06 15:54:00
举报
文章被收录于专栏:运维小白运维小白

12.10 Nginx访问日志

Nginx访问日志目录概要

  • 日志格式
  • vim /usr/local/nginx/conf/nginx.conf //搜索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

  • 除了在主配置文件nginx.conf里定义日志格式外,还需要在虚拟主机配置文件中增加
  • access_log /tmp/1.log combined_realip;
  • 这里的combined_realip就是在nginx.conf中定义的日志格式名字 -t && -s reload
  • curl -x127.0.0.1:80 test.com -I
  • cat /tmp/1.log

Nginx访问日志

  • 日志的文件也是在主配置文件中
  1. 打开主配置文件vim /usr/local/nginx/conf/nginx.conf
代码语言:javascript
复制
[root@hanfeng vhost]# vim /usr/local/nginx/conf/nginx.conf

搜索/log_format 找到以下内容,就是来定义日志格式的
 log_format combined_realip '$remote_addr $http_x_forwarded_for [$time_local]'
    ' $host "$request_uri" $status'
    ' "$http_referer" "$http_user_agent"';
  • combined_realip 日志格式的名字,可以随便定义,这里定义成什么名字,后面就引用成什么名字,决定了虚拟主机引用日志的类型
  • 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(标识)

  • 若想自己的公网IP,可以直接百度IP,就会出来自己上网的IP地址
  1. 除了在主配置文件nginx.conf里定义日志格式外,还需要在虚拟主机配置文件去定义access_log /tmp/1.log combined_realip; 来定义访问日志路径
  • vim /usr/local/nginx/conf/vhost/test.com.conf
代码语言:javascript
复制
[root@hanfeng vhost]# vim test.com.conf

在第一个括号中添加access_log /tmp/1.log combined_realip;即可

server
{
    listen 80;
    server_name test.com test1.com test2.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;
}
保存退出
  • 如果不写日志格式,那就会走默认的日志格式
  1. 然后检查配置文件是否存在语法错误,并重新加载配置文件
代码语言:javascript
复制
[root@hanfeng 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@hanfeng vhost]# /usr/local/nginx/sbin/nginx -s reload
[root@hanfeng vhost]# 
  1. 测试
代码语言:javascript
复制
[root@hanfeng vhost]# curl -x127.0.0.1:80 test1.com -I
HTTP/1.1 301 Moved Permanently
Server: nginx/1.12.1
Date: Thu, 04 Jan 2018 14:15:18 GMT
Content-Type: text/html
Content-Length: 185
Connection: keep-alive
Location: http://test.com/

[root@hanfeng vhost]# curl -x127.0.0.1:80 test2.com -I
HTTP/1.1 301 Moved Permanently
Server: nginx/1.12.1
Date: Thu, 04 Jan 2018 14:15:25 GMT
Content-Type: text/html
Content-Length: 185
Connection: keep-alive
Location: http://test.com/

[root@hanfeng vhost]# 
  1. 查看日志cat /tmp/test.com.log
代码语言:javascript
复制
[root@hanfeng vhost]# cat /tmp/test.com.log
127.0.0.1 - [04/Jan/2018:22:15:18 +0800] test1.com "/" 301 "-" "curl/7.29.0"
127.0.0.1 - [04/Jan/2018:22:15:25 +0800] test2.com "/" 301 "-" "curl/7.29.0"
[root@hanfeng vhost]# 

12.11 Nginx日志切割

Nginx日志切割目录概要

  • 自定义shell 脚本
  • vim /usr/local/sbin/nginx_log_rotate.sh//写入如下内容
代码语言:javascript
复制
#! /bin/bash
## 假设nginx的日志存放路径为/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 in `ls *.log`
do
    mv $log $log-$d
done
/bin/kill -HUP `cat $nginx_pid`
  • 任务计划
  • 0 0 * * * /bin/bash /usr/local/sbin/nginx_log_rotate.sh

Nginx日志切割

  • Nginx没有自带日志切割工具,只能借助系统的日志切割的工具或者自己写切割的脚本实现
  1. 这里写一个日志切割脚本
  2. 首先创建一个shell脚本vim /usr/local/sbin/nginx_log_rotate.sh
  • 所有的shell脚本放入到/usr/local/sbin/目录下
代码语言:javascript
复制
[root@hanfeng vhost]# vim /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`
  • d=date -d “-1 day” +%Y%m%d // 生成昨天的日期,格式为年月日
  • logdir=”tmp/” // 上一节的时候,定义了日志存放在/tmp/目录下
  • nginx_pid=”/usr/local/nginx/logs/nginx.pid” //查找nginx的PID,目的是为了执行/bin/kill -HUP cat $nginx_pid ,而这个命令目的和nginx -s relo 是一样的
  • cd $logdir //进入“logdir”日志目录下
  • for log in ls *.log //开始语句循环,看错有哪些 log后缀的文件
  • do //执行
  • mv $log $log-$d //将 log改名为《原名字-“`date -d “-1 day” +%Y%m%d` ”这个结尾的文件 》
  • done //结束
  • /bin/kill -HUP cat $nginx_pid // 重新加载,生成一个新的“nginx_pid=”/usr/local/nginx/logs/nginx.pid”
代码语言:javascript
复制
[root@hanfeng vhost]# ls
aaa.com.conf  test.com.conf
[root@hanfeng vhost]# for f in `ls `; do ls -l $f ; done
-rw-r--r--. 1 root root 140 1月   3 23:17 aaa.com.conf
-rw-r--r--. 1 root root 295 1月   4 22:07 test.com.conf
[root@hanfeng vhost]# 
  1. 执行shell脚本,并加-x权限
  • -x选项,是为了查看脚本执行的过程
代码语言:javascript
复制
[root@hanfeng vhost]# sh -x /usr/local/sbin/nginx_log_rotate.sh
++ date -d '-1 day' +%Y%m%d
+ d=20180103
+ logdir=/tmp/
+ nginx_pid=/usr/local/nginx/logs/nginx.pid
+ cd /tmp/
++ ls test.com.log
+ for log in '`ls *.log`'
+ mv test.com.log test.com.log-20180103
++ cat /usr/local/nginx/logs/nginx.pid
+ /bin/kill -HUP 1612
[root@hanfeng vhost]# 
  1. 查看日志切割文件,每天都生成一个日志,在每天切割后,过段时间还要定期清理
代码语言:javascript
复制
[root@hanfeng vhost]# ls /tmp/
mysql.sock  pear  php-fcgi.sock  test.com.log  test.com.log-20180103
[root@hanfeng vhost]# 
  1. 删除30天以前的日志文件
代码语言:javascript
复制
[root@hanfeng vhost]# find /tmp/ -name *.log-* -type f -mtime +30 |xargs rm
  1. 写完脚本后,还要加一个任务计划crontab -e——>这里因为是测试,脚本就不加入到任务计划中了
代码语言:javascript
复制
[root@hanfeng vhost]# crontab -e

0 0 * * * /bin/bash /usr/local/sbin/nginx_log_rotate.sh

shell脚本知识点

  • 知识点:
  1. 日志时间切割的定义
  • 写shell脚本的时候,如果有命令不明白,可以直接把命令运行一下就知道结果了
  • 假设这个命令“ d=date -d “-1 day” +%Y%m%d ”不明白意思
  • ctrl+z 把当前操作暂停丢到后台
代码语言:javascript
复制
[root@hanfneg ~]# date -d “-1 day” +%Y%m%d
20180103
[root@hanfeng vhost]# date
2018年 01月 04日 星期四 23:27:23 CST
  • 就是时间,而且是昨天的时间,因为目前做的日志切割都是以天为单位,而且,日志需要过了当天23点59分59秒以后到第二天的0点0分01秒才切割
  1. 指定PID路径的意义
  • “ nginx_pid=”/usr/local/nginx/logs/nginx.pid” ”这条命令的意思,就是指定nginx的PID 的路径所在
  • 如果找不到指定PID的所在,那么下面的“ /bin/kill -HUP cat $nginx_pid ”这个命令也将没有办法继续执行
  • “ /bin/kill -HUP cat $nginx_pid ” z这条命令的意思就是重新加载一次nginx服务
  • 执行“ /bin/kill -HUP cat $nginx_pid ”这条命令的目的是因为切割日志以后 “mv $log $log-$d ” 会将日志移动位置,如果不使用这条命令重新加载一次nginx服务、重新生成一次日志文件,那么将会导致服务出错
  • 所以,为了保证“ /bin/kill -HUP cat $nginx_pid ”能准确的执行,需要确定nginx的PID所在
代码语言:javascript
复制
[root@hanfeng ~]# ls /usr/local/nginx/logs/
access.log  error.log  nginx_error.log  nginx.pid
  1. 循环语句理解
  • for f in ‘ls ‘ ; do ls -l $f; done
    • for 循环开始,f 表示文件,in 表示做什么,‘ls’in执行的东西; do 执行 ls -f $f;done 结束
  • 任务计划
    • 脚本写完以后,需要写一个计划,让脚本在规定的时间运行。
    • crontab -e
      • 0 0 * * * /bin/bash /usr/local/sbin/nginx_logrotate.sh
  • 长时间累积,会生成大量的日志需要进行清理
    • fidn /tmp/ -type f -name .log- -mtime +30 |xargs rm

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

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

  • 配置如下
代码语言:javascript
复制
location ~ .*\.(gif|jpg|jpeg|png|bmp|swf)$
    {
          expires      7d;
          access_log off;
    }
location ~ .*\.(js|css)$
    {
          expires      12h;
          access_log off;
    }

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

  • 在配置文件中添加
代码语言:javascript
复制
location ~ .*\.(gif|jpg|jpeg|png|bmp|swf)$    //匹配gif|jpg|jpeg|png|bmp|swf 后缀的文件
    {
          expires      7d;        //7天后过期
          access_log off;        //匹配“.*.(gif|jpg|jpeg|png|bmp|swf) ”关闭记录日志
    }
location ~ .*\.(js|css)$
    {
          expires      12h;        //12个小时后过期
          access_log off;        //匹配“.*.(js|css) ”关闭记录日志
    }
  1. 打开虚拟主机配置文件vim /usr/local/nginx/conf/vhost/test.com.conf
代码语言:javascript
复制
[root@hanfeng vhost]# vim /usr/local/nginx/conf/vhost/test.com.conf

server
{
    listen 80;
    server_name test.com test1.com test2.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 combined_realip;
}   
保存退出
  1. 检查配置文件语法错误,并重新加载配置文件
代码语言:javascript
复制
[root@hanfeng 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@hanfeng vhost]# /usr/local/nginx/sbin/nginx -s reload
[root@hanfeng vhost]# 
  1. 测试,先来模拟一个图片
代码语言:javascript
复制
[root@hanfeng vhost]# cd /data/wwwroot/test.com/
[root@hanfeng test.com]# ls
admin  index.html
[root@hanfeng test.com]# vim 1.gif        在1.gif随意写入一些内容
[root@hanfeng test.com]# vim 2.js
[root@hanfeng test.com]# 
  1. 接下来做一个访问测试
代码语言:javascript
复制
[root@hanfeng test.com]# curl -x127.0.0.1:80 test.com/1.gif
sdafasf
[root@hanfeng test.com]# curl -x127.0.0.1:80 test.com/2.js
fghdfsd
[root@hanfeng test.com]# curl -x127.0.0.1:80 test.com/index.html
“test.com”
[root@hanfeng test.com]# 
  1. 查看日志,会看到只有一条日志
代码语言:javascript
复制
[root@hanfeng test.com]# cat /tmp/test.com.log
127.0.0.1 - [05/Jan/2018:00:17:53 +0800] test.com "/index.html" 200 "-" "curl/7.29.0"
[root@hanfeng test.com]# 
  1. 测试过期时间,加上-I参数
代码语言:javascript
复制
[root@hanfeng 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, 04 Jan 2018 16:22:07 GMT
Content-Type: application/javascript
Content-Length: 8
Last-Modified: Thu, 04 Jan 2018 16:15:42 GMT
Connection: keep-alive
ETag: "5a4e532e-8"
Expires: Fri, 05 Jan 2018 04:22:07 GMT
Cache-Control: max-age=43200
Accept-Ranges: bytes

[root@hanfeng test.com]# 
  • max-age=43200 过期时间
  1. 如果去掉expires,则不会显示max-age过期时间
本文参与 腾讯云自媒体分享计划,分享自作者个人站点/博客。
如有侵权请联系 cloudcommunity@tencent.com 删除

本文分享自 作者个人站点/博客 前往查看

如有侵权,请联系 cloudcommunity@tencent.com 删除。

本文参与 腾讯云自媒体分享计划  ,欢迎热爱写作的你一起参与!

评论
登录后参与评论
0 条评论
热度
最新
推荐阅读
目录
  • 12.10 Nginx访问日志
    • Nginx访问日志目录概要
      • Nginx访问日志
      • 12.11 Nginx日志切割
        • Nginx日志切割目录概要
          • Nginx日志切割
            • shell脚本知识点
        • 12.12 静态文件不记录日志和过期时间
          • 静态文件不记录日志和过期时间目录概要
            • 静态文件不记录日志和过期时间
            相关产品与服务
            轻量应用服务器
            轻量应用服务器(TencentCloud Lighthouse)是新一代开箱即用、面向轻量应用场景的云服务器产品,助力中小企业和开发者便捷高效的在云端构建网站、Web应用、小程序/小游戏、游戏服、电商应用、云盘/图床和开发测试环境,相比普通云服务器更加简单易用且更贴近应用,以套餐形式整体售卖云资源并提供高带宽流量包,将热门开源软件打包实现一键构建应用,提供极简上云体验。
            领券
            问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档