博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
自定义nginx访问日志和内置变量使用
阅读量:6937 次
发布时间:2019-06-27

本文共 5949 字,大约阅读时间需要 19 分钟。

自定义nginx访问日志和内置变量使用

安装第三方echo模块后查看内置变量

内置变量

1.$args 用户在浏览器中查找的相关参数(uri中?之后的字段)

2.$document_root 站点根目录所在的位置
3.$document_uri 去除url中域名部分后所剩下的目录
4.$host 所访问的主机
5.$http_user_agent 客户端所使用的浏览器
6.$http_cookie 客户端的cookie信息
7.$limit_rate 客户端的下载速率0表示不限制速度

server {    server_name www.mylinuxops.com;    location /python {        root /data/www;        index index.html;        echo args: $args;        echo document_root: $document_root;        echo document_uri: $document_uri;        echo host: $host;        echo http_user_agent $http_user_agent;        echo http_cookie: $http_cookie;        echo limit_rate: $limit_rate; }}
[root@localhost ~]# curl www.mylinuxops.com/python?12345args: 12345document_root: /data/wwwdocument_uri: /pythonhost: www.mylinuxops.comhttp_user_agent curl/7.29.0http_cookie:limit_rate: 0

客户端相关的变量

8$remote_addr 显示客户端地址

9.$remote_port 客户端端口
10.$remote_user 客户端用户一般显示为-,只有认证登录的用户才显示
分别测试登录和非登录

server {    server_name www.mylinuxops.com;    location / {        root /data/www;        index index.html;        echo remote_addr: $remote_addr;        echo remote_port: $remote_port;        echo remote_user: $remote_user; }    location /linux {        root /data/www;        index index.html;        echo $host;        auth_basic  "login";        auth_basic_user_file /apps/nginx/conf/.htpasswd;        echo remote_addr: $remote_addr;        echo remote_port: $remote_port;        echo remote_user: $remote_user; }}
[root@localhost ~]# curl www.mylinuxops.comremote_addr: 172.22.27.10remote_port: 56608remote_user:[root@localhost ~]# curl -u masuri:111111 www.mylinuxops.comremote_addr: 172.22.27.10remote_port: 56610remote_user: masuri

请求报文相关的变量

11.$request_body_file

12.$request_method 请求的方法有
13.$request_filename 请求文件在服务器上的位置
14.$request_uri 请求的uri

server {    server_name www.mylinuxops.com;    location / {        root /data/www;        index index.html;        echo request_body_file: $request_body_file;        echo request_method: $request_method;        echo request_filename: $request_filename;        echo request_uri: $request_uri; }}
[root@localhost ~]# curl www.mylinuxops.com/index.htmlrequest_body_file:request_method: GETrequest_filename: /data/www/index.htmlrequest_uri: /index.html

服务器相关的变量

15.$scheme 请求时所时使用的协议

16.$server_protocol 所使用的http协议版本号
17.$server_addr 服务器的IP地址
18.$server_name 服务器名
19.$server_port 服务器所使用的端口
修改配置文件

server {    server_name www.mylinuxops.com;    location / {        root /data/www;        index index.html;                echo server_protocol: $server_protocol;        echo server_addr: $server_addr;        echo server_name: $server_name;        echo server_port: $server_port;        echo scheme: $scheme; }}

测试

[root@localhost ~]# curl www.mylinuxops.comscheme: httpserver_protocol: HTTP/1.1server_addr: 172.22.27.10server_name: www.mylinuxops.comserver_port: 80

nginx自定义访问日志

访问日志是记录客户端即用户的具体请求内容信息,全局配置模块中的error_log是记录nginx服务器运行时的日志保存路径和日志的等级,因此有着本质的区别,而且Nginx的错误日志一般只有一个,但是访问日志可以再不同server中定义多个,定义一个日志需要使用access_log指定日志的保存路径,使用log_format指定日志的格式,格式中定义要保存的具体日志内容。

系统默认日志

1.错误日志

系统的错误日志在主配置文件的全局配置中开启

error_log  logs/error.log;

2.访问日志

系统默认访问日志的格式在主配置文件的http字段中定义,然后在每一个server段中开启访问日志并调用,这样是为了方便日志的管理,确保每个server都有一个独立的访问日志

http {    include       mime.types;    default_type  application/octet-stream;    log_format  main  '$remote_addr - $remote_user [$time_local] "$request" '                      '$status $body_bytes_sent "$http_referer" '                      '"$http_user_agent" "$http_x_forwarded_for"';}

在server中开启并调用日志规则

server {    server_name www.mylinuxops.com;    access_log /var/logs/nginx/mylinuxops.log main;  #开启日志调用日志规则    location / {        root /data/www;        index index.html; }

开启访问日志时需要注意确保日志存放的目录对于nginx来说是有写权限的否则将无法生成日志

生成存放日志的目录

[root@localhost ~]# mkdir /var/log/nginx

检查配置文件并生效

[root@localhost log]# nginx -tnginx: the configuration file /apps/nginx/conf/nginx.conf syntax is oknginx: configuration file /apps/nginx/conf/nginx.conf test is successful[root@localhost log]# nginx -s reload

访问站点,查看访问日志

[root@localhost log]# curl www.mylinuxops.commylinuxops.com[root@localhost log]# cat /var/log/nginx/mylinuxops.log172.22.27.10 - - [30/May/2019:10:34:27 +0800] "GET / HTTP/1.1" 200 15 "-" "curl/7.29.0" "-"

nginx自定以json格式日志

nginx默认的访问日志记录的内容相对比较单一,默认的格式也不方便后期做日志的统计分析,生产环境中通常将nginx日志转换为json日志,然后配合ELK做日志收集、统计、分析

jion格式日志

修改配置文件写入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"}';

在server中调用日志格式

server {    server_name www.mylinuxops.com;    access_log /var/log/nginx/mylinuxops.log access_json;    location / {        root /data/www;        index index.html; }}

检查配置文件,重读配置文件

[root@localhost log]# nginx -tnginx: the configuration file /apps/nginx/conf/nginx.conf syntax is oknginx: configuration file /apps/nginx/conf/nginx.conf test is successful[root@localhost log]# nginx -s reload

再次访问后查看日志

[root@localhost log]# curl www.mylinuxops.commylinuxops.com[root@localhost log]# cat /var/log/nginx/mylinuxops.log172.22.27.10 - - [30/May/2019:10:34:27 +0800] "GET / HTTP/1.1" 200 15 "-" "curl/7.29.0" "-"{"@timestamp":"2019-05-30T11:08:45+08:00",   "host":"172.22.27.10",   "clientip":"172.22.27.10",   "size":15,   "responsetime":0.000,   "upstreamtime":"-",   "upstreamhost":"-",   "http_host":"www.mylinuxops.com",   "uri":"/index.html",   "domain":"www.mylinuxops.com",   "xff":"-",   "referer":"-",   "tcp_xff":"",   "http_user_agent":"curl/7.29.0",   "status":"200"}        #此为json格式的日志

转载于:https://blog.51cto.com/11886307/2403947

你可能感兴趣的文章
【REACT NATIVE 系列教程之一】触摸事件的两种形式与四种TOUCHABLE组件详解
查看>>
一对一培训之视频免费分享-2018-01-21-第 03 阶段-准备-基础-架构-01
查看>>
Microsoft UC 2013 Preview-3-Deploy Microsoft SharePoint Server 2013
查看>>
丢失日志文件的风险与对策
查看>>
从何处入手——小议流程制度规范改进时各种宏观微观,定位执行间的扯淡之处...
查看>>
如何实现将PPT、Word、PDF导入在onenote同一页
查看>>
Centos5.8上面用Shell脚本一键安装mysql5.5.25源码包
查看>>
小程序,将如何助力品牌企业数字化转型升级!
查看>>
《Java从小白到大牛》之第7章 控制语句
查看>>
网站HTTP升级HTTPS完全配置手册
查看>>
VMM2012应用指南之2- 准备VMM2012虚拟机
查看>>
让虚拟化的风暴 来得再猛烈些吧
查看>>
编写校验规则文件
查看>>
Gartner:2013年SIEM市场分析(MQ)
查看>>
基于Windows server 2008 R2和Windows7的企业环境的SSTP(或SSL) ***构建二
查看>>
"log_bin.index not found" 启动报错解决
查看>>
富士康再出击,富连网何以定义新电商?
查看>>
我看360与酷派的“爱情”之争
查看>>
初入运维职场的老男孩教育学员必须坚守的素质和态度!
查看>>
19个Linux备份压缩命令
查看>>