学习linux第五十天

nginx访问日志

[root@hanlin ~]# vim /usr/local/nginx/conf/nginx.conf
 

user nobody nobody;
worker_processes 2;
error_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 xy '$remote_addr $http_x_forwarded_for [$time_local]' (主配置文件中定义的日志格式combined_realip是日志格式的名字,把它改成xy)

' $host "$request_uri" $status'
' "$http_referer" "$http_user_agent"';

 

以下是日志格式

remote_addr 客户端ip(公网ip)

http_x_forward_for  代理服务器的ip

time_local       服务器本地时间

host                访问主机名,域名

request_uri    访问的url地址

status             状态码

http_referer   referer

http_user_agent    user_agent


[root@hanlin ~]# 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;
}
access_log /tmp/test.conf.log xy; (虚拟主机配置文件里定义日志格式)

}
[root@hanlin ~]# curl -x127.0.0.1:80 test.com/
wo shi zhongguoren
[root@hanlin ~]# curl -x127.0.0.1:80 test.com/index.html
wo shi zhongguoren
[root@hanlin ~]# curl -x127.0.0.1:80 test1.com/index.html
<html>
<head><title>301 Moved Permanently</title></head>
<body bgcolor="white">
<center><h1>301 Moved Permanently</h1></center>
<hr><center>nginx/1.12.1</center>
</body>
</html>
[root@hanlin ~]# curl -x127.0.0.1:80 test2.com/index.html
<html>
<head><title>301 Moved Permanently</title></head>
<body bgcolor="white">
<center><h1>301 Moved Permanently</h1></center>
<hr><center>nginx/1.12.1</center>
</body>
</html>
[root@hanlin ~]# cat /tmp/test.conf.log  (查看日志)
127.0.0.1 - [27/Nov/2018:00:59:21 +0800] test.com "/" 200 "-" "curl/7.29.0"
127.0.0.1 - [27/Nov/2018:00:59:37 +0800] test.com "/index.html" 200 "-" "curl/7.29.0"
127.0.0.1 - [27/Nov/2018:00:59:44 +0800] test1.com "/index.html" 301 "-" "curl/7.29.0"
127.0.0.1 - [27/Nov/2018:00:59:50 +0800] test2.com "/index.html" 301 "-" "curl/7.29.0"
 

nginx日志切割

[root@hanlin ~]# 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`
do
mv $log $log-$d
done (for循环,遍查所有.log的日志,然后改成带有日期的名字)
/bin/kill -HUP `cat $nginx_pid` (生成新的test.conf.log,nginx.pid控制生成日志)
 
[root@hanlin ~]# sh -x /usr/local/sbin/nginx_logrotate.sh  (-x显示脚本执行的全部过程)
++ date -d '-1 day' +%Y%m%d
+ d=20181126
+ logdir=/tmp/
+ nginx_pid=/usr/local/nginx/logs/nginx.pid
+ cd /tmp/
++ ls test.conf.log
+ for log in '`ls *.log`'
+ mv test.conf.log test.conf.log-20181126
++ cat /usr/local/nginx/logs/nginx.pid
+ /bin/kill -HUP 1202
[root@hanlin ~]# ls /tmp (生成了两个log,一个昨天的,一个新的)
hanlin
mysql.sock
pear
php-fcgi.sock
systemd-private-1bcc066210384f3b951a318450201d62-chronyd.service-kwpjvY
systemd-private-1bcc066210384f3b951a318450201d62-cups.service-oomBTk
systemd-private-1bcc066210384f3b951a318450201d62-vgauthd.service-KyfzmG
systemd-private-1bcc066210384f3b951a318450201d62-vmtoolsd.service-nTbNpk
test.conf.log
test.conf.log-20181126

[root@hanlin ~]# crontab -e (再做一个任务计划,每天0:0执行一下脚本)
 

0 0 * * * /bin/bash /usr/local/nginx_logrorate.sh
 

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

[root@hanlin ~]# 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; (图片过期时间7天)
access_log off; (不记录日志)
}
location ~ .*\.(js|css)$
{
expires 12h;
access_log off;
}



access_log /tmp/test.conf.log xy;


测试
[root@hanlin ~]# cd /data/wwwroot/test.com/
[root@hanlin test.com]# vim 1.gif
[root@hanlin test.com]# vim 2.css
[root@hanlin 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@hanlin test.com]# /usr/local/nginx/sbin/nginx -s reload
[root@hanlin test.com]# curl -x127.0.0.1:80 test.com/1.gif
dgd
[root@hanlin test.com]# curl -x127.0.0.1:80 test.com/2.css
sg
[root@hanlin test.com]# curl -x127.0.0.1:80 test.com/index.html
wo shi zhongguoren

[root@hanlin test.com]# cat /tmp/test.conf.log
127.0.0.1 - [27/Nov/2018:01:44:18 +0800] test.com "/index.html" 200 "-" "curl/7.29.0" (测试,没有记录图片和css的访问记录)

 

[root@hanlin test.com]# curl -x127.0.0.1:80 -I test.com/2.css (如果配置文件里没有expires老化时间的配置,-I下面就不会出来max-age)
HTTP/1.1 200 OK
Server: nginx/1.12.1
Date: Mon, 26 Nov 2018 17:48:30 GMT
Content-Type: text/css
Content-Length: 3
Last-Modified: Mon, 26 Nov 2018 17:43:30 GMT
Connection: keep-alive
ETag: "5bfc30c2-3"
Expires: Tue, 27 Nov 2018 05:48:30 GMT
Cache-Control: max-age=43200
Accept-Ranges: bytes

转载于:https://my.oschina.net/u/3867255/blog/2961782

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值