十二周三次课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
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"';
$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.除了在主配置文件nginx.conf里定义日志格式外,还须要在虚拟主机配置文件去定义access_log /tmp/1.log combined_realip;来定义访问日志路径.
[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
1.这里写一个日志切割脚本
2.首先建立一个shell脚本vim /usr/local/sbin/nginx_log_rotate.sh
[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`
[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
[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
//直接退出不保存
1.日志切割的定义
date -d “-1 day” +%Y%m%d
”不明白意思[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]#
2.指定PID路径的意义
cat $nginx_pid
”这个命令也将没有办法继续执行cat $nginx_pid
” z这条命令的意思就是从新加载一次nginx服务cat $nginx_pid
”这条命令的目的是由于切割日志之后 “mv $log $log-$d ” 会将日志移动位置,若是不使用这条命令从新加载一次nginx服务、从新生成一第二天志文件,那么将会致使服务出错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.循环语句理解
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]#
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