NginxÔõÑùʵÏÖ»ùÓÚÇëÇóȪԴÓòÃûµÄ»á¼û¿ØÖÆÉèÖÃ
NginxÔõÑùʵÏÖ»ùÓÚÇëÇóȪԴÓòÃûµÄ»á¼û¿ØÖÆÉèÖã¬ÐèÒªÏêϸ´úÂëʾÀý
NginxÊÇÒ»¿î¸ßÐÔÄܵÄWebЧÀÍÆ÷Èí¼þ£¬Ëü²»µ«¿ÉÒÔ×÷Ϊ¾²Ì¬ÎļþЧÀÍÆ÷£¬»¹¿ÉÒÔͨ¹ýÉèÖÃʵÏÖÎÞаµÄ»á¼û¿ØÖÆ¡£±¾ÎĽ«ÏÈÈÝÔõÑùͨ¹ýNginxʵÏÖ»ùÓÚÇëÇóȪԴÓòÃûµÄ»á¼û¿ØÖÆÉèÖ㬲¢ÌṩÏêϸµÄ´úÂëʾÀý¡£
NginxÉèÖÃÎļþͨ³£Î»ÓÚ/etc/nginx/nginx.conf£¬ÎÒÃÇ¿ÉÒÔÔÚ¸ÃÎļþÖÐÌí¼ÓÏà¹ØµÄÉèÖá£ÏÂÃæÊÇÒ»¸ö»ù±¾µÄNginxÉèÖÃÎļþʾÀý£º
http { server { listen 80; server_name example.com; location / { proxy_pass http://localhost:8000; } } }
µÇ¼ºó¸´ÖÆ
ÔÚÉÏÃæµÄʾÀýÖУ¬ÎÒÃǼàÌý80¶Ë¿Ú£¬²¢½«ËùÓÐÇëÇóת·¢µ½ÍâµØµÄ8000¶Ë¿Ú¡£Õâ¸öÉèÖÃÖÐʹÓÃÁËserver_nameÖ¸ÁîÀ´Ö¸¶¨½ÓÊÜÇëÇóµÄÓòÃû¡£Ä¬ÈÏÇéÐÎÏ£¬Nginx»á½ÓÊÜËùÓÐÓòÃûµÄÇëÇó¡£ÈôÊÇÄãÏëҪʵÏÖ»ùÓÚÇëÇóȪԴÓòÃûµÄ»á¼û¿ØÖÆÉèÖ㬿ÉÒÔʹÓÃifÖ¸ÁîÍŽá$request_headerÀ´ÊµÏÖ¡£
ÏÂÃæÊÇÒ»¸öʾÀý£º
http { server { listen 80; if ($http_host ~* "^(www.)?example.com$") { location / { proxy_pass http://localhost:8000; } } if ($http_host ~* "^(www.)?example2.com$") { return 403; } } }
µÇ¼ºó¸´ÖÆ
ÔÚÉÏÃæµÄʾÀýÖУ¬ÎÒÃÇʹÓÃÁËÁ½¸öifÖ¸ÁîÀ´ÊµÏÖ»ùÓÚÇëÇóȪԴÓòÃûµÄ»á¼û¿ØÖÆÉèÖᣵÚÒ»¸öif¿éÖУ¬ÎÒÃÇʹÓÃ$http_host±äÁ¿À´Æ¥Åäexample.comÓòÃû£¬²¢½«ÇëÇóת·¢µ½ÍâµØµÄ8000¶Ë¿Ú¡£¶øÔÚµÚ¶þ¸öif¿éÖУ¬ÎÒÃÇͬÑùʹÓÃ$http_host±äÁ¿À´Æ¥Åäexample2.comÓòÃû£¬²¢·µ»Ø403¹ýʧ¡£
ÐèҪעÖصÄÊÇ£¬ÔÚNginxÖÐʹÓÃifÖ¸Áî»á´øÀ´ÐÔÄÜ·½ÃæµÄËðʧ¡£ÈôÊÇ¿ÉÄܵĻ°£¬½¨ÒéʹÓÃÕýÔò±í´ïʽ¾ÙÐÐÓòÃûÆ¥Å䣬ͬʱʹÓÃlocationÖ¸ÁîÀ´ÊµÏÖ¸ü¸ßЧµÄÉèÖá£
³ýÁËʹÓÃifÖ¸ÁNginx»¹ÌṩÁËÐí¶àÆäËûµÄÖ¸ÁîºÍÄ£¿é£¬ÓÃÓÚʵÏÖ¸üÖØ´óµÄ»á¼û¿ØÖÆÉèÖ㬺ñÈngx_http_access_moduleÄ£¿é¡¢ngx_http_auth_basic_moduleÄ£¿éµÈ¡£Äã¿ÉÒÔƾ֤ÏÖʵÐèÇóÑ¡ÔñºÏÊʵÄÉèÖ÷½·¨¡£
×ܽáÆðÀ´£¬Í¨¹ýNginx¿ÉÒÔʵÏÖ»ùÓÚÇëÇóȪԴÓòÃûµÄ»á¼û¿ØÖÆÉèÖá£ÔÚÉèÖÃÎļþÖÐʹÓÃifÖ¸ÁîÍŽá$http_host±äÁ¿£¬¿ÉÒÔƾ֤ÇëÇóµÄÓòÃû¾ÙÐÐÌõ¼þÅжϣ¬´Ó¶øʵÏÖÎÞаµÄ»á¼û¿ØÖÆ¡£ËäÈ»£¬ÎªÁË°ü¹ÜÐÔÄÜ£¬½¨ÒéÑ¡ÔñºÏÊʵÄÉèÖ÷½·¨£¬²¢ÍŽáÆäËûÄ£¿éʵÏÖ¸üÖØ´óµÄ¿ØÖÆÐèÇó¡£
ÒÔÉϾÍÊÇNginxÔõÑùʵÏÖ»ùÓÚÇëÇóȪԴÓòÃûµÄ»á¼û¿ØÖÆÉèÖõÄÏêϸÄÚÈÝ£¬¸ü¶àÇë¹Ø×¢±¾ÍøÄÚÆäËüÏà¹ØÎÄÕ£¡