NginxÔõÑùʵÏÖ»ùÓÚIPµØµãµÄ»á¼û¿ØÖÆÉèÖÃ
NginxÔõÑùʵÏÖ»ùÓÚIPµØµãµÄ»á¼û¿ØÖÆÉèÖã¬ÐèÒªÏêϸ´úÂëʾÀý
NginxÊÇÏÖÔÚÆÕ±éʹÓõÄÒ»¿î¸ßÐÔÄܵÄWebЧÀÍÆ÷Èí¼þ£¬Ëü³ýÁË¿ÉÒÔ×÷ΪWebЧÀÍÆ÷ʹÓÃÍ⣬»¹¿ÉÒÔ×÷Ϊ·´ÏòÊðÀíЧÀÍÆ÷¡¢¸ºÔØƽºâÆ÷µÈ¡£ÔÚWebЧÀÍÆ÷µÄÓ¦ÓÃÖУ¬Çå¾²ÐÔÊÇÒ»ÏîºÜÊÇÖ÷ÒªµÄ˼Á¿ÒòËØ¡£NginxÌṩÁ˶àÖÖ·½·¨À´ÊµÏÖ»ùÓÚIPµØµãµÄ»á¼û¿ØÖÆÉèÖ㬿ÉÒÔ×ÊÖúÖÎÀíÔ±ÓÐÓõؿØÖƶÔÍøÕ¾×ÊÔ´µÄ»á¼û¡£
Ò»¡¢NginxÉèÖÃÖеÄIPµØµã±äÁ¿
ÔÚNginxµÄÉèÖÃÎļþÖУ¬Í¨¹ýһЩ±äÁ¿À´ÌåÏÖ¿Í»§¶ËµÄIPµØµã¡£ÆäÖÐ×î³£ÓõÄÊÇ$remote_addr±äÁ¿£¬Ëü´ú±íÁË¿Í»§¶ËµÄIPµØµã¡£
¶þ¡¢¼òÆÓµÄIPµØµã¹ýÂËÉèÖÃ
ÔÊÐíÌض¨IPµØµã»á¼û
location / { allow 192.168.1.100; deny all; }
µÇ¼ºó¸´ÖÆ
ÉÏÊöÉèÖÃÌåÏÖÖ»ÔÊÐíIPµØµãΪ192.168.1.100µÄ¿Í»§¶Ë»á¼û¸Ãlocation¿é£¬ÆäËüIPµØµãµÄ¿Í»§¶Ë½«±»¾Ü¾ø¡£ÆäÖÐdeny allÌåÏ־ܾøËùÓÐÆäËüIPµØµãµÄ»á¼û¡£
¾Ü¾øÌض¨IPµØµã»á¼û
location / { deny 192.168.1.200; allow all; }
µÇ¼ºó¸´ÖÆ
ÉÏÊöÉèÖÃÌåÏÖ²»ÔÊÐíIPµØµãΪ192.168.1.200µÄ¿Í»§¶Ë»á¼û¸Ãlocation¿é£¬ÆäËüIPµØµãµÄ¿Í»§¶Ë½«±»ÔÊÐí¡£ÆäÖÐallow allÌåÏÖÔÊÐíËùÓÐÆäËüIPµØµãµÄ»á¼û¡£
Èý¡¢¶à¸öIPµØµã¹ýÂËÉèÖÃ
ÔÊÐí¶à¸öÌض¨IPµØµã»á¼û
location / { allow 192.168.1.100; allow 192.168.1.200; deny all; }
µÇ¼ºó¸´ÖÆ
ÉÏÊöÉèÖÃÌåÏÖÖ»ÔÊÐíIPµØµãΪ192.168.1.100ºÍ192.168.1.200µÄ¿Í»§¶Ë»á¼û¸Ãlocation¿é£¬ÆäËüIPµØµãµÄ¿Í»§¶Ë½«±»¾Ü¾ø¡£ÆäÖÐdeny allÌåÏ־ܾøËùÓÐÆäËüIPµØµãµÄ»á¼û¡£
¾Ü¾ø¶à¸öÌض¨IPµØµã»á¼û
location / { deny 192.168.1.100; deny 192.168.1.200; allow all; }
µÇ¼ºó¸´ÖÆ
ÉÏÊöÉèÖÃÌåÏÖ²»ÔÊÐíIPµØµãΪ192.168.1.100ºÍ192.168.1.200µÄ¿Í»§¶Ë»á¼û¸Ãlocation¿é£¬ÆäËüIPµØµãµÄ¿Í»§¶Ë½«±»ÔÊÐí¡£ÆäÖÐallow allÌåÏÖÔÊÐíËùÓÐÆäËüIPµØµãµÄ»á¼û¡£
ËÄ¡¢Ê¹ÓÃIPµØµã¶ÎµÄ¹ýÂËÉèÖÃ
Nginx»¹Ö§³ÖʹÓÃIPµØµã¶ÎÀ´¾ÙÐлá¼û¿ØÖÆÉèÖã¬ÏêϸµÄÉèÖÃÈçÏÂËùʾ£º
geo $whitelist { default 0; 192.168.1.0/24 1; } location / { if ($whitelist) { allow all; } deny all; }
µÇ¼ºó¸´ÖÆ
ÉÏÊöÉèÖÃÖУ¬geoÖ¸Áî½ç˵ÁËÒ»¸öÃûΪ$whitelistµÄ±äÁ¿£¬Æ¾Ö¤¿Í»§¶ËµÄIPµØµãÊÇ·ñÔÚ192.168.1.0/24Íø¶ÎÄÚ£¬½«¸Ã±äÁ¿µÄÖµÉèÖÃΪ1»ò0¡£È»ºóͨ¹ýifÓï¾äÀ´ÅжÏÊÇ·ñÔÊÐí»á¼û¸Ãlocation¿é¡£ÈôÊÇ$whitelistµÄֵΪ1£¬ÔòÔÊÐíËùÓпͻ§¶Ë»á¼û£»²»È»£¬¾Ü¾øËùÓлá¼û¡£
×ÛÉÏËùÊö£¬NginxÌṩÁ˶àÖÖ·½·¨À´ÊµÏÖ»ùÓÚIPµØµãµÄ»á¼û¿ØÖÆÉèÖá£Í¨¹ýºÏÀíÉèÖÃNginxµÄÉèÖÃÎļþ£¬ÖÎÀíÔ±¿ÉÒÔƾ֤ÏÖʵÐèÇó£¬ÎÞаµØ¿ØÖƶÔÍøÕ¾×ÊÔ´µÄ»á¼û£¬Ìá¸ßÍøÕ¾µÄÇå¾²ÐÔ¡£
²Î¿¼ÎÄÏ×£ºhttps://docs.nginx.com/nginx/admin-guide/security-controls/controlling-access-by-client-ip/
ÒÔÉϾÍÊÇNginxÔõÑùʵÏÖ»ùÓÚIPµØµãµÄ»á¼û¿ØÖÆÉèÖõÄÏêϸÄÚÈÝ£¬¸ü¶àÇë¹Ø×¢±¾ÍøÄÚÆäËüÏà¹ØÎÄÕ£¡