2018-04-25 Linux学习

12.10 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

操作过程

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

//格式中 combined_realip 是日志格式代表
log_format combined_realip '$remote_addr $http_x_forwarded_for [$time_local]'
' $host "$request_uri" $status'
' "$http_referer" "$http_user_agent"';

[root@linux01 ~]# 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;
}
access_log /tmp/test.com.log combined_realip;
}

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

[root@linux01 ~]# curl -x127.0.0.1:80 test2.com/admin/index.html -I
HTTP/1.1 301 Moved Permanently
Server: nginx/1.14.0
Date: Thu, 19 Apr 2018 14:28:19 GMT
Content-Type: text/html
Content-Length: 185
Connection: keep-alive
Location: http://test.com/admin/index.html

[root@linux01 ~]# curl -x127.0.0.1:80 test3.com/admin/index.html -I
HTTP/1.1 301 Moved Permanently
Server: nginx/1.14.0
Date: Thu, 19 Apr 2018 14:28:25 GMT
Content-Type: text/html
Content-Length: 185
Connection: keep-alive
Location: http://test.com/admin/index.html

[root@linux01 ~]# cat /tmp/test.com.log 
127.0.0.1 - [19/Apr/2018:22:28:19 +0800] test2.com "/admin/index.html" 301 "-" "curl/7.29.0"
127.0.0.1 - [19/Apr/2018:22:28:25 +0800] test3.com "/admin/index.html" 301 "-" "curl/7.29.0"

12.11 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

操作过程

[root@linux01 ~]# 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
/bin/kill -HUP cat $nginx_pid

[root@linux01 ~]# sh -x /usr/local/sbin/nginx_logrotate.sh

[root@linux01 ~]# crontab -e
0 0 * * * /bin/bash /usr/local/sbin/nginx_logrotate.sh

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

配置如下,添加到虚拟主机配置文件里
location ~ ..(gif|jpg|jpeg|png|bmp|swf)$
{
expires 7d;
access_log off;
}
location ~ .
.(js|css)$
{
expires 12h;
access_log off;
}

操作过程

[root@linux-01 ~]# 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 aming;

}

[root@linux01 ~]# cd /data/wwwroot/test.com/
[root@linux01 test.com]# touch 1.gif
[root@linux01 test.com]# touch 2.js

未重启Nginx测试

[root@linux01 test.com]# curl -x127.0.0.1:80 test.com/1.gif
[root@linux01 test.com]# curl -x127.0.0.1:80 test.com/2.js
[root@linux01 test.com]# curl -x127.0.0.1:80 test.com/index.html
test.com
test.com

[root@linux01 test.com]# cat /tmp/test.com.log
127.0.0.1 - [20/Apr/2018:17:55:13 +0800] test.com "/1.gif" 200 "-" "curl/7.29.0"
127.0.0.1 - [20/Apr/2018:17:55:20 +0800] test.com "/2.js" 200 "-" "curl/7.29.0"
127.0.0.1 - [20/Apr/2018:17:55:39 +0800] test.com "/index.html" 200 "-" "curl/7.29.0"

重启Nginx测试

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

[root@linux01 ~]# curl -x127.0.0.1:80 test.com/1.gif
[root@linux01 ~]# curl -x127.0.0.1:80 test.com/2.js
[root@linux01 ~]# curl -x127.0.0.1:80 test.com/index.html
test.com
test.com

[root@linux01 ~]# cat /tmp/test.com.log
127.0.0.1 - [20/Apr/2018:17:55:13 +0800] test.com "/1.gif" 200 "-" "curl/7.29.0"
127.0.0.1 - [20/Apr/2018:17:55:20 +0800] test.com "/2.js" 200 "-" "curl/7.29.0"
127.0.0.1 - [20/Apr/2018:17:55:39 +0800] test.com "/index.html" 200 "-" "curl/7.29.0"
127.0.0.1 - [20/Apr/2018:17:57:27 +0800] test.com "/index.html" 200 "-" "curl/7.29.0"

时间过期功能设置成功 max-age=43200

[root@linux01 ~]# curl -x127.0.0.1:80 test.com/2.js -I
HTTP/1.1 200 OK
Server: nginx/1.14.0
Date: Fri, 20 Apr 2018 09:58:16 GMT
Content-Type: application/javascript
Content-Length: 0
Last-Modified: Fri, 20 Apr 2018 09:54:41 GMT
Connection: keep-alive
ETag: "5ad9b8e1-0"
Expires: Fri, 20 Apr 2018 21:58:16 GMT
Cache-Control: max-age=43200
Accept-Ranges: bytes

[root@linux01 ~]# cat /tmp/test.com.log
127.0.0.1 - [20/Apr/2018:17:55:13 +0800] test.com "/1.gif" 200 "-" "curl/7.29.0"
127.0.0.1 - [20/Apr/2018:17:55:20 +0800] test.com "/2.js" 200 "-" "curl/7.29.0"
127.0.0.1 - [20/Apr/2018:17:55:39 +0800] test.com "/index.html" 200 "-" "curl/7.29.0"
127.0.0.1 - [20/Apr/2018:17:57:27 +0800] test.com "/index.html" 200 "-" "curl/7.29.0"

猜你喜欢

转载自blog.51cto.com/9298822/2107867