log_format

log_format mylogformat '$http_x_forwarded_for - $remote_user [$time_local] ' '"$request" $status $body_btyes_send' '"$http_referer" "$http_user_agent"';

$remote_addr和$http_x_forwarded_for  用于记录IP
$remote_user 记录远程客户端用户名称
$time_local 记录访问时间和时区
$request 记录url和http协议
$status 记录请求状态 200 400 404 
$body_bytes_send 用于记录发送给客户单的主体文件内容大小
$http_referer 记录前面链接
$http_user_agnet 记录客户端浏览器信息

combined

combined 是一种默认的log_format

access_log

access_log path [format [buffer=size | off]]

1.不记录日志
    access_log off;

2.使用默认combined格式
    access_log /var/www/note/logs/filename.log;
    或
    access_log /var/www/note/logs/filename.log combined;

3.使用自定义格式
    log_format mylogformat '$http_x_forwarded_for - $remote_user [$time_local] ' '"$request" $status $body_btyes_send' '"$http_referer" "$http_user_agent"';

    access_log /var/www/note/logs/filename.log mylogformat buffer=32k;

日志分割

nginx不支持像Apache一样使用cronlog来轮转日志,但是可以采用以下方式来切割日志
mv /var/www/note/logs/access.log /var/www/note/logs/20170517.log

kill -USR1 $nginxpid


#!/bin/bash  
## 零点执行该脚本  

## Nginx 日志文件所在的目录  
LOGS_PATH=/usr/local/nginx/logs  

## 获取昨天的 yyyy-MM-dd  
YESTERDAY=$(date -d "yesterday" +%Y-%m-%d)  

## 移动文件  
mv ${LOGS_PATH}/access.log ${LOGS_PATH}/access_${YESTERDAY}.log  

## 向 Nginx 主进程发送 USR1 信号。USR1 信号是重新打开日志文件  
kill -USR1 $(cat /usr/local/nginx/nginx.pid)  

results matching ""

    No results matching ""