本文共 5949 字,大约阅读时间需要 19 分钟。
安装第三方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 请求的uriserver { 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
访问日志是记录客户端即用户的具体请求内容信息,全局配置模块中的error_log是记录nginx服务器运行时的日志保存路径和日志的等级,因此有着本质的区别,而且Nginx的错误日志一般只有一个,但是访问日志可以再不同server中定义多个,定义一个日志需要使用access_log指定日志的保存路径,使用log_format指定日志的格式,格式中定义要保存的具体日志内容。
系统的错误日志在主配置文件的全局配置中开启
error_log logs/error.log;
系统默认访问日志的格式在主配置文件的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默认的访问日志记录的内容相对比较单一,默认的格式也不方便后期做日志的统计分析,生产环境中通常将nginx日志转换为json日志,然后配合ELK做日志收集、统计、分析
修改配置文件写入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