JAVA和Nginx 教程大全

网站首页 > 精选教程 正文

nginx入门——nginx访问日志(六)

wys521 2024-09-09 12:09:35 精选教程 85 ℃ 0 评论

日志功能在ngx_http_log_module模块中定义,实现了以指定格式写入请求日志。

我们先来看一个nginx配置文件:

http {
        include mime.types;
        default_type application/octet-stream;
  
        log_format custom '$remote_addr - $remote_user [$time_local] "$request" '
        '$status $body_bytes_sent "$http_referer" '
        '"$http_user_agent" "$http_x_forwarded_for" $http_host $name $id';
        map $http_host $name {
                hostnames;
                default 0;
                www.example.com 1;
        }
        sendfile on;
        keepalive_timeout 65;
        server {
                listen 80;
                server_name localhost;
                if ($http_cookie ~* "id=(\S+) {
                        set $id $1;
        }
                access_log log/www.example.com_access.log custom;
                location / {
                root html;
                try_files $uri /index.html;
                }
        }
}

nginx 日志相关指令

access_log指令第一个参数定义了日志文件名和写入位置,第二个参数是日志格式名称。适用于http, server, location, if in location, limit_except上下文

access_log log/www.example.com_access.log custom;

log_format指令定义日志输出格式。适用于http上下文。

log_format custom '$remote_addr - $remote_user [$time_local] "$request" '
                                '$status $body_bytes_sent "$http_referer" '
                                '"$http_user_agent" "$http_x_forwarded_for" $http_host $name $id';

日志格式中预定义变量


$name $id这两个变量是在配置文件里面自定义的。自定义变量的方法大家可以参考上一篇《nginx入门——预定义变量和自定义变量(五)》。

Tags:

本文暂时没有评论,来添加一个吧(●'◡'●)

欢迎 发表评论:

最近发表
标签列表