Nginx 访问日志

配置访问日志:

[root@localhost ~]$ cat /usr/local/nginx/conf/nginx.conf

http {    

    log_format  main  '$remote_addr - $remote_user [$time_local] "$request" '    # 定义日志格式,main是日志格式的名称,以便后面调用
                      '$status $body_bytes_sent "$http_referer" '                
                      '"$http_user_agent" $http_x_forwarded_for';

    access_log  /data/nginx_logs/access.log  main;    # 指定访问日志的存放路径及使用哪个日志格式来记录日志
}

可以指定静态文件被访问时不记录日志:

[root@localhost ~]$ cat /usr/local/nginx/conf/vhost/test.com.conf 
server {
    listen 80;
    server_name www.test.com;
    index index.html index.html index.php;
    root /data/www;
  
    location ~ .*.(gif|jpg|jpeg|png|bmp|swf|js|css)$ {
        access_log off;
    }
}
变量 含义
$remote_addr 客户端IP
$remote_user 客户端用户
$time_local 服务器本地时间,也就是客户端访问时间
$request 客户端请求的URL
$status 请求的状态码
$body_bytes_sent 发送给客户端的字节数
$http_referer 从哪个网址链接过来
$http_user_agent 使用哪个浏览器访问
$http_x_forwarded_for 代理服务器IP

     

原文地址:https://www.cnblogs.com/pzk7788/p/10333032.html