Nginx»á¼û¿ØÖÆÉèÖã¬ÏÞÖÆÖ¸¶¨Óû§»á¼û
nginx»á¼û¿ØÖÆÉèÖã¬ÏÞÖÆÖ¸¶¨Óû§»á¼û
ÔÚWebЧÀÍÆ÷ÖУ¬»á¼û¿ØÖÆÊÇÒ»¸öÖ÷ÒªµÄÇå¾²²½·¥£¬ÓÃÓÚÏÞÖÆÌض¨Óû§»òIPµØµãµÄ»á¼ûȨÏÞ¡£Nginx×÷Ϊһ¿î¸ßÐÔÄܵÄWebЧÀÍÆ÷£¬Ò²ÌṩÁËÇ¿Ê¢µÄ»á¼û¿ØÖƹ¦Ð§¡£±¾ÎĽ«ÏÈÈÝÔõÑùʹÓÃNginxÉèÖÃÏÞÖÆÖ¸¶¨Óû§µÄ»á¼ûȨÏÞ£¬Í¬Ê±Ìṩ´úÂëʾÀý¹©²Î¿¼¡£
Ê×ÏÈ£¬ÎÒÃÇÐèҪ׼±¸Ò»¸ö»ù±¾µÄNginxÉèÖÃÎļþ¡£¼ÙÉèÎÒÃÇÒѾÓÐÒ»¸öÍøÕ¾£¬ÉèÖÃÎļþ·¾¶Îª/etc/nginx/nginx.conf¡£ÔÚ¸ÃÉèÖÃÎļþÖУ¬ÎÒÃǽ«Ìí¼ÓÒÔÏ»á¼û¿ØÖÆÉèÖãº
http { # ÆäËûÉèÖÃÄÚÈÝ... # ½ç˵һ¸öÑéÖ¤Îļþ£¬°üÀ¨ÔÊÐí»á¼ûµÄÓû§Ãû¼°ÃÜÂë auth_basic_user_file /etc/nginx/conf.d/.htpasswd; # ½ç˵һ¸ölocation¿é£¬¶ÔÖ¸¶¨URL·¾¶¾ÙÐлá¼û¿ØÖÆ location /private { # ¿ªÆô»ùÓÚHTTP»ù±¾ÈÏÖ¤µÄ»á¼û¿ØÖÆ auth_basic "Restricted"; # Ö¸¶¨Ö»¶ÔÌض¨Óû§Ãû¾ÙÐлá¼û¿ØÖÆ auth_basic_user_file /etc/nginx/conf.d/.htpasswd; # ÆäËûÉèÖÃÄÚÈÝ... } }
µÇ¼ºó¸´ÖÆ
ÔÚÉÏÊöÉèÖÃÖУ¬ÎÒÃÇʹÓÃÁËauth_basic_user_fileÖ¸Áî½ç˵ÁËÒ»¸öÑéÖ¤Îļþ£¬Ëü°üÀ¨ÁËÔÊÐí»á¼ûµÄÓû§Ãû¼°¶ÔÓ¦µÄÃÜÂë¡£Õâ¸öÑéÖ¤ÎļþµÄ·¾¶ÊÇ/etc/nginx/conf.d/.htpasswd£¬ÎÒÃÇ¿ÉÒÔƾ֤ÏÖʵÐèÒª¾ÙÐиü¸Ä¡£
½ÓÏÂÀ´£¬ÎÒÃÇʹÓÃlocation¿é¶ÔÖ¸¶¨µÄURL·¾¶¾ÙÐлá¼û¿ØÖÆ¡£ÔÚʾÀýÖУ¬ÎÒÃÇʹÓÃÁË/private×÷Ϊ±»ÏÞÖÆ»á¼ûµÄ·¾¶£¬Äú¿ÉÒÔƾ֤ÏÖÕæÏàÐξÙÐе÷½â¡£ÔÚ¸Ãlocation¿éÖУ¬ÎÒÃÇʹÓÃÁËauth_basicÖ¸ÁÆôÁË»ùÓÚHTTP»ù±¾ÈÏÖ¤µÄ»á¼û¿ØÖÆ¡£
ΪÁËÏÞÖÆÖ»ÓÐÌض¨Óû§¿ÉÒÔ»á¼û£¬ÎÒÃÇÔÙ´ÎʹÓÃÁËauth_basic_user_fileÖ¸Á²¢Ö¸¶¨ÁËÑéÖ¤ÎļþµÄ·¾¶¡£ÕâÑù£¬Ö»ÓÐÑéÖ¤ÎļþÖб£´æµÄÓû§Ãû²Å»ª»á¼û±»ÏÞÖƵÄURL·¾¶¡£
½ÓÏÂÀ´£¬ÎÒÃÇÐèҪ׼±¸ÑéÖ¤Îļþ.htpasswd¡£Õâ¸öÎļþ¿ÉÒÔʹÓÃhtpasswdÏÂÁîÌìÉú£¬ËüÊÇApache HTTP ServerÌṩµÄÒ»¸ö¹¤¾ß¡£ÔÚÖÕ¶ËÖÐÖ´ÐÐÒÔÏÂÏÂÁîÌìÉúÑéÖ¤Îļþ£º
htpasswd -c /etc/nginx/conf.d/.htpasswd user1
µÇ¼ºó¸´ÖÆ
ÉÏÊöÏÂÁÔÚÖ¸¶¨Â·¾¶ÏÂÌìÉúÒ»¸ö.htpasswdÎļþ£¬²¢ÎªÓû§user1ÉèÖÃÃÜÂ롣ΪÁËÔöÌí¸ü¶àÓû§£¬¿ÉÒÔÈ¥µô-cÑ¡ÏÈçÏÂËùʾ£º
htpasswd /etc/nginx/conf.d/.htpasswd user2
µÇ¼ºó¸´ÖÆ
ÔÚÕâÖ®ºó£¬Äú¿ÉÒÔƾ֤ÐèÒª¼ÌÐøΪ¸ü¶àÓû§ÉèÖÃÃÜÂë¡£
×îºó£¬ÎÒÃÇÐèÒªÖØÆôNginxЧÀÍÆ÷ʹÉèÖÃÉúЧ¡£ÔÚÖÕ¶ËÖÐÖ´ÐÐÒÔÏÂÏÂÁ
sudo service nginx restart
µÇ¼ºó¸´ÖÆ
ÏÖÔÚ£¬Ö»ÓÐÔÚÑéÖ¤ÎļþÖб£´æµÄÓû§²Å»ª»á¼û±»ÏÞÖƵÄURL·¾¶¡£ÆäËûÓû§½«ÎÞ·¨Í¨¹ýÊÚȨ£¬´Ó¶øÔöÌíÁËÍøÕ¾µÄÇå¾²ÐÔ¡£
×ܽ᣺
±¾ÎÄÏÈÈÝÁËÔõÑùʹÓÃNginxÉèÖÃÏÞÖÆÖ¸¶¨Óû§µÄ»á¼ûȨÏÞ¡£Ê×ÏÈ£¬ÎÒÃÇÔÚNginxÉèÖÃÎļþÖнç˵ÁËÑéÖ¤ÎļþµÄ·¾¶£¬²¢¿ªÆôÁË»ùÓÚHTTP»ù±¾ÈÏÖ¤µÄ»á¼û¿ØÖÆ¡£È»ºó£¬Í¨¹ýlocation¿éÖ¸¶¨Á˱»ÏÞÖÆ»á¼ûµÄURL·¾¶£¬²¢ÔÙ´ÎÖ¸¶¨ÁËÑéÖ¤ÎļþµÄ·¾¶£¬ÒÔÏÞÖÆÖ»ÓÐÌض¨Óû§¿ÉÒÔ»á¼û¡£×îºó£¬ÎÒÃÇʹÓÃhtpasswdÏÂÁîÌìÉúÁËÑéÖ¤Îļþ£¬²¢ÖØÆôÁËNginxЧÀÍÆ÷ʹÉèÖÃÉúЧ¡£
Ï£Íû±¾ÎĶÔÄúÃ÷È·Nginx»á¼û¿ØÖÆÉèÖ㬲¢Ñ§»áÏÞÖÆÖ¸¶¨Óû§µÄ»á¼ûȨÏÞÓÐËù×ÊÖú¡£ÈôÓÐÆäËûÎÊÌ⣬¿É²Î¿¼Nginx¹Ù·½Îĵµ»ò¾ÙÐнøÒ»²½µÄ×ÉѯºÍÑо¿¡£
ÒÔÉϾÍÊÇNginx»á¼û¿ØÖÆÉèÖã¬ÏÞÖÆÖ¸¶¨Óû§»á¼ûµÄÏêϸÄÚÈÝ£¬¸ü¶àÇë¹Ø×¢±¾ÍøÄÚÆäËüÏà¹ØÎÄÕ£¡