{"id":49730,"date":"2024-10-08T07:01:13","date_gmt":"2024-10-07T23:01:13","guid":{"rendered":"https:\/\/server.hk\/cnblog\/49730\/"},"modified":"2024-10-08T07:01:13","modified_gmt":"2024-10-07T23:01:13","slug":"nginx-%e6%8a%80%e5%b7%a7%ef%bc%9a%e5%af%a6%e7%8f%be-ngx_http_geo_module-%e9%80%b2%e8%a1%8c%e5%9f%ba%e6%96%bc%e5%9c%b0%e7%90%86%e7%9a%84%e9%85%8d%e7%bd%ae","status":"publish","type":"post","link":"https:\/\/server.hk\/cnblog\/49730\/","title":{"rendered":"Nginx \u6280\u5de7\uff1a\u5be6\u73fe ngx_http_geo_module \u9032\u884c\u57fa\u65bc\u5730\u7406\u7684\u914d\u7f6e"},"content":{"rendered":"<h1 id=\"nginx-%e6%8a%80%e5%b7%a7%ef%bc%9a%e5%af%a6%e7%8f%be-ngx_http_geo_module-%e9%80%b2%e8%a1%8c%e5%9f%ba%e6%96%bc%e5%9c%b0%e7%90%86%e7%9a%84%e9%85%8d%e7%bd%ae-OhaoXpvYBv\">Nginx \u6280\u5de7\uff1a\u5be6\u73fe ngx_http_geo_module \u9032\u884c\u57fa\u65bc\u5730\u7406\u7684\u914d\u7f6e<\/h1>\n<p>Nginx \u662f\u4e00\u6b3e\u9ad8\u6548\u80fd\u7684\u7db2\u9801\u4f3a\u670d\u5668\uff0c\u5ee3\u6cdb\u61c9\u7528\u65bc\u5404\u7a2e\u7db2\u7ad9\u548c\u61c9\u7528\u7a0b\u5f0f\u4e2d\u3002\u5176\u6a21\u7d44\u5316\u7684\u8a2d\u8a08\u4f7f\u5f97\u7528\u6236\u80fd\u5920\u6839\u64da\u9700\u6c42\u9032\u884c\u64f4\u5c55\u548c\u914d\u7f6e\u3002\u5176\u4e2d\uff0c<code>ngx_http_geo_module<\/code> \u662f\u4e00\u500b\u975e\u5e38\u6709\u7528\u7684\u6a21\u7d44\uff0c\u80fd\u5920\u6839\u64da\u5ba2\u6236\u7aef\u7684 IP \u5730\u5740\u4f86\u9032\u884c\u5730\u7406\u4f4d\u7f6e\u7684\u8b58\u5225\uff0c\u5f9e\u800c\u5be6\u73fe\u57fa\u65bc\u5730\u7406\u7684\u914d\u7f6e\u3002<\/p>\n<h2 id=\"%e4%bb%80%e9%ba%bc%e6%98%af-ngx_http_geo_module%ef%bc%9f-OhaoXpvYBv\">\u4ec0\u9ebc\u662f ngx_http_geo_module\uff1f<\/h2>\n<p><code>ngx_http_geo_module<\/code> \u662f Nginx \u7684\u4e00\u500b\u5167\u5efa\u6a21\u7d44\uff0c\u4e3b\u8981\u7528\u65bc\u6839\u64da\u5ba2\u6236\u7aef\u7684 IP \u5730\u5740\u4f86\u5224\u65b7\u5176\u5730\u7406\u4f4d\u7f6e\u3002\u9019\u500b\u6a21\u7d44\u53ef\u4ee5\u7528\u4f86\u8a2d\u7f6e\u8b8a\u6578\uff0c\u9019\u4e9b\u8b8a\u6578\u53ef\u4ee5\u5728 Nginx \u7684\u5176\u4ed6\u914d\u7f6e\u4e2d\u4f7f\u7528\uff0c\u4f8b\u5982\u6839\u64da\u7528\u6236\u7684\u5730\u7406\u4f4d\u7f6e\u4f86\u63d0\u4f9b\u4e0d\u540c\u7684\u5167\u5bb9\u6216\u670d\u52d9\u3002<\/p>\n<h2 id=\"%e5%a6%82%e4%bd%95%e9%85%8d%e7%bd%ae-ngx_http_geo_module-OhaoXpvYBv\">\u5982\u4f55\u914d\u7f6e ngx_http_geo_module<\/h2>\n<p>\u8981\u4f7f\u7528 <code>ngx_http_geo_module<\/code>\uff0c\u9996\u5148\u9700\u8981\u5728 Nginx \u7684\u914d\u7f6e\u6587\u4ef6\u4e2d\u9032\u884c\u8a2d\u7f6e\u3002\u4ee5\u4e0b\u662f\u4e00\u500b\u57fa\u672c\u7684\u914d\u7f6e\u7bc4\u4f8b\uff1a<\/p>\n<pre><code>http {\n    geo $geoip_country_code {\n        default XX;  # Default value for unknown IPs\n        192.0.2.0\/24 US;  # Example for US IP range\n        203.0.113.0\/24 HK;  # Example for HK IP range\n    }\n\n    server {\n        listen 80;\n        server_name example.com;\n\n        location \/ {\n            if ($geoip_country_code = HK) {\n                return 200 \"Welcome to our Hong Kong site!\";\n            }\n            return 200 \"Welcome to our international site!\";\n        }\n    }\n}<\/code><\/pre>\n<p>\u5728\u9019\u500b\u7bc4\u4f8b\u4e2d\uff0c\u6211\u5011\u5b9a\u7fa9\u4e86\u4e00\u500b\u540d\u70ba <code>$geoip_country_code<\/code> \u7684\u8b8a\u6578\uff0c\u4e26\u6839\u64da\u4e0d\u540c\u7684 IP \u7bc4\u570d\u8a2d\u7f6e\u4e86\u5c0d\u61c9\u7684\u570b\u5bb6\u4ee3\u78bc\u3002\u7576\u7528\u6236\u8a2a\u554f\u7db2\u7ad9\u6642\uff0cNginx \u6703\u6839\u64da\u5176 IP \u5730\u5740\u81ea\u52d5\u5224\u65b7\u5176\u5730\u7406\u4f4d\u7f6e\uff0c\u4e26\u8fd4\u56de\u76f8\u61c9\u7684\u5167\u5bb9\u3002<\/p>\n<h2 id=\"%e4%bd%bf%e7%94%a8-geoip-%e6%95%b8%e6%93%9a%e5%ba%ab-OhaoXpvYBv\">\u4f7f\u7528 GeoIP \u6578\u64da\u5eab<\/h2>\n<p>\u70ba\u4e86\u4f7f <code>ngx_http_geo_module<\/code> \u80fd\u5920\u6b63\u78ba\u8b58\u5225\u5730\u7406\u4f4d\u7f6e\uff0c\u60a8\u9700\u8981\u4f7f\u7528 GeoIP \u6578\u64da\u5eab\u3002\u9019\u4e9b\u6578\u64da\u5eab\u53ef\u4ee5\u5f9e MaxMind \u7b49\u63d0\u4f9b\u5546\u7372\u53d6\u3002\u5b89\u88dd\u548c\u914d\u7f6e GeoIP \u6578\u64da\u5eab\u7684\u6b65\u9a5f\u5982\u4e0b\uff1a<\/p>\n<ul>\n<li>\u4e0b\u8f09 GeoIP \u6578\u64da\u5eab\uff0c\u4f8b\u5982 <code>GeoLite2-Country.mmdb<\/code>\u3002<\/li>\n<li>\u5c07\u6578\u64da\u5eab\u6587\u4ef6\u653e\u7f6e\u5728 Nginx \u53ef\u8a2a\u554f\u7684\u76ee\u9304\u4e2d\u3002<\/li>\n<li>\u5728 Nginx \u914d\u7f6e\u6587\u4ef6\u4e2d\u6dfb\u52a0\u4ee5\u4e0b\u884c\uff1a<\/li>\n<\/ul>\n<pre><code>http {\n    geoip_country \/path\/to\/GeoLite2-Country.mmdb;\n}<\/code><\/pre>\n<p>\u9019\u6a23\u914d\u7f6e\u5f8c\uff0cNginx \u5c31\u80fd\u5920\u4f7f\u7528 GeoIP \u6578\u64da\u5eab\u4f86\u8b58\u5225\u7528\u6236\u7684\u5730\u7406\u4f4d\u7f6e\u3002<\/p>\n<h2 id=\"%e9%80%b2%e9%9a%8e%e6%87%89%e7%94%a8-OhaoXpvYBv\">\u9032\u968e\u61c9\u7528<\/h2>\n<p>\u9664\u4e86\u57fa\u672c\u7684\u5730\u7406\u4f4d\u7f6e\u8b58\u5225\uff0c<code>ngx_http_geo_module<\/code> \u9084\u53ef\u4ee5\u7528\u65bc\u66f4\u8907\u96dc\u7684\u5834\u666f\u3002\u4f8b\u5982\uff0c\u60a8\u53ef\u4ee5\u6839\u64da\u7528\u6236\u7684\u5730\u7406\u4f4d\u7f6e\u4f86\u9650\u5236\u8a2a\u554f\u3001\u91cd\u5b9a\u5411\u6d41\u91cf\u6216\u63d0\u4f9b\u7279\u5b9a\u7684\u5167\u5bb9\u3002\u4ee5\u4e0b\u662f\u4e00\u500b\u9032\u968e\u7684\u914d\u7f6e\u7bc4\u4f8b\uff1a<\/p>\n<pre><code>http {\n    geo $geoip_country_code {\n        default XX;\n        192.0.2.0\/24 US;\n        203.0.113.0\/24 HK;\n    }\n\n    server {\n        listen 80;\n        server_name example.com;\n\n        location \/ {\n            if ($geoip_country_code = HK) {\n                rewrite ^\/old-path \/new-path permanent;\n            }\n        }\n    }\n}<\/code><\/pre>\n<p>\u5728\u9019\u500b\u7bc4\u4f8b\u4e2d\uff0c\u4f86\u81ea\u9999\u6e2f\u7684\u7528\u6236\u5c07\u88ab\u6c38\u4e45\u91cd\u5b9a\u5411\u5230\u65b0\u7684\u8def\u5f91\uff0c\u9019\u6a23\u53ef\u4ee5\u6709\u6548\u5730\u7ba1\u7406\u7db2\u7ad9\u7684\u6d41\u91cf\u548c\u5167\u5bb9\u3002<\/p>\n<h2 id=\"%e7%b8%bd%e7%b5%90-OhaoXpvYBv\">\u7e3d\u7d50<\/h2>\n<p>\u901a\u904e\u4f7f\u7528 <code>ngx_http_geo_module<\/code>\uff0cNginx \u7528\u6236\u53ef\u4ee5\u8f15\u9b06\u5be6\u73fe\u57fa\u65bc\u5730\u7406\u4f4d\u7f6e\u7684\u914d\u7f6e\uff0c\u5f9e\u800c\u63d0\u4f9b\u66f4\u5177\u91dd\u5c0d\u6027\u7684\u670d\u52d9\u3002\u7121\u8ad6\u662f\u7c21\u55ae\u7684\u5167\u5bb9\u986f\u793a\u9084\u662f\u8907\u96dc\u7684\u6d41\u91cf\u7ba1\u7406\uff0c\u9019\u500b\u6a21\u7d44\u90fd\u80fd\u5920\u5e6b\u52a9\u7528\u6236\u63d0\u5347\u7db2\u7ad9\u7684\u9748\u6d3b\u6027\u548c\u6548\u7387\u3002\u5982\u679c\u60a8\u6b63\u5728\u5c0b\u627e\u9ad8\u6548\u7684 <a href=\"https:\/\/server.hk\">\u9999\u6e2fVPS<\/a> \u89e3\u6c7a\u65b9\u6848\uff0cServer.HK \u63d0\u4f9b\u591a\u7a2e\u9078\u64c7\uff0c\u6eff\u8db3\u4e0d\u540c\u9700\u6c42\u7684\u7528\u6236\u3002<\/p>\n","protected":false},"excerpt":{"rendered":"<p>\u63a2\u7d22 Nginx \u7684 ngx_http_geo_module\uff0c\u5b78\u7fd2\u5982\u4f55\u5be6\u73fe\u57fa\u65bc\u5730\u7406\u4f4d\u7f6e\u7684\u914d\u7f6e\uff0c\u63d0\u5347\u7db2\u7ad9\u6027\u80fd\u8207\u7528\u6236\u9ad4\u9a57\u3002<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"closed","ping_status":"","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[4924],"tags":[],"class_list":["post-49730","post","type-post","status-publish","format-standard","hentry","category-setup-tutorials"],"_links":{"self":[{"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/posts\/49730","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/comments?post=49730"}],"version-history":[{"count":1,"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/posts\/49730\/revisions"}],"predecessor-version":[{"id":49731,"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/posts\/49730\/revisions\/49731"}],"wp:attachment":[{"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/media?parent=49730"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/categories?post=49730"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/tags?post=49730"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}