十二周三次课

十二周三次课javascript

12.10Nginx访问日志php

12.11Nginx日志切割css

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

12.10Nginx访问日志java

•日志格式mysql

• vim /usr/local/nginx/conf/nginx.conf //搜索log_formatnginx

$remote_addrsql

客户端IP(公网IP)shell

$http_x_forwarded_forvim

代理服务器的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.打开主配置文件/usr/local/nginx/conf/nginx.conf

[root@tianqi-01 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,就会出来本身上网的IP地址

2.除了在主配置文件nginx.conf里定义日志格式外,还须要在虚拟主机配置文件去定义access_log /tmp/1.log combined_realip;来定义访问日志路径.

  • vim /usr/local/nginx/conf/vhost/test.com.conf

[root@tianqi-01 vhost]# vim test.com.conf

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

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;
}

保存退出

  • 若是不写日志格式,那就会走默认的日志格式

3.而后检查配置文件是否存在语法错误,并从新加载配置文件

[root@tianqi-01 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@tianqi-01 vhost]# /usr/local/nginx/sbin/nginx -s reload
[root@tianqi-01 vhost]# 

4.测试

[root@tianqi-01 vhost]# curl -x127.0.0.1:80 test4.com/admin.index.html/adafsbs -I
HTTP/1.1 404 Not Found
Server: nginx/1.12.1
Date: Tue, 13 Mar 2018 12:17:23 GMT
Content-Type: text/html
Content-Length: 169
Connection: keep-alive

[root@tianqi-01 vhost]# curl -x127.0.0.1:80 test3.com/admin.index.html/adafsbs -I
HTTP/1.1 301 Moved Permanently
Server: nginx/1.12.1
Date: Tue, 13 Mar 2018 12:17:53 GMT
Content-Type: text/html
Content-Length: 185
Connection: keep-alive
Location: http://test.com/admin.index.html/adafsbs

[root@tianqi-01 vhost]# curl -x127.0.0.1:80 test2.com/admin.index.html/adafsbs -I
HTTP/1.1 301 Moved Permanently
Server: nginx/1.12.1
Date: Tue, 13 Mar 2018 12:18:07 GMT
Content-Type: text/html
Content-Length: 185
Connection: keep-alive
Location: http://test.com/admin.index.html/adafsbs

5.查看日志/tmp/test.com.log

[root@tianqi-01 vhost]# cat /tmp/test.com.log
127.0.0.1 - [13/Mar/2018:20:17:53 +0800] test3.com "/admin.index.html/adafsbs" 301 "-" "curl/7.29.0"
127.0.0.1 - [13/Mar/2018:20:18:07 +0800] test2.com "/admin.index.html/adafsbs" 301 "-" "curl/7.29.0"
[root@tianqi-01 vhost]# 

12.11Nginx日志切割

Nginx日志切割目录概要

• 自定义shell 脚本

• vim /usr/local/sbin/nginx_log_rotate.sh//写入以下内容

#! /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/目录下

[root@tianqi-01 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 reload 是同样的
  • 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”

[root@tianqi-01 vhost]# ls
aaa.com.conf  test.com.conf
[root@tianqi-01 vhost]# for f in `ls`; do ls -l $f ; done
-rw-r--r-- 1 root root 142 Mar 12 19:38 aaa.com.conf
-rw-r--r-- 1 root root 290 Mar 13 20:15 test.com.conf
[root@tianqi-01 vhost]# 

3.执行shell脚本,并加-x

  • -x选项,是为了查看脚本执行的过程

[root@tianqi-01 vhost]# sh -x /usr/local/sbin/nginx_log_rotate.sh
++ date -d '-1 day' +%Y%m%d
+ d=20180312
+ 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-20180312
+ for log in '`ls *.log`'
+ mv test.com.log test.com.log-20180312
++ cat /usr/local/nginx/logs/nginx.pid
+ /bin/kill -HUP 803
[root@tianqi-01 vhost]# 

4.查看日志切割文件,天天都生成一个日志,在天天切割后,过段时间还要按期清理

[root@tianqi-01 vhost]# ls /tmp/
mysql.sock
pear
php_errors.log-20180312
php-fcgi.sock
systemd-private-0e2b4ec78d5546abb6c87fc75918c612-vgauthd.service-Tmhc6I
systemd-private-0e2b4ec78d5546abb6c87fc75918c612-vmtoolsd.service-bKnQdM
systemd-private-10939be3906d4885a694c9ccaa969513-vgauthd.service-up8a1e
systemd-private-10939be3906d4885a694c9ccaa969513-vmtoolsd.service-qrMrpr
systemd-private-1e99790229834f839348180fcbb3aa93-vgauthd.service-v7jGAu
systemd-private-1e99790229834f839348180fcbb3aa93-vmtoolsd.service-pRTqgc
systemd-private-2d7e7760b6cd416895880c21a3911a53-vgauthd.service-S1F6Rz
systemd-private-2d7e7760b6cd416895880c21a3911a53-vmtoolsd.service-muJvMP
systemd-private-491ebf486ca940d885ed9348c17675f5-vgauthd.service-3sBthy
systemd-private-491ebf486ca940d885ed9348c17675f5-vmtoolsd.service-sJ80kS
systemd-private-74b18b5fecc548f1ac450ce1fea7233b-vgauthd.service-DBvBHm
systemd-private-74b18b5fecc548f1ac450ce1fea7233b-vmtoolsd.service-AjXoy6
systemd-private-7ad9977beb634b3280df7fd64e4b16d6-vgauthd.service-qkAtId
systemd-private-7ad9977beb634b3280df7fd64e4b16d6-vmtoolsd.service-z35BeI
systemd-private-7d038e320b614122b872e50458348353-vgauthd.service-Ka4suT
systemd-private-7d038e320b614122b872e50458348353-vmtoolsd.service-radGF1
systemd-private-889a29715b47432482f67cbf18e2c4ac-vgauthd.service-hGaIS9
systemd-private-889a29715b47432482f67cbf18e2c4ac-vmtoolsd.service-gDfN0p
systemd-private-aa279ed589d542bcbc4f2903687ce469-vgauthd.service-xDYbZY
systemd-private-aa279ed589d542bcbc4f2903687ce469-vmtoolsd.service-tTo26l
systemd-private-f4d35a41f34c4234a6934c03885429ba-vgauthd.service-owt3oB
systemd-private-f4d35a41f34c4234a6934c03885429ba-vmtoolsd.service-eJFX1G
test.com.log
test.com.log-20180312
[root@tianqi-01 vhost]# 

5.删除30天之前的日志文件

[root@tianqi-01 vhost]# find /tmp/ -name *.log-* -type f -mtime +30 |xargs rm
rm: missing operand
Try 'rm --help' for more information.
[root@tianqi-01 vhost]# find /tmp/ -name *.log-* -type f -mtime +30
[root@tianqi-01 vhost]# 

//不会删除任何的文件,由于根本没与符合要求的文件

[root@tianqi-01 vhost]# find /tmp/ -name *.log-* -type f
/tmp/php_errors.log-20180312
/tmp/test.com.log-20180312
[root@tianqi-01 vhost]# ls /usr/local/sbin/
iptables.sh  nginx_log_rotate.sh
[root@tianqi-01 vhost]# cat /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`
[root@tianqi-01 vhost]# 

6.完脚本后,还要加一个任务计划crontab -e——>这里由于是测试,脚本就不加入到任务计划中了

[root@tianqi-01 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 把当前操做暂停丢到后台

[root@tianqi-01 vhost]# date -d "-1 day" +%Y%m%d
20180312
[root@tianqi-01 vhost]# date
Tue Mar 13 21:16:08 CST 2018
[root@tianqi-01 vhost]# 

  • 就是时间,并且是昨天的时间,由于目前作的日志切割都是以天为单位,并且,日志须要过了当天23点59分59秒之后到次日的0点0分01秒才切割

2.指定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所在

[root@tianqi-01 vhost]# ls /usr/local/nginx/logs/
access.log  error.log  nginx_error.log  nginx.pid
[root@tianqi-01 vhost]# 

3.循环语句理解

  • 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静态文件不记录日志和过时时间

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

• 配置以下

location ~ .*\.(gif|jpg|jpeg|png|bmp|swf)$

    {

          expires      7d;

          access_log off;

    }

location ~ .*\.(js|css)$

    {

          expires      12h;

          access_log off;

    }

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

在文件中添加

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.打开虚拟主机配置文件

[root@tianqi-01 vhost]# 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 combined_realip;
}

保存退出

2.检查配置文件语法错误,并从新加载配置文件

[root@tianqi-01 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@tianqi-01 vhost]# /usr/local/nginx/sbin/nginx -s reload
[root@tianqi-01 vhost]# 

3.测试,先来模拟一个图片

[root@tianqi-01 vhost]# cd /data/wwwroot/test.com/
[root@tianqi-01 test.com]# ls

admin  index.html
[root@tianqi-01 test.com]# vim 1.gif    //在里面随便写点东西

asdbasdgb

保存退出

[root@tianqi-01 test.com]# vim 2.js    //在里面随便写点东西

sasgashv

保存退出

4.接下来作访问测试

[root@tianqi-01 test.com]# curl -x127.0.0.1:80 test.com/1.gif
asdbasdgb
[root@tianqi-01 test.com]# curl -x127.0.0.1:80 test.com/2.js
sasgashv

[root@tianqi-01 test.com]# curl -x127.0.0.1:80 test.com/index.html
test.com

4.查看日志,会看到只有一条日志

[root@tianqi-01 test.com]# cat /tmp/test.com.log
127.0.0.1 - [13/Mar/2018:21:33:52 +0800] test.com "/index.html" 200 "-" "curl/7.29.0"

[root@tianqi-01 test.com]# !cat
cat /tmp/test.com.log

127.0.0.1 - [13/Mar/2018:21:33:52 +0800] test.com "/index.html" 200 "-" "curl/7.29.0"
127.0.0.1 - [13/Mar/2018:21:35:19 +0800] test.com "/index.html" 200 "-" "curl/7.29.0"
[root@tianqi-01 test.com]# curl -x127.0.0.1:80 test.com/2.js
sasgashv
[root@tianqi-01 test.com]# !cat
cat /tmp/test.com.log

127.0.0.1 - [13/Mar/2018:21:33:52 +0800] test.com "/index.html" 200 "-" "curl/7.29.0"
127.0.0.1 - [13/Mar/2018:21:35:19 +0800] test.com "/index.html" 200 "-" "curl/7.29.0"
[root@tianqi-01 test.com]# 

//这说明访问js的时候不会记录日志

[root@tianqi-01 test.com]# curl -x127.0.0.1:80 test.com/2.jsagasg
<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@tianqi-01 test.com]# !cat
cat /tmp/test.com.log
127.0.0.1 - [13/Mar/2018:21:33:52 +0800] test.com "/index.html" 200 "-" "curl/7.29.0"
127.0.0.1 - [13/Mar/2018:21:35:19 +0800] test.com "/index.html" 200 "-" "curl/7.29.0"
127.0.0.1 - [13/Mar/2018:21:36:51 +0800] test.com "/2.jsagasg" 404 "-" "curl/7.29.0"
[root@tianqi-01 test.com]# 

5.测试过时时间,加上-I参数

[root@tianqi-01 test.com]# curl -x127.0.0.1:80 -I test.com/2.js
HTTP/1.1 200 OK
Server: nginx/1.12.1
Date: Tue, 13 Mar 2018 13:38:09 GMT
Content-Type: application/javascript
Content-Length: 9
Last-Modified: Tue, 13 Mar 2018 13:31:38 GMT
Connection: keep-alive
ETag: "5aa7d2ba-9"
Expires: Wed, 14 Mar 2018 01:38:09 GMT
Cache-Control: max-age=43200
Accept-Ranges: bytes

[root@tianqi-01 test.com]# 

  • max-age=43200 过时时间

6.若是去掉expires,则不会显示max-age过时时间

[root@tianqi-01 test.com]# vim /usr/local/nginx/conf/vhost/test.com.conf 

#          expires      12h;

//注释掉上面这一句话

7.查看配置文件是否存在语法错误,并从新加载配置文件

[root@tianqi-01 test.com]# /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@tianqi-01 test.com]# /usr/local/nginx/sbin/nginx -s reload
[root@tianqi-01 test.com]# 

8.从新进行curl访问,则max-age=43200不会出现

[root@tianqi-01 test.com]# curl -x127.0.0.1:80 -I test.com/2.js
HTTP/1.1 200 OK
Server: nginx/1.12.1
Date: Tue, 13 Mar 2018 13:41:49 GMT
Content-Type: application/javascript
Content-Length: 9
Last-Modified: Tue, 13 Mar 2018 13:31:38 GMT
Connection: keep-alive
ETag: "5aa7d2ba-9"
Accept-Ranges: bytes

[root@tianqi-01 test.com]# 

友情连接:阿铭Linux

相关文章
相关标签/搜索