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_lengthHTTP请求信息里的"Content-Length"
$content_type请求信息里的"Content-Type"
$host请求信息中的"Host",如果请求中没有Host行,则等于设置的服务器名
$http_cookiecookie 信息
$http_referer引用地址
$http_user_agent客户端代理信息
$http_via最后一个访问服务器的Ip地址。
$http_x_forwarded_for相当于网络访问路径
$is_args如果请求行带有参数,返回“?”,否则返回空字符串
$pidworker进程的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,可能和最初的值有不同,比如经过重定向之类的
Logo

为开发者提供学习成长、分享交流、生态实践、资源工具等服务,帮助开发者快速成长。

更多推荐