NginxÐéÄâÖ÷»úÉèÖã¬ÖÎÀí¶à¸öÍøÕ¾Èë¿Ú
nginxÐéÄâÖ÷»úÉèÖã¬ÖÎÀí¶à¸öÍøÕ¾Èë¿Ú
ÔÚ»¥ÁªÍøµÄÉú³¤Àú³ÌÖУ¬½¨Éè×Ô¼ºµÄÍøÕ¾ÒѾ³ÉΪÐí¶àÈ˵ÄÑ¡Ôñ¡£È»¶ø£¬Ëæ×ÅÍøÕ¾ÊýÄ¿µÄÔöÌí£¬ÔõÑùÓÐÓõØÖÎÀí¶à¸öÍøÕ¾Èë¿Ú³ÉΪÁËÒ»¸öÖ÷ÒªµÄÎÊÌâ¡£NginxÐéÄâÖ÷»úµÄÉèÖÿÉÒÔ×ÊÖúÎÒÃÇʵÏÖÕâһĿµÄ¡£
NginxÊÇÒ»¿î¸ßÐÔÄܵÄWebЧÀÍÆ÷ºÍ·´ÏòÊðÀíЧÀÍÆ÷£¬¾ß±¸´¦Àí¸ß²¢·¢ÇëÇóµÄÄÜÁ¦¡£ÆäÐéÄâÖ÷»úÉèÖÃÄܹ»ÈÃÒ»¸öЧÀÍÆ÷ͬʱÍйܶà¸öÍøÕ¾£¬²¢½«²î±ðµÄÍøÕ¾ÇëÇóת·¢µ½²î±ðµÄºó¶ËЧÀÍÆ÷ÉÏ¡£ÏÂÃæÎÒÃǽ«ÏÈÈÝÔõÑùʹÓÃNginx¾ÙÐÐÐéÄâÖ÷»úµÄÉèÖá£
Ê×ÏÈ£¬ÎÒÃÇÐèÒªÔÚNginxµÄÉèÖÃÎļþÖоÙÐÐÏìÓ¦µÄÉèÖá£Ò»Ñùƽ³£¶øÑÔ£¬NginxµÄÉèÖÃÎļþλÓÚ/etc/nginxĿ¼Ï¡£ÎÒÃÇ¿ÉÒÔʹÓÃí§ÒâÎı¾±à¼Æ÷·¿ª¸ÃÎļþ£¬¾ÙÐÐÉèÖá£
ÔÚÉèÖÃÎļþÖУ¬Ã¿¸öÐéÄâÖ÷»úµÄÉèÖö¼ÐèҪʹÓÃÒ»¸öserver¿éÀ´¾ÙÐÐÐÎò¡£Ã¿¸öserver¿éÖÐÐèÒªÖ¸¶¨¼àÌýµÄ¶Ë¿ÚºÍÐéÄâÖ÷»úµÄÓòÃû¡£ÀýÈ磺
server { listen 80; server_name www.example1.com; ... } server { listen 80; server_name www.example2.com; ... }
µÇ¼ºó¸´ÖÆ
ÔÚÒÔÉϵÄʾÀýÖУ¬ÎÒÃǽç˵ÁËÁ½¸öserver¿é¡£Ò»¸ö¼àÌý80¶Ë¿Ú²¢½«ÇëÇóת·¢µ½www.example1.comÍøÕ¾£¬ÁíÒ»¸ö¼àÌý80¶Ë¿Ú²¢½«ÇëÇóת·¢µ½www.example2.comÍøÕ¾¡£
½ÓÏÂÀ´£¬ÎÒÃÇÐèÒªÉèÖÃÐéÄâÖ÷»úµÄ¸ùĿ¼ºÍÆäËûÏà¹ØµÄ²ÎÊý¡£ÀýÈ磺
server { listen 80; server_name www.example1.com; root /var/www/example1; ... } server { listen 80; server_name www.example2.com; root /var/www/example2; ... }
µÇ¼ºó¸´ÖÆ
ÔÚÒÔÉϵÄʾÀýÖУ¬ÎÒÃÇΪÿ¸öÓòÃûÖ¸¶¨ÁËÏìÓ¦µÄ¸ùĿ¼¡£ÕâÑùµ±Óû§ÇëÇówww.example1.comʱ£¬Nginx»á½«ÇëÇóת·¢µ½/var/www/example1Ŀ¼Ï£¬¶øÇëÇówww.example2.comʱ£¬Ôò»áת·¢µ½/var/www/example2Ŀ¼Ï¡£
³ýÁ˸ùĿ¼£¬ÎÒÃÇ»¹¿ÉÒÔÉèÖûá¼ûÈÕÖ¾¡¢¹ýʧҳÃæµÈÆäËûÏà¹ØµÄ²ÎÊý¡£ÀýÈ磺
server { listen 80; server_name www.example1.com; root /var/www/example1; access_log /var/log/nginx/example1.access.log; error_log /var/log/nginx/example1.error.log; ... } server { listen 80; server_name www.example2.com; root /var/www/example2; access_log /var/log/nginx/example2.access.log; error_log /var/log/nginx/example2.error.log; ... }
µÇ¼ºó¸´ÖÆ
ÔÚÒÔÉϵÄʾÀýÖУ¬ÎÒÃÇΪÿ¸öÐéÄâÖ÷»úÉèÖÃÁË×ÔÁ¦µÄ»á¼ûÈÕÖ¾ºÍ¹ýʧÈÕÖ¾¡£ÕâÑù¿ÉÒÔÀû±ãÎÒÃǶÔÿ¸öÍøÕ¾µÄ»á¼û¾ÙÐмà¿ØºÍÆÊÎö¡£
×îºó£¬ÎÒÃÇÐèÒªÖØÆôNginxÒÔʹÉèÖÃÉúЧ¡£¿ÉÒÔʹÓÃÒÔÏÂÏÂÁîÀ´ÖØÆôNginx£º
sudo service nginx restart
µÇ¼ºó¸´ÖÆ
ͨ¹ýÒÔÉϵÄÉèÖã¬ÎÒÃÇ¿ÉÒÔͨ¹ý²î±ðµÄÓòÃû»á¼ûµ½²î±ðµÄÍøÕ¾£¬²¢ÇÒÿ¸öÍøÕ¾¶¼¿ÉÒÔÓÐ×ÔÁ¦µÄ¸ùĿ¼¡¢»á¼ûÈÕÖ¾ºÍ¹ýʧÈÕÖ¾¡£
×ܽáÆðÀ´£¬NginxµÄÐéÄâÖ÷»úÉèÖÿÉÒÔ×ÊÖúÎÒÃÇÖÎÀí¶à¸öÍøÕ¾Èë¿Ú¡£Í¨¹ýÉèÖüàÌýµÄ¶Ë¿Ú¡¢ÓòÃû¡¢¸ùĿ¼¡¢ÈÕÖ¾µÈ²ÎÊý£¬ÎÒÃÇ¿ÉÒÔÇáËɵØʵÏÖ¶à¸öÍøÕ¾µÄÍйܺÍÖÎÀí¡£Ï£Íû±¾ÎÄÄܶԸ÷ÈËÃ÷È·NginxÐéÄâÖ÷»úÉèÖÃÓÐËù×ÊÖú¡£
²Î¿¼Á´½Ó£º
https://www.nginx.com/
https://www.digitalocean.com/community/tutorials/how-to-set-up-nginx-server-blocks-virtual-hosts-on-ubuntu-14-04-lts
ÒÔÉϾÍÊÇNginxÐéÄâÖ÷»úÉèÖã¬ÖÎÀí¶à¸öÍøÕ¾Èë¿ÚµÄÏêϸÄÚÈÝ£¬¸ü¶àÇë¹Ø×¢±¾ÍøÄÚÆäËüÏà¹ØÎÄÕ£¡