nginx常用的日志配置

我租了一个服务器搭建了nginx,发现一个报错,后面我解决了,既然这样话,我就顺便把方法教给你们。

log_format compression '$remote_addr - $remote_user [$time_local] '
 '"$request" $status $bytes_sent '
 '"$http_referer" "$http_user_agent" "$gzip_ratio"';
access_log /spool/logs/nginx-access.log compression buffer=32k;

注意:
compression为log_format后面的日志样式名称。
这个日志对应的路径对启动nginx的用户需要有写权限
nginx转发json

log_format json '{"remote_addr":"$remote_addr","remote_user":"$remote_user",'
 '"request":"$request","status":"$status","remote_user":"$bytes_sent",'
 '"http_referer":"$http_referer","http_user_agent":"$http_user_agent"}';
access_log /spool/logs/nginx-access.log json buffer=32k;

注意:
json 为log_format后面的日志样式名称。
这个日志对应的路径对启动nginx的用户需要有写权限
好用的变量
变量名称 说明示例

$arg_name 请求中的name参数
$args 请求中的参数
$body_bytes_sent 已发送的消息体字节数
$content_length HTTP请求信息里的"Content-Length"
$content_type 请求信息里的"Content-Type"
$host 请求信息中的"Host",如果请求中没有Host行,则等于设置的服务器名
$http_cookie cookie 信息
$http_referer 引用地址
$http_user_agent 客户端代理信息
$http_via 最后一个访问服务器的Ip地址。
$http_x_forwarded_for 相当于网络访问路径
$is_args 如果请求行带有参数,返回“?”,否则返回空字符串
$pid worker进程的PID
$remote_addr 客户端IP地址
$remote_port 客户端端口号
$remote_user 客户端用户名,认证用

request相关常用的:
变量名称 说明示例

$request 用户请求
$request_body 这个变量包含请求的主要信息。在使用proxy_pass或fastcgi_pass指令的location中比较有意义
$request_filename 当前请求的文件路径名,比如/opt/nginx/www/test.php
$request_method 请求的方法,比如"GET"、"POST"等
$request_uri 请求的URI,带参数

server相关常用的:
变量名称 说明示例

$scheme 所用的协议,比如http或者是https,常用这个
$server_addr 服务器地址,如果没有用listen指明服务器地址,使用这个变量将发起一次系统调用以取得地址(造成资源浪费)
$server_name 请求到达的服务器名
$server_port 请求到达的服务器端口号
$server_protocol 请求的协议版本,“HTTP/1.0"或"HTTP/1.1”
$uri 请求的URI,可能和最初的值有不同,比如经过重定向之类的

如果需要服务器的话,可以后台联系我

作者:从容的黑框眼镜原文地址:https://segmentfault.com/a/1190000042455552

%s 个评论

要回复文章请先登录注册