ÆÊÎöNginxµÄÐéÄâÖ÷»úÉèÖúÍÓòÃûÆÊÎöʵÏÖϸ½Ú
ÆÊÎönginxµÄÐéÄâÖ÷»úÉèÖúÍÓòÃûÆÊÎöʵÏÖϸ½Ú
NginxÊÇÒ»¿î¸ßÐÔÄܵÄWebЧÀÍÆ÷ºÍ·´ÏòÊðÀíЧÀÍÆ÷£¬ÆÕ±éÓÃÓÚ»¥ÁªÍøÁìÓò¡£ÐéÄâÖ÷»úÉèÖúÍÓòÃûÆÊÎöÊÇNginxµÄÖ÷Òª¹¦Ð§Ö®Ò»£¬±¾ÎĽ«ÏêϸÆÊÎöNginxµÄÐéÄâÖ÷»úÉèÖúÍÓòÃûÆÊÎöµÄʵÏÖϸ½Ú£¬²¢¸ø³ö´úÂëʾÀý¡£
Ò»¡¢ÐéÄâÖ÷»úÉèÖÃ
ÐéÄâÖ÷»úÊÇÖ¸ÔÚһ̨ÎïÀíЧÀÍÆ÷ÉÏͬʱÍйܶà¸öÓòÃûµÄÄÜÁ¦¡£Nginxͨ¹ýÉèÖòî±ðµÄÐéÄâÖ÷»úÀ´ÊµÏÖÕâÒ»¹¦Ð§¡£
·¿ªNginxµÄÉèÖÃÎļþnginx.conf£¬Ò»Ñùƽ³£Î»ÓÚ/etc/nginxĿ¼Ï¡£
ÔÚhttp¿éÖÐÌí¼ÓÒÔÏ´úÂ룺
http { server { listen 80; server_name example.com; root /var/www/example.com; index index.html; } server { listen 80; server_name example2.com; root /var/www/example2.com; index index.html; } }
µÇ¼ºó¸´ÖÆ
ÒÔÉÏÉèÖýç˵ÁËÁ½¸öÐéÄâÖ÷»ú£¬»®·Ö¶ÔÓ¦example.comºÍexample2.comÁ½¸öÓòÃû¡£Ã¿¸öÐéÄâÖ÷»úµÄÉèÖÿéÖаüÀ¨Á˼àÌýµÄ¶Ë¿Ú¡¢Ð§ÀÍÆ÷Ãû³Æ¡¢¸ùĿ¼ºÍĬÈÏË÷ÒýÎļþ¡£
ÉúÑIJ¢Í˳önginx.confÎļþ¡£
ÖØмÓÔØNginxÉèÖã¬Ê¹ÆäÉúЧ£ºsudo nginx -s reload¡£
ͨ¹ýÒÔÉÏÉèÖã¬Nginx»áƾ֤ÇëÇóµÄÓòÃû½«ÇëÇóת·¢¸øÏìÓ¦µÄÐéÄâÖ÷»ú¾ÙÐд¦Àí¡£
¶þ¡¢ÓòÃûÆÊÎöʵÏÖϸ½Ú
±à¼/etc/hostsÎļþ£¬½«example.comºÍexample2.comÁ½¸öÓòÃûÖ¸ÏòЧÀÍÆ÷IPµØµã£¬ÀýÈ磺
127.0.0.1 example.com 127.0.0.1 example2.com
µÇ¼ºó¸´ÖÆ
ÕâÑùÍâµØ²âÊÔʱ£¬¿ÉÒÔÖ±½Ó»á¼ûÓòÃû£¬¶øÎÞÐè¾ÙÐÐÓòÃûÆÊÎö¡£
ÔÚÓòÃû×¢²áÉÌ´¦ÉèÖÃÓòÃûÆÊÎö£¬½«ÓòÃûÖ¸ÏòЧÀÍÆ÷IPµØµã¡£
ͨ¹ýDNSЧÀÍÆ÷¾ÙÐÐÓòÃûÆÊÎö£¬½«Óû§»á¼ûµÄÓòÃûÆÊÎöΪЧÀÍÆ÷µÄIPµØµã¡£
ÔÚÉÏÊöÁ½ÖÖÇéÐÎÖУ¬Óû§»á¼ûexample.com»òexample2.comʱ£¬¶¼»áͨ¹ýÓòÃûÆÊÎö½«ÓòÃûÓ³ÉäΪЧÀÍÆ÷IPµØµã£¬ÒÔ±ãNginxÄܹ»×¼È·µØÕÒµ½¶ÔÓ¦µÄÐéÄâÖ÷»ú¾ÙÐÐÇëÇó´¦Àí¡£
´úÂëʾÀý£º
ÔÚNginxµÄÐéÄâÖ÷»úÉèÖÃÖУ¬»¹¿ÉÒÔÌí¼ÓһЩÆäËûÉèÖòÎÊý£¬ÒÔʵÏÖ¸üÎÞаµÄ¹¦Ð§¡£
ÀýÈ磬¿ÉÒÔʹÓÃrewriteÖ¸ÁîʵÏÖURLÖØд£¬½«ÇëÇóÖض¨Ïòµ½ÆäËûÒ³Ãæ¡£ÒÔÏ´úÂëʾÀý½«ÇëÇóexample.com/oldpageÖض¨Ïòµ½example.com/newpage¡£
http { server { listen 80; server_name example.com; root /var/www/example.com; index index.html; location /oldpage { rewrite ^/oldpage /newpage; } } }
µÇ¼ºó¸´ÖÆ
ÒÔÉÏ´úÂëÖУ¬location /oldpageÉèÖý«Æ¥ÅäÒÔ/oldpage¿ªÍ·µÄURL£¬²¢Ê¹ÓÃrewriteÖ¸ÁÆäÖض¨Ïòµ½/newpage¡£
×ܽ᣺
±¾ÎÄͨ¹ýÆÊÎönginxµÄÐéÄâÖ÷»úÉèÖúÍÓòÃûÆÊÎöʵÏÖϸ½Ú£¬ÏêϸÏÈÈÝÁËNginxÔõÑùʵÏÖ¶à¸öÓòÃûµÄÍйܺÍÇëÇóת·¢¡£Í¨¹ýѧϰNginxµÄÐéÄâÖ÷»úÉèÖúÍÓòÃûÆÊÎö£¬¿ÉÒÔ¸üºÃµØÃ÷È·ºÍÓ¦ÓÃNginx£¬Ìá¸ßЧÀÍÆ÷µÄÐÔÄܺÍÎÞаÐÔ¡£
ÒÔÉϾÍÊÇÆÊÎöNginxµÄÐéÄâÖ÷»úÉèÖúÍÓòÃûÆÊÎöʵÏÖϸ½ÚµÄÏêϸÄÚÈÝ£¬¸ü¶àÇë¹Ø×¢±¾ÍøÄÚÆäËüÏà¹ØÎÄÕ£¡