nginx日志格式配置

Lear 2024-08-06 14:15:00
Categories: Tags:

nginx日志格式配置

我一向对日志这个东西有些许恐惧,因为在分析日志是需要记住不同服务器日志的格式,就拿提取ip这一项来说,有的服务器日志是在第一列,有的是第二列或则第三列等等。知道今天我才发现,日志格式是可以自定义配置的。。。。
现在我们来看一下nginx的日志格式如何自定义配置
log_format指令用来设置日志格式,其语法如下:
log_format name format [format …]

nginx默认日志格式具体参数如下:

log_format combined ‘$remote_addr - $remote_user [$time_local]’

‘“$request” $status $body_bytes_sent ‘

‘“$http_referer” “$http_user_agent”‘;

看一下一条nginx日志

192.168.3.113 - - [23/Jul/2017:01:17:43 -0700] “GET / HTTP/1.1” 304 0 “-“ “Mozilla/5.0 (X11; Linux x86_64; rv:45.0) Gecko/20100101 Firefox/45.0”

可以看到分别对应的是ip 远程用户名 时间 请求 请求状态 以及用户客户端信息等等

那么日志配置是在哪里进行配置的呢

[root@localhost nginx]# !446

cat conf/nginx.conf|grep -v “#“|grep -v “^$“

worker_processes 1;

events {

worker_connections 1024;

}

http {

include mime.types;

default_type application/octet-stream;

log_format mylog ‘$remote_addr [$time_local]’ ‘“$http_user_agent”‘; #在这里进行配置,命名为mylog,也可以把这条语句放在server里面

sendfile on;

keepalive_timeout 65;

server {

listen 80;

server_name www.bp1.com;

location / {

root html;

index index.html index.htm index.php;

}

error_page 404 /404.html;

error_page 500 502 503 504 /50x.html;

location = /50x.html {

root html;

}

location ~ \.php$ {

root html;

fastcgi_pass 127.0.0.1:9000;

fastcgi_index index.php;

fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;

include fastcgi_params;

}

}

server{

listen 192.168.3.123:80;

server_name www.bp3.com;

access_log logs/bp2.access.log mylog; #使用刚配置的日志文件格式mylog

location /

{

index index.html index.php;

root html/bp2;

}

location ~ \.php$ {

root html/bp2;

fastcgi_pass 127.0.0.1:9000;

fastcgi_index index.php;

fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;

include fastcgi_params;

}

}

server{

listen 192.168.3.125:80;

server_name www.bp2.com;

location /{

root html/bp3;

index index.html index.php;

}

location ~ \.php$ {

root html/bp3;

fastcgi_pass 127.0.0.1:9000;

fastcgi_index index.php;

fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;

include fastcgi_params;

}

}

}

[root@localhost nginx]#

效果

192.168.3.113 [23/Jul/2017:01:37:51 -0700]”Mozilla/5.0 (X11; Linux x86_64; rv:45.0) Gecko/20100101 Firefox/45.0”