打造一套NGINX监控0. 前文笔者之前无心查看nginx的拜访日志access.log文件,发现有很多的像/phpMyadmin/index.php这种拜访,IP归属地来自世界各地,应该有人在用肉鸡在始终扫描服务器破绽,就萌发了想打造一套nginx监控零碎的想法。我的要求很简略,只有能在网站上实时查看日志就行,不必每次去服务器查看。最开始我想到的是ELK这套解决方案,我之前我理解过,然而前面想了一下,ELK有点太重了,次要是外围的部件Elasticsearch是用Java写的,比拟占资源,用在我这里有点杀鸡用牛刀的感觉(服务器扛不住)。前面在网上查找的时候,理解到了loki+promtail+grafana的这套轻量级解决方案,便决定部署来试试看。
以下操作应用的是Centos 7进行演示1. 装置nginx编译nginx,加载ngx_http_geoip2_module模块,获取拜访ip的geoip2区域信息1.1 下载、编译nginx# 装置编译环境$ yum -y install gcc pcre pcre-devel zlib zlib-devel openssl openssl-devel# 下载安装libmaxminddb$ wget https://github.com/maxmind/libmaxminddb/releases/download/1.3.2/libmaxminddb-1.3.2.tar.gz$ tar -zxvf libmaxminddb-1.3.2.tar.gz$ cd libmaxminddb-1.3.2$ ./configure && make && make install$ echo /usr/local/lib >> /etc/ld.so.conf.d/local.conf $ ldconfig# 下载ngx_http_geoip2_module模块$ yum install -y git$ git clone https://github.com/leev/ngx_http_geoip2_module# 下载编译nginx$ wget https://nginx.org/download/nginx-1.18.0.tar.gz$ tar -zxvf nginx-1.18.0.tar.gz$ cd nginx-1.18.0# 如果之前曾经装置过nginx,能够用/path/to/nginx/sbin/nginx -V查看nginx编译参数# /root/ngx_http_geoip2_module换成你本人的门路$ ./configure --prefix=/usr/local/nginx --with-http_ssl_module --add-module=/root/ngx_http_geoip2_module$ make && make install1.2 下载GeoLite2数据库maxmind官网下载,能下载最新的,但须要注册登录
笔者提供的,不是最新的,但应该也够用GeoLite2-City_20220705.tar.gzGeoLite2-Country_20220705.tar.gz
$ tar -zxvf GeoLite2-City_20220705.tar.gz$ mv ./GeoLite2-City_20220705/GeoLite2-City.mmdb /usr/local/nginx/conf$ tar -zxvf GeoLite2-Country_20220705.tar.gz$ mv ./GeoLite2-Country_20220705/GeoLite2-Country.mmdb /usr/local/nginx/conf1.3 配置nginx须要将日志格局配置成JSON,外面蕴含了geoip_country_codehttp { include mime.types; default_type application/octet-stream; log_format json_analytics '{' '"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": "$geoip2_data_country_code"' '}'; access_log logs/json_access.log json_analytics; geoip2 /usr/local/nginx/conf/GeoLite2-Country.mmdb { $geoip2_data_country_code country iso_code; $geoip2_data_country_name country names en; } geoip2 /usr/local/nginx/conf/GeoLite2-City.mmdb { $geoip2_data_city_name city names en; $geoip2_data_province_name subdivisions 0 names en; }}1.4 启动nginx$ /usr/local/nginx/sbin/nginx2. 装置loki、promtail、grafana应用docker部署,须要提前装置好docker,这里不再赘述,具体参见docker官网docker官网
...