NginxÔõÑùʵÏÖ»ùÓÚÓû§ÈÏÖ¤µÄ»á¼û¿ØÖÆÉèÖÃ
NginxÔõÑùʵÏÖ»ùÓÚÓû§ÈÏÖ¤µÄ»á¼û¿ØÖÆÉèÖã¬ÐèÒªÏêϸ´úÂëʾÀý
NginxÊÇÒ»¿î¸ßÐÔÄܵÄWebЧÀÍÆ÷Èí¼þ£¬³ýÁ˳£¼ûµÄ¾²Ì¬ÎļþЧÀͺͷ´ÏòÊðÀí¹¦Ð§Í⣬Nginx»¹ÌṩÁËÐí¶àÇ¿Ê¢µÄ¹¦Ð§£¬ÈçÓû§ÈÏÖ¤¡¢»á¼û¿ØÖƵȡ£ÔÚ±¾ÎÄÖУ¬½«ÏÈÈÝÔõÑùʹÓÃNginxʵÏÖ»ùÓÚÓû§ÈÏÖ¤µÄ»á¼û¿ØÖÆÉèÖ㬲¢ÌṩÏêϸµÄ´úÂëʾÀý¡£
Ê×ÏÈ£¬ÎÒÃÇÐèÒªÔÚNginxµÄÉèÖÃÎļþÖÐÌí¼ÓÒÔÏ´úÂëÀ´ÆôÓûùÓÚÓû§ÈÏÖ¤µÄ¹¦Ð§£º
location / { auth_basic "Restricted"; # ÆôÓûùÓÚÓû§ÈÏÖ¤µÄ»á¼û¿ØÖÆ auth_basic_user_file /etc/nginx/.htpasswd; # Ö¸¶¨ÉúÑÄÓû§ÈÏÖ¤ÐÅÏ¢µÄÎļþ·¾¶ try_files $uri $uri/ /index.php?$query_string; # ÆäËûÐèÒª´¦Öóͷ£µÄÇëÇó }
µÇ¼ºó¸´ÖÆ
ÒÔÉÏ´úÂëÖУ¬auth_basicÌåÏÖÆôÓûùÓÚÓû§ÈÏÖ¤µÄ¹¦Ð§£¬²¢ÉèÖÃÁËÒ»¸öÈÏÖ¤ÌáÐÑÐÅÏ¢”Restricted”¡£ auth_basic_user_fileÖ¸¶¨ÁËÉúÑÄÓû§ÈÏÖ¤ÐÅÏ¢µÄÎļþ·¾¶£¬Õâ¸öÎļþ¿ÉÒÔʹÓÃhtpasswd¹¤¾ßÀ´ÌìÉú¡£
½ÓÏÂÀ´£¬ÎÒÃÇÐèҪʹÓÃhtpasswd¹¤¾ßÌìÉúÓû§ÈÏÖ¤ÐÅÏ¢Îļþ¡£ÔÚÖÕ¶ËÖÐÔËÐÐÒÔÏÂÏÂÁ
$ htpasswd -c /etc/nginx/.htpasswd username
µÇ¼ºó¸´ÖÆ
ÉÏÃæµÄÏÂÁîÖУ¬-cÌåÏÖ½¨ÉèÒ»¸öеÄÎļþ£¬/etc/nginx/.htpasswdÊÇÉúÑÄÓû§ÈÏÖ¤ÐÅÏ¢µÄÎļþ·¾¶£¬usernameÊÇÐèÒªÌí¼Óµ½ÈÏÖ¤ÎļþÖеÄÓû§Ãû¡£Ö´ÐÐÉÏÊöÏÂÁîºó£¬½«»áÈÃÄãÉèÖÃÃÜÂë¡£
ÏÖÔÚ£¬ÎÒÃÇÀ´½¨ÉèÒ»¸ö¼òÆÓµÄʾÀýÀ´²âÊÔ»ùÓÚÓû§ÈÏÖ¤µÄ»á¼û¿ØÖÆ¡£
¼ÙÉèÎÒÃÇÓÐÒ»¸öÍøÕ¾£¬ÆäÖÐÖ»ÓоÓÉÈÏÖ¤µÄÓû§²Å»ª»á¼û/adminĿ¼¡£ÔÚNginxµÄÉèÖÃÎļþÖÐÌí¼ÓÒÔÏ´úÂ룺
location /admin/ { auth_basic "Restricted"; # ÆôÓûùÓÚÓû§ÈÏÖ¤µÄ»á¼û¿ØÖÆ auth_basic_user_file /etc/nginx/.htpasswd; # Ö¸¶¨ÉúÑÄÓû§ÈÏÖ¤ÐÅÏ¢µÄÎļþ·¾¶ try_files $uri $uri/ /index.php?$query_string; # ÆäËûÐèÒª´¦Öóͷ£µÄÇëÇó }
µÇ¼ºó¸´ÖÆ
ÉúÑÄÉèÖÃÎļþ²¢ÖØмÓÔØNginxЧÀÍ¡£È»ºó£¬ÊµÑé»á¼û/adminĿ¼£¬Ä㽫»á±»ÒªÇóÊäÈëÓû§ÃûºÍÃÜÂë¡£
ÒÔÉϾÍÊÇʹÓÃNginxʵÏÖ»ùÓÚÓû§ÈÏÖ¤µÄ»á¼û¿ØÖÆÉèÖõÄÏêϸ°ì·¨ºÍʾÀý´úÂ롣ͨ¹ýʹÓÃÕâЩ¹¦Ð§£¬¿ÉÒÔÓÐÓõر£»¤ÄãµÄÍøÕ¾²»±»Î´¾ÊÚȨµÄÓû§»á¼û¡£Çë¼Ç×Å£¬ÎªÁË°ü¹ÜÇå¾²ÐÔ£¬Ó¦¸Ã°´ÆÚ¸ü¸ÄÓû§ÈÏÖ¤ÐÅÏ¢£¬²¢ÏÞÖƶÔÉúÑÄÓû§ÈÏÖ¤ÐÅÏ¢µÄÎļþµÄ»á¼ûȨÏÞ¡£
ÒÔÉϾÍÊÇNginxÔõÑùʵÏÖ»ùÓÚÓû§ÈÏÖ¤µÄ»á¼û¿ØÖÆÉèÖõÄÏêϸÄÚÈÝ£¬¸ü¶àÇë¹Ø×¢±¾ÍøÄÚÆäËüÏà¹ØÎÄÕ£¡