NginxÐéÄâÖ÷»úÉèÖã¬Í¬Ê±Íйܶà¸öÍøÕ¾Èë¿Ú
nginxÐéÄâÖ÷»úÉèÖã¬Í¬Ê±Íйܶà¸öÍøÕ¾Èë¿Ú
NginxÊÇÒ»ÖÖ¸ßÐÔÄܵÄWebЧÀÍÆ÷£¬Ëü¿ÉÒÔ´¦Öóͷ£¸ß²¢·¢µÄÇëÇó²¢Ìṩ¿ìËÙµÄÏìÓ¦¡£ÔÚÏÖʵӦÓÃÖУ¬ÎÒÃǾ³£ÐèÒªÍйܶà¸öÍøÕ¾Èë¿Ú£¬Õâ¾ÍÐèÒªÉèÖÃNginxµÄÐéÄâÖ÷»ú¡£±¾ÎĽ«ÏÈÈÝÔõÑùÉèÖÃNginxÐéÄâÖ÷»ú£¬Í¬Ê±Íйܶà¸öÍøÕ¾Èë¿ÚµÄÒªÁì¡£
Ê×ÏÈ£¬ÎÒÃÇÐèÒªÔÚNginxµÄÉèÖÃÎļþÖнç˵¶à¸öserver¿é£¬Ã¿¸öserver¿é´ú±íÒ»¸öÍøÕ¾Èë¿Ú¡£¿ÉÒÔ½«ÒÔÏÂʾÀý²åÈëµ½NginxµÄÉèÖÃÎļþÖУº
server { listen 80; server_name www.example1.com; root /var/www/example1; index index.html; location / { try_files $uri $uri/ =404; } } server { listen 80; server_name www.example2.com; root /var/www/example2; index index.html; location / { try_files $uri $uri/ =404; } }
µÇ¼ºó¸´ÖÆ
ÒÔÉÏʾÀýÖУ¬ÎÒÃǽç˵ÁËÁ½¸öserver¿é£¬»®·Ö´ú±íÁËwww.example1.comºÍwww.example2.comÁ½¸öÍøÕ¾Èë¿Ú¡£Ã¿¸öserver¿éÖж¼°üÀ¨ÁËlistenÖ¸ÁîÓÃÓÚÖ¸¶¨¼àÌýµÄ¶Ë¿Ú£¬server_nameÖ¸ÁîÓÃÓÚÖ¸¶¨ÓòÃû£¬rootÖ¸ÁîÓÃÓÚÖ¸¶¨ÍøÕ¾µÄ¸ùĿ¼£¬indexÖ¸ÁîÓÃÓÚÖ¸¶¨Ä¬ÈϵÄÊ×Ò³Îļþ¡£ÔÚlocation¿éÖУ¬ÎÒÃÇʹÓÃÁËtry_filesÖ¸ÁîÀ´´¦Öóͷ£ÇëÇó¡£
ÉèÖÃÍêÒÔÉÏÄÚÈݺó£¬ÉúÑIJ¢ÖØмÓÔØNginxµÄÉèÖÃÎļþ¡£¿ÉÒÔʹÓÃÒÔÏÂÏÂÁîÀ´ÑéÖ¤ÉèÖÃÎļþÊÇ·ñ׼ȷ£º
nginx -t
µÇ¼ºó¸´ÖÆ
ÈôÊÇûÓйýʧÌáÐÑ£¬ËµÃ÷ÉèÖÃÎļþûÓÐÓï·¨¹ýʧ¡£½ÓÏÂÀ´£¬ÎÒÃÇÐèÒª½«ÓòÃûÓëЧÀÍÆ÷µÄIPµØµã¾ÙÐÐÓ³Éä¡£¿ÉÒÔͨ¹ýÐÞ¸ÄÍâµØhostsÎļþ»òÕßÔÚDNSЧÀÍÆ÷ÖÐÌí¼ÓÏìÓ¦µÄÆÊÎö¼Í¼À´ÊµÏÖ¡£È·±£ÓòÃûÆÊÎöºó£¬Í¨¹ýä¯ÀÀÆ÷»á¼û¶ÔÓ¦µÄÓòÃû£¬¼´¿É¿´µ½ÏìÓ¦ÍøÕ¾µÄÄÚÈÝ¡£
ͬʱ£¬ÎÒÃÇÒ²¿ÉÒÔƾ֤ÐèÒªÉèÖøü¶àµÄserver¿éÀ´Íйܸü¶àµÄÍøÕ¾Èë¿Ú¡£Ö»ÐèÒª²Î¿¼ÒÔÉÏʾÀý£¬ÔÚÉèÖÃÎļþÖÐÌí¼ÓеÄserver¿é²¢¾ÙÐÐÏìÓ¦µÄÐ޸ļ´¿É¡£
ÖµµÃÒ»ÌáµÄÊÇ£¬ÒÔÉÏʾÀýÖÐʹÓÃÁË80¶Ë¿Ú×÷Ϊ¼àÌý¶Ë¿Ú¡£ÈôÊÇÐèҪʹÓÃÆäËû¶Ë¿Ú£¬Ç뽫listenÖ¸ÁîÖеĶ˿ںÅÐÞ¸ÄΪÏìÓ¦µÄÖµ¡£
×ܽ᣺
ͨ¹ýÉèÖÃNginxµÄÐéÄâÖ÷»ú£¬ÎÒÃÇ¿ÉÒÔÀû±ãµØͬʱÍйܶà¸öÍøÕ¾Èë¿Ú¡£Ã¿¸öÍøÕ¾Èë¿Ú¿ÉÒÔÓÐ×Ô¼ºµÄÓòÃûºÍ¸ùĿ¼£¬ÊµÏÖÁËÍøÕ¾µÄ¸ôÀëºÍ×ÔÁ¦ÐÔ¡£Í¨¹ý±¾ÎÄÌṩµÄʾÀý´úÂ룬ϣÍûÄܹ»×ÊÖú¶ÁÕß¿ìËٴ¶à¸öÍøÕ¾Èë¿ÚµÄÇéÐΣ¬²¢¸üºÃµØÓ¦ÓÃNginxµÄÓÅÊÆ¡£
ÒÔÉϾÍÊÇNginxÐéÄâÖ÷»úÉèÖã¬Í¬Ê±Íйܶà¸öÍøÕ¾Èë¿ÚµÄÏêϸÄÚÈÝ£¬¸ü¶àÇë¹Ø×¢±¾ÍøÄÚÆäËüÏà¹ØÎÄÕ£¡