nginx变量&&自定义日志收集

发布于:2024-05-05 ⋅ 阅读:(28) ⋅ 点赞:(0)

内置变量

$remote_addr;存放了客户端的地址,注意是客户端的公网IP,也就是一家人访问一个网站,则会显示为路由器的公网IP。

$args;变量中存放了URL中的指令

[root@localhost conf.d]# cat pc.conf 
server {
  listen 80;
  server_name www.fxq.com;
  location / {
     root /data/nginx/html/pc;
     index index.html;
  }
  location /m78 {
    default_type text/html;
    echo $remote_addr;
    echo $args;
 }  
}

 

我这边电脑与服务器之间的网段做了路由,显示了网关

$document_root;    保存了针对当前资源的请求的系统根目录

$document_uri;      保存了当前请求中不包含指令的URI

[root@localhost conf.d]# cat pc.conf 
server {
  listen 80;
  server_name www.fxq.com;
  location / {
     root /data/nginx/html/pc;
     index index.html;
  }
  location /m78 {
    root /data/nginx/html/pc;
    default_type text/html;
    echo $document_root; 
    echo $document_uri;
 }  
}

 $host; 存放了请求的host名称

 $http_user_agent; 客户端浏览器的详细信息

 $http_cookie;客户端的cookie信息。

[root@localhost conf.d]# cat pc.conf 
server {
  listen 80;
  server_name www.fxq.com;
  location / {
     root /data/nginx/html/pc;
     index index.html;
  }
  location /m78 {
    root /data/nginx/html/pc;
    default_type text/html;
    echo  $host;
    echo  $http_user_agent;
    echo  $http_cookie;
 }  

}

 limit_rate 10240;
echo $limit_rate;如果nginx服务器使用limit_rate配置了显示网络速率,则会显示,如果没有设置, 则显示0

 $remote_user; 已经过Auth Basic Module验证的用户名。

 $request_body_file;做反向代理时发给后端服务器的本地资源的名称。

 $request_method;请求资源的方式,GET/PUT/DELETE等

 $request_filename;当前请求的资源文件的路径名称,由root或alias指令与URI请求生成的文件绝对路径

 $request_uri;包含请求参数的原始URI,不包含主机名

 $scheme;请求的协议,如ftp,https,http等;

 $server_protocol;保存了客户端请求资源使用的协议的版本,如HTTP/1.0,HTTP/1.1,HTTP/2.0等

 $server_addr;保存了服务器的IP地址;

 $server_name;请求的服务器的主机名。

 $server_port;请求的服务器的端口号。

自定义变量

使用set自定义变量

[root@localhost conf.d]# cat pc.conf 
server {
  listen 80;
  server_name www.fxq.com;
  location / {
     root /data/nginx/html/pc;
     index index.html;
  }
  location /m78 {
    root /data/nginx/html/pc;
    default_type text/html;
    set $name fxq;
    echo $name;
}  

}

 

日志格式     

默认格式   

                                              

自定义json格式

    log_format access_json '{"@timestamp":"$time_iso8601",'
        '"host":"$server_addr",'
        '"clientip":"$remote_addr",'
        '"size":$body_bytes_sent,'
        '"responsetime":$request_time,'
        '"upstreamtime":"$upstream_response_time",'
        '"upstreamhost":"$upstream_addr",'
        '"http_host":"$host",'
        '"uri":"$uri",'
        '"domain":"$host",'
        '"xff":"$http_x_forwarded_for",'
        '"referer":"$http_referer",'
        '"tcp_xff":"$proxy_protocol_addr",'
        '"http_user_agent":"$http_user_agent",'
        '"status":"$status"}';
    access_log  logs/access.log  access_json;

 python日志收集

#!/usr/bin/env python
#coding: utf-8 
status_200=[]
status_404=[]
with open("/apps/nginx/logs/access.log") as f:
    for line in f.readlines():
        line = eval(line)
        if line.get("status") == "200":
            status_200.append(line.get)
        elif line.get("status") == "404":
            status_404.append(line.get)
        else:
            print("状态码 ERROR")
f.close()
print "状态码200的有--:",len(status_200)
print "状态码404的有--:",len(status_404)