最近客户有个新需求 , 就是想查看网站的访问情况 , 由于网站没有做google的统计和百度的统计 , 所以访问情况 , 只能通过日志查看 , 通过脚本的形式给客户导出也不太实际 , 给客户写个简单的页面 , 咱也做不到
成熟的日志解决方案 , 那就是ELK , 还有现在比较火的Loki , (当然还有很多其他解决方案 , 比如Splunk、Datadog等) , 那我们这个小网站 , 小体量 , 必然是选择Loki来做
所以这次就采用Nginx+Promtail+Loki+Grafana来做一个简单的Nginx日志展示
文章插图
Nginx的安装不多说 , Promtail和Loki都选用二进制的方式进行安装 , 直接下载对应版本的二进制文件 , 解压后指定配置文件启动即可
其中promtail配置文件如下配置:
【这套Nginx日志解决方案,真香】
server:http_listen_port: 9080grpc_listen_port: 0positions:filename: /tmp/positions.yamlclients:- url: http://localhost:3100/loki/api/v1/pushscrape_configs:- job_name: nginxpipeline_stages:- replace:expression: '(?:[0-9]{1,3}.){3}([0-9]{1,3})'replace: '***'static_configs:- targets:- localhostlabels:job: nginx_access_loghost: expatsxxxxsagent: promtail__path__: /var/log/nginx/expatshxxxxs.access.log
日志收集工作完成后 , 在Nginx中 , 需要修改日志格式 , 修改Nginx的日志格式为Json格式 , 配置如下:log_format json_analytics escape=json '{''"msec": "$msec", ' # request unixtime in seconds with a milliseconds resolution'"connection": "$connection", ' # connection serial number'"connection_requests": "$connection_requests", ' # number of requests made in connection'"pid": "$pid", ' # process pid'"request_id": "$request_id", ' # the unique request id'"request_length": "$request_length", ' # request length (including headers and body)'"remote_addr": "$remote_addr", ' # client IP'"remote_user": "$remote_user", ' # client HTTP username'"remote_port": "$remote_port", ' # client port'"time_local": "$time_local", ''"time_iso8601": "$time_iso8601", ' # local time in the ISO 8601 standard format'"request": "$request", ' # full path no arguments if the request'"request_uri": "$request_uri", ' # full path and arguments if the request'"args": "$args", ' # args'"status": "$status", ' # response status code'"body_bytes_sent": "$body_bytes_sent", ' # the number of body bytes exclude headers sent to a client'"bytes_sent": "$bytes_sent", ' # the number of bytes sent to a client'"http_referer": "$http_referer", ' # HTTP referer'"http_user_agent": "$http_user_agent", ' # user agent'"http_x_forwarded_for": "$http_x_forwarded_for", ' # http_x_forwarded_for'"http_host": "$http_host", ' # the request Host: header'"server_name": "$server_name", ' # the name of the vhost serving the request'"request_time": "$request_time", ' # request processing time in seconds with msec resolution'"upstream": "$upstream_addr", ' # upstream backend server for proxied requests'"upstream_connect_time": "$upstream_connect_time", ' # upstream handshake time incl. TLS'"upstream_header_time": "$upstream_header_time", ' # time spent receiving upstream headers'"upstream_response_time": "$upstream_response_time", ' # time spend receiving upstream body'"upstream_response_length": "$upstream_response_length", ' # upstream response length'"upstream_cache_status": "$upstream_cache_status", ' # cache HIT/MISS where Applicable'"ssl_protocol": "$ssl_protocol", ' # TLS protocol'"ssl_cipher": "$ssl_cipher", ' # TLS cipher'"scheme": "$scheme", ' # http or https'"request_method": "$request_method", ' # request method'"server_protocol": "$server_protocol", ' # request protocol, like HTTP/1.1 or HTTP/2.0'"pipe": "$pipe", ' # "p" if request was pipelined, "." otherwise'"gzip_ratio": "$gzip_ratio", ''"http_cf_ray": "$http_cf_ray",''"geoip_country_code": "$geoip_country_code"''}';
推荐阅读
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
- Nginx - 最小配置!你值得拥有
- Nginx 推出 Kubernetes 微服务参考架构
- MySQL的监控原理
- Linux安装调试Nginx
- Loki日志系统,比ELK轻量多了
- nginx简易实现权限登录
- Nginx负载均衡
- PHP+NGINX服务器性能安全优化
- nginx real ip?nginx x-real-ip
- Oracle日志文件