×ðÁú¿­Ê±¹ÙÍøµÇ¼

ÔõÑùʹÓÃNginxʵÏÖ»ùÓÚIPµØµãµÄ»á¼û¿ØÖÆ

ÔõÑùʹÓÃnginxʵÏÖ»ùÓÚipµØµãµÄ»á¼û¿ØÖÆ

СÐò£º

ÔÚÍøÂçÇå¾²ÖÐ £¬¹ØÓÚͨ¹ýIPµØµã¾ÙÐлá¼û¿ØÖÆÊÇÒ»ÖÖ³£¼ûµÄ·½·¨¡£Nginx×÷Ϊһ¿î¸ßÐÔÄܵÄWebЧÀÍÆ÷ £¬Ò²ÌṩÁËÏìÓ¦µÄÄ £¿éÒÔÖ§³Ö»ùÓÚIPµØµãµÄ»á¼û¿ØÖÆ¡£±¾ÎĽ«ÏÈÈÝÔõÑùʹÓÃNginxÀ´ÊµÏÖ»ùÓÚIPµØµãµÄ»á¼û¿ØÖÆ £¬²¢¸½ÉÏÏìÓ¦µÄ´úÂëʾÀý¡£

Ò»¡¢NginxµÄ»á¼û¿ØÖÆÄ £¿é

NginxÌṩÁËÐí¶àÄ £¿é £¬ÓÃÓÚʵÏÖ²î±ðµÄ¹¦Ð§¡£ËäÈ» £¬ÒªÊµÏÖ»ùÓÚIPµØµãµÄ»á¼û¿ØÖÆ £¬ÎÒÃÇÐèҪʹÓõ½NginxµÄ»á¼û¿ØÖÆÄ £¿é¡£³£ÓõÄNginx»á¼û¿ØÖÆÄ £¿éÓÐÒÔÏÂÁ½ÖÖ£º

ngx_http_access_module£º¸ÃÄ £¿é¿ÉÒÔͨ¹ýÉèÖÃÎļþÀ´¿ØÖƶԿͻ§¶ËµÄ»á¼ûȨÏÞ £¬¿ÉÒÔÔÊÐí»òÕ߾ܾøijЩIPµØµã»òÕßIPµØµã¶ÎµÄ»á¼û¡£

ngx_http_geo_module£º¸ÃÄ £¿é¿ÉÒÔƾ֤¿Í»§¶ËµÄµØÓßλÖÃÐÅÏ¢¾ÙÐлá¼û¿ØÖÆ £¬¿ÉÒÔÔÊÐí»òÕ߾ܾøijЩÌض¨ÇøÓòµÄ»á¼û¡£

ÏÂÃæ»®·ÖÏÈÈÝÕâÁ½ÖÖÄ £¿éµÄʹÓÃÒªÁì¡£

¶þ¡¢Ê¹Óà ngx_http_access_module ʵÏÖ»ùÓÚIPµØµãµÄ»á¼û¿ØÖÆ

ngx_http_access_module Ä £¿é¿ÉÒÔͨ¹ýÔÚ Nginx ÉèÖÃÎļþÖÐµÄ http¡¢server »ò location ÉèÖÿéÖÐÌí¼Ó allow ºÍ deny Ö¸ÁîÀ´ÊµÏÖ»ùÓÚ IP µØµãµÄ»á¼û¿ØÖÆ¡£

ÀýÈç £¬ÎÒÃÇÏëÔÊÐíIPµØµãΪ192.168.0.1µÄ¿Í»§¶Ë»á¼ûЧÀÍÆ÷ £¬²¢¾Ü¾øIPµØµãΪ192.168.0.2µÄ¿Í»§¶Ë»á¼ûЧÀÍÆ÷ £¬ÄÇô¿ÉÒÔƾ֤ÒÔÏ·½·¨¾ÙÐÐÉèÖãº

http {
  server {
    listen       80;
    server_name  localhost;
    
    location / {
      deny 192.168.0.2;
      allow 192.168.0.1;
      deny all;
    }
  }
}

µÇ¼ºó¸´ÖÆ

ÉÏÊöÉèÖÃÖÐ £¬deny Ö¸ÁîÓÃÓھܾøijЩ IP µØµãµÄ»á¼û £¬¶ø allow Ö¸ÁîÔòÓÃÓÚÔÊÐíijЩ IP µØµãµÄ»á¼û¡£deny all ÌåÏÖ³ýÁËÔÊÐíÁбíÖÐµÄ IP µØµãÍâ £¬ÆäËû IP µØµã¶¼½«±»¾Ü¾ø¡£

Èý¡¢Ê¹Óà ngx_http_geo_module ʵÏÖ»ùÓÚIPµØµãµÄ»á¼û¿ØÖÆ

ngx_http_geo_module Ä £¿é¿ÉÒÔͨ¹ýÔÚNginxÉèÖÃÎļþÖеÄhttp¡¢server»òlocationÉèÖÿéÖÐÌí¼ÓgeoÖ¸ÁîºÍgeoip_countryÖ¸ÁîÀ´ÊµÏÖ»ùÓÚIPµØµãµÄ»á¼û¿ØÖÆ¡£

Ê×ÏÈ £¬ÐèÒªÔÚÉèÖÃÎļþµÄhttp¿éÖÐʹÓÃgeoip_countryÖ¸Áî¼ÓÔØIPµØÓßλÖÃÊý¾Ý¿âÎļþ £¬ÀýÈ磺

http {
  geoip_country /path/to/GeoIP.dat;
}

µÇ¼ºó¸´ÖÆ

È»ºó £¬ÔÚÏìÓ¦µÄserver»òlocationÉèÖÿéÖÐʹÓÃgeoÖ¸ÁîÀ´Æ¥ÅäIPµØµãËù¶ÔÓ¦µÄ¹ú¼Ò £¬²¢Æ¾Ö¤ÐèÒª¾ÙÐлá¼û¿ØÖÆ¡£ÀýÈç £¬ÎÒÃÇÏë½öÔÊÐíÖйú´ó½µÄIPµØµã»á¼ûЧÀÍÆ÷ £¬¿ÉÒÔƾ֤ÒÔÏ·½·¨¾ÙÐÐÉèÖãº

http {
  server {
    listen       80;
    server_name  localhost;
    
    location / {
      geo $country {
          default 0;
          CN 1;
      }
      
      if ($country = 0) {
          return 403;
      }
    }
  }
}

µÇ¼ºó¸´ÖÆ

ÉÏÊöÉèÖÃÖÐ £¬geo Ö¸ÁîÓÃÓÚƾ֤ IP µØµã»ñÈ¡ÆäËù¶ÔÓ¦µÄ¹ú¼Ò´úÂë £¬$country ±äÁ¿¼´ÎªËù»ñÈ¡µÄ¹ú¼Ò´úÂë¡£if ($country = 0) ÌåÏÖÈôÊÇËù»ñÈ¡µÄ¹ú¼Ò´úÂëΪ0£¨ÌåÏÖ·ÇÖйú´ó½µÄIPµØµã£© £¬Ôò·µ»Ø403¹ýʧҳÃæ¡£

½áÓ

ͨ¹ýNginxµÄ»á¼û¿ØÖÆÄ £¿é £¬ÎÒÃÇ¿ÉÒÔÇáËɵØʵÏÖ»ùÓÚIPµØµãµÄ»á¼û¿ØÖÆ¡£Í¨¹ýºÏÀíÉèÖà £¬¿ÉÒÔÌá¸ßЧÀÍÆ÷µÄÇå¾²ÐÔºÍÎȹÌÐÔ £¬±£»¤Ð§ÀÍÆ÷ÃâÊܲ»Á¼IPµØµãµÄ»á¼û¡£Ï£Íû±¾ÎĶÔÄúÃ÷È·ºÍʹÓÃNginxµÄ»á¼û¿ØÖÆÄ £¿éÓÐËù×ÊÖú £¬Ð»Ð»ÔĶÁ¡£

²Î¿¼×ÊÁÏ£º

Nginx¹Ù·½Îĵµ£ºhttp://nginx.org/en/docs/http/ngx_http_access_module.html

Nginx¹Ù·½Îĵµ£ºhttp://nginx.org/en/docs/http/ngx_http_geo_module.html

ÒÔÉϾÍÊÇÔõÑùʹÓÃNginxʵÏÖ»ùÓÚIPµØµãµÄ»á¼û¿ØÖƵÄÏêϸÄÚÈÝ £¬¸ü¶àÇë¹Ø×¢±¾ÍøÄÚÆäËüÏà¹ØÎÄÕ£¡

ÃâÔð˵Ã÷£ºÒÔÉÏչʾÄÚÈÝȪԴÓÚÏàÖúýÌå¡¢ÆóÒµ»ú¹¹¡¢ÍøÓÑÌṩ»òÍøÂçÍøÂçÕûÀí £¬°æȨÕùÒéÓë±¾Õ¾ÎÞ¹Ø £¬ÎÄÕÂÉæ¼°¿´·¨Óë¿´·¨²»´ú±í×ðÁú¿­Ê±¹ÙÍøµÇ¼ÂËÓÍ»úÍø¹Ù·½Ì¬¶È £¬Çë¶ÁÕß½ö×ö²Î¿¼¡£±¾ÎĽӴýתÔØ £¬×ªÔØÇë˵Ã÷À´ÓÉ¡£ÈôÄúÒÔΪ±¾ÎÄÇÖÕ¼ÁËÄúµÄ°æȨÐÅÏ¢ £¬»òÄú·¢Ã÷¸ÃÄÚÈÝÓÐÈκÎÉæ¼°ÓÐÎ¥¹«µÂ¡¢Ã°·¸Ö´·¨µÈÎ¥·¨ÐÅÏ¢ £¬ÇëÄúÁ¬Ã¦ÁªÏµ×ðÁú¿­Ê±¹ÙÍøµÇ¼ʵʱÐÞÕý»òɾ³ý¡£

Ïà¹ØÐÂÎÅ

ÁªÏµ×ðÁú¿­Ê±¹ÙÍøµÇ¼

18523999891

¿É΢ÐÅÔÚÏß×Éѯ

ÊÂÇéʱ¼ä£ºÖÜÒ»ÖÁÖÜÎå £¬9:30-18:30 £¬½ÚãåÈÕÐÝÏ¢

QR code
ÍøÕ¾µØͼ