Experimental environment: Before the elk has been initially installed, only need to send Nginx log sent over it, Nginx IP: 192.168.137.101. Because the server only collects logs, the following operations are done entirely on the nginx machine. Thanks to the "ELK Stack authoritative guide" book, the contents of the following configuration are borrowed from this book.
#/opt/logstash/bin/logstash-plugin Install Logstash-filter-geoip
Install this plug-in, the old version of the previous need to download GeoIP and the database of the plugin, now do not need, has been assembled into a plug-in, directly in the configuration file call just fine.
#vim/etc/logstash/conf.d/nginx-out-es.conf
My Logstash profile is in this path, and the contents are configured according to the book mentioned above.
Input { file { path => "/usr/local/nginx/logs/access.log" codec => json }}filter { geoip { source => "ClientIP" fields => ["City_name", "Country_code2", "country_name", "latitude", "longitude", "Region_name"] remove_field => ["[geoip][ Latitude] ", " [Geoip][longitude] "] } mutate { split => [ "Upstreamtime", "," ] } mutate { convert => [ "UPstreamtime ", " float " ] }}output { elasticsearch { hosts => ["192.168.137.191:9200"] index => "nginx-access-log-%{+yyyy. MM.dd.HH} " workers => 1 template_overwrite => true }}
#vim/usr/local/nginx/conf/nginx.conf
log_format 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 ", '    &Nbsp; ' "url": "$uri", ' ' xff ': "$http _x_ Forwarded_for ", ' ' referer ': ' $http _referer ', ' ' agent ': ' $http _user_agent ', ' ' status ': ' $status ', ' ' Country_code ': "$geoip _city_ Country_code ", ' ' country_code3 ': ' $geoip _city_country_code3 ', ' ' "City_country_name": "$geoip _city_country_name", ' ' region_name ': ' $geoip _region ', ' ' city_name ': "$geoip _city", ' ' "Postal _code ":" $geoip _postal_code "} ';access_log logs/access.log json;
This nginx configuration content is based on the contents of the book is written out, the bottom of some of the city's information is to fill it up.
#/opt/logstash/bin/logstash--configtest-f/etc/logstash/conf.d/nginx-out-es.conf
Test the Logstash configuration file syntax correctly
#service nginx Restart
Restart Nginx to make the configuration file effective
#nohup/opt/logstash/bin/logstash-f/etc/logstash/conf.d/nginx-out-es.conf &
Background silent start Longstash
You can then view the log messages received from the server.
650) this.width=650; "Src=" Http://s2.51cto.com/wyfs02/M02/85/25/wKiom1ea95CAjkqaAAC1tCdfSws333.png-wh_500x0-wm_3 -wmp_4-s_584186291.png "style=" Float:none; "title=" QQ picture 20160729142736.png "alt=" Wkiom1ea95cajkqaaac1tcdfsws333.png-wh_50 "/>
The above image is the result of intranet access
650) this.width=650; "Src=" Http://s2.51cto.com/wyfs02/M01/85/25/wKioL1ea95CxLClYAAC_xS8D5ys786.png-wh_500x0-wm_3 -wmp_4-s_3180749795.png "title=" Qq20160729142817.png "style=" Float:none; "alt=" wkiol1ea95cxlclyaac_ Xs8d5ys786.png-wh_50 "/>
The above image is the result of a public network, and will automatically add location and city information and coordinates.
In addition to using the above method can also use Nginx own GeoIP module (need to download the dependent Library Geoip,geoip-devel,perl-geo-ip ) to analyze the incoming IP address and then send the complete information to the ES side, but this method will increase the resource consumption of nginx. The method is simple after the module is added to download the GeoIP database file, in Nginx specify the GEOIP database file path, remember to add permissions otherwise can not read, other configuration content does not need to modify, restart Nginx can be. The specific method Nginx official website is some very detailed.
This article is from the "White and Food" blog, please be sure to keep this source http://ls40905250.blog.51cto.com/1270522/1831698
Elk Monitoring Log nginx (integrated GeoIP)