{"id":1149,"date":"2022-03-14T13:13:56","date_gmt":"2022-03-14T05:13:56","guid":{"rendered":"https:\/\/www.yinyubo.com\/?p=1149"},"modified":"2022-05-16T18:00:36","modified_gmt":"2022-05-16T10:00:36","slug":"nginx","status":"publish","type":"post","link":"https:\/\/www.yinyubo.com\/?p=1149","title":{"rendered":"nginx\u52a8\u6001\u589e\u52a0\u6a21\u5757ngx_http_geoip2_module"},"content":{"rendered":"\n<h2 class=\"wp-block-heading\">0.\u524d\u63d0\uff1a<\/h2>\n\n\n\n<p>1.\u5df2\u7ecf\u9605\u8bfb\u8fc7\u6211\u7684\u53e6\u4e00\u7bc7\u52a8\u6001\u589e\u52a0nginx-module-vts\u6a21\u5757\u7684\u6587\u7ae0\uff0c\u670d\u52a1\u5668\u91cc\u5df2\u7ecf\u5b89\u88c5\u4e86nginx\u548c\u5176\u6e90\u7801\u3002https:\/\/www.yinyubo.com\/2022\/03\/14\/apt%e6%96%b9%e5%bc%8f%e5%ae%89%e8%a3%85nginx%e4%bb%a5%e5%8f%8a%e5%8a%a8%e6%80%81%e5%a2%9e%e5%8a%a0%e6%a8%a1%e5%9d%97nginx-module-vts\/<\/p>\n\n\n\n<p>2.\u5df2\u7ecf\u53bbGeoLite2\u7684\u5b98\u7f51\u4e0b\u8f7d\u4e86GeoLite2-Country.mmdb\u6587\u4ef6\uff0c\u8fd9\u4e2a\u7f51\u7ad9\u9700\u8981\u6ce8\u518c\u624d\u80fd\u4e0b\u8f7d<\/p>\n\n\n\n<p>3.\u53ef\u4ee5\u53c2\u8003\u7684github\u7f51\u7ad9\u6709<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>https:&#47;&#47;github.com\/leev\/ngx_http_geoip2_module\n\nhttps:\/\/github.com\/maxmind\/libmaxminddb<\/code><\/pre>\n\n\n\n<h2 class=\"wp-block-heading\">1.\u5b89\u88c5<a href=\"https:\/\/github.com\/maxmind\/libmaxminddb\">libmaxminddb<\/a><\/h2>\n\n\n\n<pre class=\"wp-block-code\"><code>\nsudo add-apt-repository ppa:maxmind\/ppa\nsudo apt update\nsudo apt install libmaxminddb0 libmaxminddb-dev mmdb-bin<\/code><\/pre>\n\n\n\n<h2 class=\"wp-block-heading\">2.\u4e0b\u8f7d<strong>ngx_http_geoip2_module<\/strong>\u6e90\u7801\u4ee5\u53ca\u52a8\u6001\u7f16\u8bd1<\/h2>\n\n\n\n<pre class=\"wp-block-code\"><code>\n# \u4e0b\u8f7dngx_http_geoip2_module\u6e90\u7801\ngit clone https:\/\/github.com\/leev\/ngx_http_geoip2_module.git\n#cd nginx\u6e90\u7801\u76ee\u5f55\uff0c\u4f8b\u5982\u4e0b\u9762\u7684\u547d\u4ee4\ncd nginx-1.20.2\/\n# \u8fdb\u884c\u52a8\u6001\u7f16\u8bd1\n.\/configure --with-compat --prefix=\/etc\/nginx --sbin-path=\/usr\/sbin\/nginx --add-dynamic-module=..\/ngx_http_geoip2_module --with-stream\nmake modules\ncp objs\/ngx_http_geoip2_module.so \/etc\/nginx\/modules\/.<\/code><\/pre>\n\n\n\n<h2 class=\"wp-block-heading\">3.\u5728nginx\u914d\u7f6e\u6587\u4ef6\u91cc\u5f15\u5165geoip2<\/h2>\n\n\n\n<p>\u8fd9\u91cc\u6211\u4ee5\u5728nginx-module-vts\u91cc\u52a0\u5165geoip2\u5730\u533a\u89e3\u6790\u4e3a\u4f8b\uff0c\u4fee\u6539nginx.conf<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>...\nload_module modules\/ngx_http_vhost_traffic_status_module.so;\nload_module modules\/ngx_http_geoip2_module.so;\n...\nhttp {\n    ...\n    geoip2 \/home\/lzw\/GeoLite2-Country_20220222\/GeoLite2-Country.mmdb {\n        auto_reload 5m;\n        $geoip2_metadata_country_build metadata build_epoch;\n        $geoip2_data_country_code default=CN source=$remote_addr country iso_code;\n        $geoip2_data_country_name country names es;\n    }\n    geoip2 \/home\/lzw\/GeoLite2-City_20220222\/GeoLite2-City.mmdb {\n        $geoip2_data_city_name default=Nanjing city names en;\n        $geoip2_data_latitude location latitude;\n        $geoip2_data_longitude location longitude;\n        $geoip2_data_postalcode postal code;\n    }\n    default_type  application\/octet-stream;\n    vhost_traffic_status_zone;\n    vhost_traffic_status_filter_by_set_key $geoip2_data_country_code country::*;\n    vhost_traffic_status_filter_by_set_key $geoip2_data_city_name city::*;\n    vhost_traffic_status_filter_by_set_key \"$geoip2_data_latitude,$geoip2_data_longitude\" latlong::*;\n    vhost_traffic_status_filter_by_set_key $geoip2_data_longitude longitude::*;\n    vhost_traffic_status_filter_by_set_key $geoip2_data_latitude latitude::*;\n    vhost_traffic_status_filter_by_set_key $geoip2_data_postalcode postal::*;\n    log_format  main  '$remote_addr - $remote_user &#91;$time_local] \"$request\" '\n                      '$status $body_bytes_sent \"$http_referer\" '\n                      '\"$http_user_agent\" \"$http_x_forwarded_for\"';\n \n    access_log  \/var\/log\/nginx\/access.log  main;\n    ...\n}<\/code><\/pre>\n\n\n\n<p>\u5728\u6bcf\u4e00\u4e2a\u88ab\u76d1\u63a7\u7684conf\u6587\u4ef6\u91cc \u6dfb\u52a0vhost_traffic_status_filter_by_set_key \u4fe1\u606f,\u4f8b\u5982\u4e0b\u9762\u6211\u6dfb\u52a0\u4e866\u4e2a\u7b5b\u9009\u9879<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>\nserver {\n    listen       5320;\n    server_name  localhost;    \n    vhost_traffic_status_filter_by_set_key $geoip2_data_country_code country::$server_name;\n    vhost_traffic_status_filter_by_set_key $geoip2_data_city_name city::$server_name;\n    vhost_traffic_status_filter_by_set_key \"$geoip2_data_latitude,$geoip2_data_longitude\" latlong::$server_name;\n    vhost_traffic_status_filter_by_set_key $geoip2_data_longitude longitude::$server_name;\n    vhost_traffic_status_filter_by_set_key $geoip2_data_latitude latitude::$server_name;\n    vhost_traffic_status_filter_by_set_key $geoip2_data_postalcode postal::$server_name;  \n \n    location \/status {\n        vhost_traffic_status_display;\n        vhost_traffic_status_display_format html;\n        root   \/usr\/share\/nginx\/html;\n        index  index.html index.htm;\n    }\n}<\/code><\/pre>\n","protected":false},"excerpt":{"rendered":"<p>0.\u524d\u63d0\uff1a 1.\u5df2\u7ecf\u9605\u8bfb\u8fc7\u6211\u7684\u53e6\u4e00\u7bc7\u52a8\u6001\u589e\u52a0nginx-module-vts\u6a21\u5757\u7684\u6587\u7ae0\uff0c\u670d\u52a1\u5668\u91cc\u5df2\u7ecf\u5b89\u88c5\u4e86ng [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"closed","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[7],"tags":[],"class_list":["post-1149","post","type-post","status-publish","format-standard","hentry","category-7"],"_links":{"self":[{"href":"https:\/\/www.yinyubo.com\/index.php?rest_route=\/wp\/v2\/posts\/1149","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/www.yinyubo.com\/index.php?rest_route=\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.yinyubo.com\/index.php?rest_route=\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.yinyubo.com\/index.php?rest_route=\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/www.yinyubo.com\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=1149"}],"version-history":[{"count":2,"href":"https:\/\/www.yinyubo.com\/index.php?rest_route=\/wp\/v2\/posts\/1149\/revisions"}],"predecessor-version":[{"id":1152,"href":"https:\/\/www.yinyubo.com\/index.php?rest_route=\/wp\/v2\/posts\/1149\/revisions\/1152"}],"wp:attachment":[{"href":"https:\/\/www.yinyubo.com\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=1149"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.yinyubo.com\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=1149"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.yinyubo.com\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=1149"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}